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@0: use Symfony\Component\DependencyInjection\ContainerBuilder; Chris@0: use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; Chris@0: use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException; 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@0: foreach ($container->findTaggedServiceIds($this->rendererTag) as $id => $tags) { Chris@0: $def = $container->getDefinition($id); Chris@0: if (!$def->isPublic()) { Chris@0: throw new InvalidArgumentException(sprintf('The service "%s" must be public as fragment renderer are lazy-loaded.', $id)); Chris@0: } Chris@0: Chris@0: if ($def->isAbstract()) { Chris@0: throw new InvalidArgumentException(sprintf('The service "%s" must not be abstract as fragment renderer are lazy-loaded.', $id)); Chris@0: } Chris@0: Chris@0: $class = $container->getParameterBag()->resolveValue($def->getClass()); Chris@0: $interface = 'Symfony\Component\HttpKernel\Fragment\FragmentRendererInterface'; Chris@0: Chris@0: if (!is_subclass_of($class, $interface)) { Chris@0: if (!class_exists($class, false)) { Chris@0: throw new InvalidArgumentException(sprintf('Class "%s" used for service "%s" cannot be found.', $class, $id)); Chris@0: } Chris@0: Chris@0: throw new InvalidArgumentException(sprintf('Service "%s" must implement interface "%s".', $id, $interface)); Chris@0: } Chris@0: Chris@0: foreach ($tags as $tag) { Chris@0: $definition->addMethodCall('addRendererService', array($tag['alias'], $id)); Chris@0: } Chris@0: } Chris@0: } Chris@0: }