Skip to content

Commit f797e9f

Browse files
committed
fix(object-mapper): use existing symfony object-mapper
1 parent bad923f commit f797e9f

File tree

4 files changed

+59
-10
lines changed

4 files changed

+59
-10
lines changed

src/Symfony/Bundle/ApiPlatformBundle.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use ApiPlatform\Symfony\Bundle\DependencyInjection\Compiler\GraphQlTypePass;
2424
use ApiPlatform\Symfony\Bundle\DependencyInjection\Compiler\MetadataAwareNameConverterPass;
2525
use ApiPlatform\Symfony\Bundle\DependencyInjection\Compiler\MutatorPass;
26+
use ApiPlatform\Symfony\Bundle\DependencyInjection\Compiler\ObjectMapperPass;
2627
use ApiPlatform\Symfony\Bundle\DependencyInjection\Compiler\SerializerMappingLoaderPass;
2728
use ApiPlatform\Symfony\Bundle\DependencyInjection\Compiler\TestClientPass;
2829
use ApiPlatform\Symfony\Bundle\DependencyInjection\Compiler\TestMercureHubPass;
@@ -59,5 +60,6 @@ public function build(ContainerBuilder $container): void
5960
$container->addCompilerPass(new AuthenticatorManagerPass());
6061
$container->addCompilerPass(new SerializerMappingLoaderPass());
6162
$container->addCompilerPass(new MutatorPass());
63+
$container->addCompilerPass(new ObjectMapperPass());
6264
}
6365
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
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+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Symfony\Bundle\DependencyInjection\Compiler;
15+
16+
use Symfony\Component\DependencyInjection\Argument\ServiceLocatorArgument;
17+
use Symfony\Component\DependencyInjection\Argument\TaggedIteratorArgument;
18+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
19+
use Symfony\Component\DependencyInjection\ContainerBuilder;
20+
use Symfony\Component\DependencyInjection\ContainerInterface;
21+
use Symfony\Component\DependencyInjection\Definition;
22+
use Symfony\Component\DependencyInjection\Reference;
23+
use Symfony\Component\ObjectMapper\ObjectMapper;
24+
25+
/**
26+
* Creates the Object Mapper.
27+
*
28+
* @author Florent Blaison <[email protected]>
29+
*/
30+
final class ObjectMapperPass implements CompilerPassInterface
31+
{
32+
public function process(ContainerBuilder $container): void
33+
{
34+
if (!class_exists(ObjectMapper::class)) {
35+
return;
36+
}
37+
38+
if ($container->has('object_mapper.metadata_factory')) {
39+
$container->setAlias('api_platform.object_mapper.metadata_factory', 'object_mapper.metadata_factory');
40+
} else {
41+
$container->setDefinition('api_platform.object_mapper.metadata_factory', new Definition('Symfony\Component\ObjectMapper\Metadata\ReflectionObjectMapperMetadataFactory'));
42+
}
43+
44+
if ($container->has('object_mapper')) {
45+
$container->setAlias('api_platform.object_mapper', 'object_mapper');
46+
} else {
47+
$container->setDefinition('api_platform.object_mapper', new Definition('Symfony\Component\ObjectMapper\ObjectMapper', [
48+
new Reference('api_platform.object_mapper.metadata_factory'),
49+
new Reference('property_accessor', ContainerInterface::NULL_ON_INVALID_REFERENCE),
50+
new ServiceLocatorArgument(new TaggedIteratorArgument('object_mapper.transform_callable', null, null, true)),
51+
new ServiceLocatorArgument(new TaggedIteratorArgument('object_mapper.condition_callable', null, null, true)),
52+
]));
53+
}
54+
}
55+
}

src/Symfony/Bundle/Resources/config/state/object_mapper.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,6 @@
1616
return function (ContainerConfigurator $container) {
1717
$services = $container->services();
1818

19-
$services->set('api_platform.object_mapper.metadata_factory', 'Symfony\Component\ObjectMapper\Metadata\ReflectionObjectMapperMetadataFactory');
20-
21-
$services->set('api_platform.object_mapper', 'Symfony\Component\ObjectMapper\ObjectMapper')
22-
->args([
23-
service('api_platform.object_mapper.metadata_factory'),
24-
service('property_accessor')->nullOnInvalid(),
25-
tagged_locator('object_mapper.transform_callable'),
26-
tagged_locator('object_mapper.condition_callable'),
27-
]);
28-
2919
$services->set('api_platform.object_mapper.relation', 'ApiPlatform\State\ObjectMapper\ObjectMapper')
3020
->decorate('api_platform.object_mapper', null, -255)
3121
->args([service('api_platform.object_mapper.relation.inner')]);

tests/Symfony/Bundle/ApiPlatformBundleTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use ApiPlatform\Symfony\Bundle\DependencyInjection\Compiler\GraphQlTypePass;
2525
use ApiPlatform\Symfony\Bundle\DependencyInjection\Compiler\MetadataAwareNameConverterPass;
2626
use ApiPlatform\Symfony\Bundle\DependencyInjection\Compiler\MutatorPass;
27+
use ApiPlatform\Symfony\Bundle\DependencyInjection\Compiler\ObjectMapperPass;
2728
use ApiPlatform\Symfony\Bundle\DependencyInjection\Compiler\SerializerMappingLoaderPass;
2829
use ApiPlatform\Symfony\Bundle\DependencyInjection\Compiler\TestClientPass;
2930
use ApiPlatform\Symfony\Bundle\DependencyInjection\Compiler\TestMercureHubPass;
@@ -57,6 +58,7 @@ public function testBuild(): void
5758
$containerProphecy->addCompilerPass(Argument::type(AuthenticatorManagerPass::class))->willReturn($containerProphecy->reveal())->shouldBeCalled();
5859
$containerProphecy->addCompilerPass(Argument::type(SerializerMappingLoaderPass::class))->willReturn($containerProphecy->reveal())->shouldBeCalled();
5960
$containerProphecy->addCompilerPass(Argument::type(MutatorPass::class))->willReturn($containerProphecy->reveal())->shouldBeCalled();
61+
$containerProphecy->addCompilerPass(Argument::type(ObjectMapperPass::class))->willReturn($containerProphecy->reveal())->shouldBeCalled();
6062

6163
$bundle = new ApiPlatformBundle();
6264
$bundle->build($containerProphecy->reveal());

0 commit comments

Comments
 (0)