Skip to content

Commit d88d596

Browse files
committed
minor #1793 use a lazy loading service locator for decoders (xabbuh)
This PR was merged into the 2.2-dev branch. Discussion ---------- use a lazy loading service locator for decoders part of #1790 Commits ------- aaa5681 use a lazy loading service locator for decoders
2 parents 73c8fd0 + aaa5681 commit d88d596

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

Decoder/ContainerDecoderProvider.php

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

1212
namespace FOS\RestBundle\Decoder;
1313

14+
use Psr\Container\ContainerInterface as PsrContainerInterface;
1415
use Symfony\Component\DependencyInjection\ContainerInterface;
1516

1617
/**
@@ -26,11 +27,15 @@ class ContainerDecoderProvider implements DecoderProviderInterface
2627
/**
2728
* Constructor.
2829
*
29-
* @param ContainerInterface $container The container from which the actual decoders are retrieved
30-
* @param array $decoders List of key (format) value (service ids) of decoders
30+
* @param PsrContainerInterface $container The container from which the actual decoders are retrieved
31+
* @param array $decoders List of key (format) value (service ids) of decoders
3132
*/
32-
public function __construct(ContainerInterface $container, array $decoders)
33+
public function __construct($container, array $decoders)
3334
{
35+
if (!$container instanceof PsrContainerInterface && !$container instanceof ContainerInterface) {
36+
throw new \InvalidArgumentException(sprintf('The container must be an instance of %s or %s (%s given).', PsrContainerInterface::class, ContainerInterface::class, is_object($container) ? get_class($container) : gettype($container)));
37+
}
38+
3439
$this->container = $container;
3540
$this->decoders = $decoders;
3641
}

DependencyInjection/FOSRestExtension.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Component\Config\FileLocator;
1515
use Symfony\Component\DependencyInjection\ChildDefinition;
16+
use Symfony\Component\DependencyInjection\Compiler\ServiceLocatorTagPass;
1617
use Symfony\Component\DependencyInjection\ContainerBuilder;
1718
use Symfony\Component\DependencyInjection\DefinitionDecorator;
1819
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
@@ -151,6 +152,17 @@ private function loadBodyListener(array $config, XmlFileLoader $loader, Containe
151152

152153
$container->getDefinition('fos_rest.decoder_provider')->replaceArgument(1, $config['body_listener']['decoders']);
153154

155+
if (class_exists(ServiceLocatorTagPass::class)) {
156+
$decoderServicesMap = array();
157+
158+
foreach ($config['body_listener']['decoders'] as $id) {
159+
$decoderServicesMap[$id] = new Reference($id);
160+
}
161+
162+
$decodersServiceLocator = ServiceLocatorTagPass::register($container, $decoderServicesMap);
163+
$container->getDefinition('fos_rest.decoder_provider')->replaceArgument(0, $decodersServiceLocator);
164+
}
165+
154166
$arrayNormalizer = $config['body_listener']['array_normalizer'];
155167

156168
if (null !== $arrayNormalizer['service']) {

0 commit comments

Comments
 (0)