Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace Symfony\Component\HttpKernel\DependencyInjection; Chris@0: Chris@17: use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; Chris@14: use Symfony\Component\DependencyInjection\Compiler\ServiceLocatorTagPass; Chris@0: use Symfony\Component\DependencyInjection\ContainerBuilder; Chris@0: use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException; Chris@14: use Symfony\Component\DependencyInjection\Reference; Chris@14: use Symfony\Component\HttpKernel\Fragment\FragmentRendererInterface; Chris@0: Chris@0: /** Chris@0: * Adds services tagged kernel.fragment_renderer as HTTP content rendering strategies. Chris@0: * Chris@0: * @author Fabien Potencier Chris@0: */ Chris@0: class FragmentRendererPass implements CompilerPassInterface Chris@0: { Chris@0: private $handlerService; Chris@0: private $rendererTag; Chris@0: Chris@0: /** Chris@0: * @param string $handlerService Service name of the fragment handler in the container Chris@0: * @param string $rendererTag Tag name used for fragments Chris@0: */ Chris@0: public function __construct($handlerService = 'fragment.handler', $rendererTag = 'kernel.fragment_renderer') Chris@0: { Chris@0: $this->handlerService = $handlerService; Chris@0: $this->rendererTag = $rendererTag; Chris@0: } Chris@0: Chris@0: public function process(ContainerBuilder $container) Chris@0: { Chris@0: if (!$container->hasDefinition($this->handlerService)) { Chris@0: return; Chris@0: } Chris@0: Chris@0: $definition = $container->getDefinition($this->handlerService); Chris@17: $renderers = []; Chris@14: foreach ($container->findTaggedServiceIds($this->rendererTag, true) as $id => $tags) { Chris@0: $def = $container->getDefinition($id); Chris@14: $class = $container->getParameterBag()->resolveValue($def->getClass()); Chris@14: Chris@14: if (!$r = $container->getReflectionClass($class)) { Chris@14: throw new InvalidArgumentException(sprintf('Class "%s" used for service "%s" cannot be found.', $class, $id)); Chris@0: } Chris@14: if (!$r->isSubclassOf(FragmentRendererInterface::class)) { Chris@14: throw new InvalidArgumentException(sprintf('Service "%s" must implement interface "%s".', $id, FragmentRendererInterface::class)); Chris@0: } Chris@0: Chris@0: foreach ($tags as $tag) { Chris@14: $renderers[$tag['alias']] = new Reference($id); Chris@0: } Chris@0: } Chris@14: Chris@14: $definition->replaceArgument(0, ServiceLocatorTagPass::register($container, $renderers)); Chris@0: } Chris@0: }