1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the FOSRestBundle package. |
5
|
|
|
* |
6
|
|
|
* (c) FriendsOfSymfony <http://friendsofsymfony.github.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 FOS\RestBundle\DependencyInjection\Compiler; |
13
|
|
|
|
14
|
|
|
use FOS\RestBundle\Serializer\JMSHandlerRegistry; |
15
|
|
|
use FOS\RestBundle\Serializer\JMSHandlerRegistryV2; |
16
|
|
|
use JMS\Serializer\Visitor\SerializationVisitorInterface; |
17
|
|
|
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
18
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
19
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Decorates the handler registry from JMSSerializerBundle. |
23
|
|
|
* |
24
|
|
|
* The logic is borrowed from the core Symfony DecoratorServicePass, but is implemented here to respect the fact that |
25
|
|
|
* custom handlers are registered in JMSSerializerBundle in a compiler pass that is executed after decorated services |
26
|
|
|
* have been resolved. |
27
|
|
|
* |
28
|
|
|
* @author Christian Flothmann <[email protected]> |
29
|
|
|
* |
30
|
|
|
* @internal |
31
|
|
|
*/ |
32
|
|
|
class HandlerRegistryDecorationPass implements CompilerPassInterface |
33
|
|
|
{ |
34
|
13 |
|
public function process(ContainerBuilder $container): void |
35
|
|
|
{ |
36
|
13 |
|
if (!$container->has('fos_rest.serializer.jms_handler_registry')) { |
37
|
9 |
|
return; |
38
|
|
|
} |
39
|
|
|
|
40
|
4 |
|
$jmsHandlerRegistry = $container->findDefinition('fos_rest.serializer.jms_handler_registry'); |
41
|
4 |
|
$public = $jmsHandlerRegistry->isPublic(); |
42
|
4 |
|
$jmsHandlerRegistry->setPublic(false); |
43
|
4 |
|
$container->setDefinition('fos_rest.serializer.jms_handler_registry.inner', $jmsHandlerRegistry); |
44
|
|
|
|
45
|
4 |
|
$fosRestHandlerRegistry = $container->register('jms_serializer.handler_registry', interface_exists(SerializationVisitorInterface::class) ? JMSHandlerRegistryV2::class : JMSHandlerRegistry::class) |
46
|
4 |
|
->setPublic($public) |
47
|
4 |
|
->addArgument(new Reference('fos_rest.serializer.jms_handler_registry.inner')); |
48
|
|
|
|
49
|
|
|
// remap existing aliases (they have already been replaced with the actual definition by Symfony's ReplaceAliasByActualDefinitionPass) |
50
|
4 |
|
foreach ($container->getDefinitions() as $id => $definition) { |
51
|
4 |
|
if ('fos_rest.serializer.jms_handler_registry.inner' !== $id && $definition === $jmsHandlerRegistry) { |
|
|
|
|
52
|
4 |
|
$container->setDefinition($id, $fosRestHandlerRegistry); |
53
|
|
|
} |
54
|
|
|
} |
55
|
4 |
|
} |
56
|
|
|
} |
57
|
|
|
|