Skip to content

[FrameworkBundle] Binding for Object Mapper component #58654

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ class UnusedTagsPass implements CompilerPassInterface
'validator.group_provider',
'validator.initializer',
'workflow',
'object_mapper.transform_callable',
'object_mapper.condition_callable',
];

public function process(ContainerBuilder $container): void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,9 @@
use Symfony\Component\Notifier\Recipient\Recipient;
use Symfony\Component\Notifier\TexterInterface;
use Symfony\Component\Notifier\Transport\TransportFactoryInterface as NotifierTransportFactoryInterface;
use Symfony\Component\ObjectMapper\ConditionCallableInterface;
use Symfony\Component\ObjectMapper\ObjectMapperInterface;
use Symfony\Component\ObjectMapper\TransformCallableInterface;
use Symfony\Component\Process\Messenger\RunProcessMessageHandler;
use Symfony\Component\PropertyAccess\PropertyAccessor;
use Symfony\Component\PropertyInfo\Extractor\ConstructorArgumentTypeExtractorInterface;
Expand Down Expand Up @@ -863,6 +866,14 @@ private function registerFormConfiguration(array $config, ContainerBuilder $cont
if (!ContainerBuilder::willBeAvailable('symfony/translation', Translator::class, ['symfony/framework-bundle', 'symfony/form'])) {
$container->removeDefinition('form.type_extension.upload.validator');
}

if (ContainerBuilder::willBeAvailable('symfony/object-mapper', ObjectMapperInterface::class, ['symfony/framework-bundle'])) {
$loader->load('object_mapper.php');
$container->registerForAutoconfiguration(TransformCallableInterface::class)
->addTag('object_mapper.transform_callable');
$container->registerForAutoconfiguration(ConditionCallableInterface::class)
->addTag('object_mapper.condition_callable');
}
}

private function registerHttpCacheConfiguration(array $config, ContainerBuilder $container, bool $httpMethodOverride): void
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\DependencyInjection\Loader\Configurator;

use Symfony\Component\ObjectMapper\Metadata\ObjectMapperMetadataFactoryInterface;
use Symfony\Component\ObjectMapper\Metadata\ReflectionObjectMapperMetadataFactory;
use Symfony\Component\ObjectMapper\ObjectMapper;
use Symfony\Component\ObjectMapper\ObjectMapperInterface;

return static function (ContainerConfigurator $container) {
$container->services()
->set('object_mapper.metadata_factory', ReflectionObjectMapperMetadataFactory::class)
->alias(ObjectMapperMetadataFactoryInterface::class, 'object_mapper.metadata_factory')

->set('object_mapper', ObjectMapper::class)
->args([
service('object_mapper.metadata_factory'),
service('property_accessor')->ignoreOnInvalid(),
tagged_locator('object_mapper.transform_callable'),
tagged_locator('object_mapper.condition_callable'),
])
->alias(ObjectMapperInterface::class, 'object_mapper')
;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bundle\FrameworkBundle\Tests\Fixtures\ObjectMapper;

final class ObjectMapped
{
public string $a;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bundle\FrameworkBundle\Tests\Fixtures\ObjectMapper;

use Symfony\Component\ObjectMapper\Attribute\Map;

#[Map(target: ObjectMapped::class)]
final class ObjectToBeMapped
{
#[Map(transform: TransformCallable::class)]
public string $a = 'nottransformed';
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bundle\FrameworkBundle\Tests\Fixtures\ObjectMapper;

use Symfony\Component\ObjectMapper\TransformCallableInterface;

/**
* @implements TransformCallableInterface<ObjectToBeMapped>
*/
final class TransformCallable implements TransformCallableInterface
{
public function __invoke(mixed $value, object $object): mixed
{
return 'transformed';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bundle\FrameworkBundle\Tests\Functional;

use Symfony\Bundle\FrameworkBundle\Tests\Fixtures\ObjectMapper\ObjectMapped;
use Symfony\Bundle\FrameworkBundle\Tests\Fixtures\ObjectMapper\ObjectToBeMapped;

/**
* @author Kévin Dunglas <dunglas@gmail.com>
*/
class ObjectMapperTest extends AbstractWebTestCase
{
public function testObjectMapper()
{
static::bootKernel(['test_case' => 'ObjectMapper']);

/** @var Symfony\Component\ObjectMapper\ObjectMapperInterface<ObjectMapped> */
$objectMapper = static::getContainer()->get('object_mapper.alias');
$mapped = $objectMapper->map(new ObjectToBeMapped());
$this->assertSame($mapped->a, 'transformed');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

use Symfony\Bundle\FrameworkBundle\FrameworkBundle;

return [
new FrameworkBundle(),
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
imports:
- { resource: ../config/default.yml }

services:
object_mapper.alias:
alias: object_mapper
public: true
Symfony\Bundle\FrameworkBundle\Tests\Fixtures\ObjectMapper\TransformCallable:
autoconfigure: true
1 change: 1 addition & 0 deletions src/Symfony/Bundle/FrameworkBundle/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
"symfony/messenger": "^6.4|^7.0",
"symfony/mime": "^6.4|^7.0",
"symfony/notifier": "^6.4|^7.0",
"symfony/object-mapper": "^7.3",
"symfony/process": "^6.4|^7.0",
"symfony/rate-limiter": "^6.4|^7.0",
"symfony/scheduler": "^6.4.4|^7.0.4",
Expand Down
Loading