annotate vendor/symfony/http-kernel/DependencyInjection/FragmentRendererPass.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 129ea1e6d783
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 /*
Chris@0 4 * This file is part of the Symfony package.
Chris@0 5 *
Chris@0 6 * (c) Fabien Potencier <fabien@symfony.com>
Chris@0 7 *
Chris@0 8 * For the full copyright and license information, please view the LICENSE
Chris@0 9 * file that was distributed with this source code.
Chris@0 10 */
Chris@0 11
Chris@0 12 namespace Symfony\Component\HttpKernel\DependencyInjection;
Chris@0 13
Chris@17 14 use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
Chris@14 15 use Symfony\Component\DependencyInjection\Compiler\ServiceLocatorTagPass;
Chris@0 16 use Symfony\Component\DependencyInjection\ContainerBuilder;
Chris@0 17 use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
Chris@14 18 use Symfony\Component\DependencyInjection\Reference;
Chris@14 19 use Symfony\Component\HttpKernel\Fragment\FragmentRendererInterface;
Chris@0 20
Chris@0 21 /**
Chris@0 22 * Adds services tagged kernel.fragment_renderer as HTTP content rendering strategies.
Chris@0 23 *
Chris@0 24 * @author Fabien Potencier <fabien@symfony.com>
Chris@0 25 */
Chris@0 26 class FragmentRendererPass implements CompilerPassInterface
Chris@0 27 {
Chris@0 28 private $handlerService;
Chris@0 29 private $rendererTag;
Chris@0 30
Chris@0 31 /**
Chris@0 32 * @param string $handlerService Service name of the fragment handler in the container
Chris@0 33 * @param string $rendererTag Tag name used for fragments
Chris@0 34 */
Chris@0 35 public function __construct($handlerService = 'fragment.handler', $rendererTag = 'kernel.fragment_renderer')
Chris@0 36 {
Chris@0 37 $this->handlerService = $handlerService;
Chris@0 38 $this->rendererTag = $rendererTag;
Chris@0 39 }
Chris@0 40
Chris@0 41 public function process(ContainerBuilder $container)
Chris@0 42 {
Chris@0 43 if (!$container->hasDefinition($this->handlerService)) {
Chris@0 44 return;
Chris@0 45 }
Chris@0 46
Chris@0 47 $definition = $container->getDefinition($this->handlerService);
Chris@17 48 $renderers = [];
Chris@14 49 foreach ($container->findTaggedServiceIds($this->rendererTag, true) as $id => $tags) {
Chris@0 50 $def = $container->getDefinition($id);
Chris@14 51 $class = $container->getParameterBag()->resolveValue($def->getClass());
Chris@14 52
Chris@14 53 if (!$r = $container->getReflectionClass($class)) {
Chris@14 54 throw new InvalidArgumentException(sprintf('Class "%s" used for service "%s" cannot be found.', $class, $id));
Chris@0 55 }
Chris@14 56 if (!$r->isSubclassOf(FragmentRendererInterface::class)) {
Chris@14 57 throw new InvalidArgumentException(sprintf('Service "%s" must implement interface "%s".', $id, FragmentRendererInterface::class));
Chris@0 58 }
Chris@0 59
Chris@0 60 foreach ($tags as $tag) {
Chris@14 61 $renderers[$tag['alias']] = new Reference($id);
Chris@0 62 }
Chris@0 63 }
Chris@14 64
Chris@14 65 $definition->replaceArgument(0, ServiceLocatorTagPass::register($container, $renderers));
Chris@0 66 }
Chris@0 67 }