|
| 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 Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
| 16 | +use Symfony\Component\DependencyInjection\ContainerBuilder; |
| 17 | +use Symfony\Component\DependencyInjection\Reference; |
| 18 | + |
| 19 | +/** |
| 20 | + * Decorates the handler registry from JMSSerializerBundle. |
| 21 | + * |
| 22 | + * The logic is borrowed from the core Symfony DecoratorServicePass, but is implemented here to respect the fact that |
| 23 | + * custom handlers are registered in JMSSerializerBundle in a compiler pass that is executed after decorated services |
| 24 | + * have been resolved. |
| 25 | + * |
| 26 | + * @author Christian Flothmann <[email protected]> |
| 27 | + */ |
| 28 | +class HandlerRegistryDecorationPass implements CompilerPassInterface |
| 29 | +{ |
| 30 | + public function process(ContainerBuilder $container) |
| 31 | + { |
| 32 | + if (!$container->has('fos_rest.serializer.jms_handler_registry')) { |
| 33 | + return; |
| 34 | + } |
| 35 | + |
| 36 | + $jmsHandlerRegistry = $container->findDefinition('fos_rest.serializer.jms_handler_registry'); |
| 37 | + $public = $jmsHandlerRegistry->isPublic(); |
| 38 | + $jmsHandlerRegistry->setPublic(false); |
| 39 | + $container->setDefinition('fos_rest.serializer.jms_handler_registry.inner', $jmsHandlerRegistry); |
| 40 | + |
| 41 | + $fosRestHandlerRegistry = $container->register('jms_serializer.handler_registry', JMSHandlerRegistry::class) |
| 42 | + ->setPublic($public) |
| 43 | + ->addArgument(new Reference('fos_rest.serializer.jms_handler_registry.inner')); |
| 44 | + |
| 45 | + // remap existing aliases (they have already been replaced with the actual definition by Symfony's ReplaceAliasByActualDefinitonPass) |
| 46 | + foreach ($container->getDefinitions() as $id => $definition) { |
| 47 | + if ('fos_rest.serializer.jms_handler_registry.inner' !== $id && $definition === $jmsHandlerRegistry) { |
| 48 | + $container->setDefinition($id, $fosRestHandlerRegistry); |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | +} |
0 commit comments