-
Notifications
You must be signed in to change notification settings - Fork 700
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bug #1873 manually decorate the core JMS handler registry (xabbuh)
This PR was merged into the 2.3-dev branch. Discussion ---------- manually decorate the core JMS handler registry This works around the fact that custom handlers are processed in JMSSerializerBundle after Symfony's DecoratorServicePass has been executed (which means that we cannot use proper service decoration). The old code still used to work on Symfony 2.x applications where FOSRestBundle was registered after JMSSerializerBundle. In those cases, custom handlers were processed before the handler registry was replaced by the compiler pass coming from FOSRestBundle. More recent Symfony applications have not been affected by this bug as the compiler pass priority would have ensured the correct order in which the passes would have been executed. Commits ------- 17c8de8 manually decorate the core JMS handler registry
- Loading branch information
Showing
3 changed files
with
57 additions
and
18 deletions.
There are no files selected for viewing
52 changes: 52 additions & 0 deletions
52
DependencyInjection/Compiler/HandlerRegistryDecorationPass.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the FOSRestBundle package. | ||
* | ||
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace FOS\RestBundle\DependencyInjection\Compiler; | ||
|
||
use FOS\RestBundle\Serializer\JMSHandlerRegistry; | ||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
|
||
/** | ||
* Decorates the handler registry from JMSSerializerBundle. | ||
* | ||
* The logic is borrowed from the core Symfony DecoratorServicePass, but is implemented here to respect the fact that | ||
* custom handlers are registered in JMSSerializerBundle in a compiler pass that is executed after decorated services | ||
* have been resolved. | ||
* | ||
* @author Christian Flothmann <[email protected]> | ||
*/ | ||
class HandlerRegistryDecorationPass implements CompilerPassInterface | ||
{ | ||
public function process(ContainerBuilder $container) | ||
{ | ||
if (!$container->has('fos_rest.serializer.jms_handler_registry')) { | ||
return; | ||
} | ||
|
||
$jmsHandlerRegistry = $container->findDefinition('fos_rest.serializer.jms_handler_registry'); | ||
$public = $jmsHandlerRegistry->isPublic(); | ||
$jmsHandlerRegistry->setPublic(false); | ||
$container->setDefinition('fos_rest.serializer.jms_handler_registry.inner', $jmsHandlerRegistry); | ||
|
||
$fosRestHandlerRegistry = $container->register('jms_serializer.handler_registry', JMSHandlerRegistry::class) | ||
->setPublic($public) | ||
->addArgument(new Reference('fos_rest.serializer.jms_handler_registry.inner')); | ||
|
||
// remap existing aliases (they have already been replaced with the actual definition by Symfony's ReplaceAliasByActualDefinitonPass) | ||
foreach ($container->getDefinitions() as $id => $definition) { | ||
if ('fos_rest.serializer.jms_handler_registry.inner' !== $id && $definition === $jmsHandlerRegistry) { | ||
$container->setDefinition($id, $fosRestHandlerRegistry); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters