8000 [FrameworkBundle] Object Mapper component bindings · symfony/symfony@af3dd91 · GitHub
[go: up one dir, main page]

Skip to content

Commit af3dd91

Browse files
committed
[FrameworkBundle] Object Mapper component bindings
1 parent d748015 commit af3dd91

File tree

11 files changed

+167
-2
lines changed

11 files changed

+167
-2
lines changed

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,8 @@
125125
use Symfony\Component\Notifier\Recipient\Recipient;
126126
use Symfony\Component\Notifier\TexterInterface;
127127
use Symfony\Component\Notifier\Transport\TransportFactoryInterface as NotifierTransportFactoryInterface;
128+
use Symfony\Component\ObjectMapper\CallableInterface;
129+
use Symfony\Component\ObjectMapper\ObjectMapperInterface;
128130
use Symfony\Component\Process\Messenger\RunProcessMessageHandler;
129131
use Symfony\Component\PropertyAccess\PropertyAccessor;
130132
use Symfony\Component\PropertyInfo\Extractor\PhpDocExtractor;
@@ -776,6 +778,12 @@ private function registerFormConfiguration(array $config, ContainerBuilder $cont
776778
if (!ContainerBuilder::willBeAvailable('symfony/translation', Translator::class, ['symfony/framework-bundle', 'symfony/form'])) {
777779
$container->removeDefinition('form.type_extension.upload.validator');
778780
}
781+
782+
if (interface_exists(ObjectMapperInterface::class)) {
783+
$loader->load('object_mapper.php');
784+
$container->registerForAutoconfiguration(CallableInterface::class)
785+
->addTag('object_mapper.callable');
786+
}
779787
}
780788

781789
private function registerHttpCacheConfiguration(array $config, ContainerBuilder $container, bool $httpMethodOverride): void
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
13+
14+
use Symfony\Component\ObjectMapper\CallablesLocator;
15+
use Symfony\Component\ObjectMapper\Metadata\MapperMetadataFactoryInterface;
16+
use Symfony\Component\ObjectMapper\Metadata\ReflectionMapperMetadataFactory;
17+
use Symfony\Component\ObjectMapper\ObjectMapper;
18+
use Symfony\Component\ObjectMapper\ObjectMapperInterface;
19+
20+
return static function (ContainerConfigurator $container) {
21+
$container->services()
22+
->set('object_mapper.metadata_factory', ReflectionMapperMetadataFactory::class)
23+
->alias(ReflectionMapperMetadataFactory::class, 'object_mapper.metadata_factory')
24+
->alias(MapperMetadataFactoryInterface::class, 'object_mapper.metadata_factory')
25+
26+
->set('object_mapper', ObjectMapper::class)
27+
->args([
28+
service('object_mapper.metadata_factory')->ignoreOnInvalid(),
29+
service('property_accessor')->ignoreOnInvalid(),
30+
tagged_locator('object_mapper.callable'),
31+
])
32+
->alias(ObjectMapper::class, 'object_mapper')
33+
->alias(ObjectMapperInterface::class, 'object_mapper')
34+
;
35+
};
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\FrameworkBundle\Tests\Fixtures\ObjectMapper;
13+
14+
final class ObjectMapped
15+
{
16+
public string $a;
17+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\FrameworkBundle\Tests\Fixtures\ObjectMapper;
13+
14+
use Symfony\Component\ObjectMapper\Attribute\Map;
15+
16+
#[Map(target: ObjectMapped::class)]
17+
final class ObjectToBeMapped
18+
{
19+
#[Map(transform: TransformCallable::class)]
20+
public string $a = 'nottransformed';
21+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\FrameworkBundle\Tests\Fixtures\ObjectMapper;
13+
14+
use Symfony\Component\ObjectMapper\CallableInterface;
15+
16+
/**
17+
* @implements CallableInterface<ObjectToBeMapped>
18+
*/
19+
final class TransformCallable implements CallableInterface
20+
{
21+
public function __invoke(mixed $value, object $object): mixed
22+
{
23+
return 'transformed';
24+
}
25+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\FrameworkBundle\Tests\Functional;
13+
14+
use Symfony\Bundle\FrameworkBundle\Tests\Fixtures\ObjectMapper\ObjectMapped;
15+
use Symfony\Bundle\FrameworkBundle\Tests\Fixtures\ObjectMapper\ObjectToBeMapped;
16+
17+
/**
18+
* @author Kévin Dunglas <dunglas@gmail.com>
19+
*/
20+
class ObjectMapperTest extends AbstractWebTestCase
21+
{
22+
public function testObjectMapper(): void
23+
{
24+
static::bootKernel(['test_case' => 'ObjectMapper']);
25+
26+
/** @var Symfony\Component\ObjectMapper\ObjectMapperInterface<ObjectMapped> */
27+
$objectMapper = static::getContainer()->get('object_mapper.alias');
28+
$mapped = $objectMapper->map(new ObjectToBeMapped());
29+
$this->assertSame($mapped->a, 'transformed');
30+
}
31+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
13+
14+
return [
15+
new FrameworkBundle(),
16+
];
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
imports:
2+
- { resource: ../config/default.yml }
3+
4+
services:
5+
object_mapper.alias:
6+
alias: object_mapper
7+
public: true
8+
Symfony\Bundle\FrameworkBundle\Tests\Fixtures\ObjectMapper\TransformCallable:
9+
autoconfigure: true

src/Symfony/Bundle/FrameworkBundle/composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@
5959
"symfony/scheduler": "^6.4.4|^7.0.4",
6060
"symfony/security-bundle": "^6.4|^7.0",
6161
"symfony/semaphore": "^6.4|^7.0",
62-
"symfony/serializer": "^7.1",
62+
"symfony/serializer": "^6.4|^7.0",
63+
"symfony/object-mapper": "^7.2",
6364
"symfony/stopwatch": "^6.4|^7.0",
6465
"symfony/string": "^6.4|^7.0",
6566
"symfony/translation": "^6.4|^7.0",

src/Symfony/Component/ObjectMapper/Attribute/Map.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace Symfony\Component\ObjectMapper\Attribute;
1313

14+
use Symfony\Component\ObjectMapper\CallableInterface;
15+
1416
/**
1517
* Configures a class or a property to map to.
1618
*

0 commit comments

Comments
 (0)
0