2015-08-17 17:00:26 -07:00
< ? php
/*
* This file is part of the Symfony package .
*
* ( c ) Fabien Potencier < fabien @ symfony . com >
*
* For the full copyright and license information , please view the LICENSE
* file that was distributed with this source code .
*/
namespace Symfony\Component\HttpKernel\DependencyInjection ;
2018-11-23 12:29:20 +00:00
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface ;
use Symfony\Component\DependencyInjection\Compiler\ServiceLocatorTagPass ;
2015-08-17 17:00:26 -07:00
use Symfony\Component\DependencyInjection\ContainerBuilder ;
2018-11-23 12:29:20 +00:00
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException ;
2015-08-17 17:00:26 -07:00
use Symfony\Component\DependencyInjection\Reference ;
2018-11-23 12:29:20 +00:00
use Symfony\Component\HttpKernel\Fragment\FragmentRendererInterface ;
2015-08-17 17:00:26 -07:00
/**
* Adds services tagged kernel . fragment_renderer as HTTP content rendering strategies .
*
* @ author Fabien Potencier < fabien @ symfony . com >
*/
class FragmentRendererPass implements CompilerPassInterface
{
private $handlerService ;
private $rendererTag ;
/**
* @ param string $handlerService Service name of the fragment handler in the container
* @ param string $rendererTag Tag name used for fragments
*/
public function __construct ( $handlerService = 'fragment.handler' , $rendererTag = 'kernel.fragment_renderer' )
{
$this -> handlerService = $handlerService ;
$this -> rendererTag = $rendererTag ;
}
public function process ( ContainerBuilder $container )
{
if ( ! $container -> hasDefinition ( $this -> handlerService )) {
return ;
}
$definition = $container -> getDefinition ( $this -> handlerService );
2018-11-23 12:29:20 +00:00
$renderers = array ();
foreach ( $container -> findTaggedServiceIds ( $this -> rendererTag , true ) as $id => $tags ) {
2015-08-17 17:00:26 -07:00
$def = $container -> getDefinition ( $id );
2016-04-20 09:56:34 -07:00
$class = $container -> getParameterBag () -> resolveValue ( $def -> getClass ());
2017-02-02 16:28:38 -08:00
2018-11-23 12:29:20 +00:00
if ( ! $r = $container -> getReflectionClass ( $class )) {
throw new InvalidArgumentException ( sprintf ( 'Class "%s" used for service "%s" cannot be found.' , $class , $id ));
}
if ( ! $r -> isSubclassOf ( FragmentRendererInterface :: class )) {
throw new InvalidArgumentException ( sprintf ( 'Service "%s" must implement interface "%s".' , $id , FragmentRendererInterface :: class ));
2015-08-17 17:00:26 -07:00
}
foreach ( $tags as $tag ) {
2018-11-23 12:29:20 +00:00
$renderers [ $tag [ 'alias' ]] = new Reference ( $id );
2015-08-17 17:00:26 -07:00
}
}
2018-11-23 12:29:20 +00:00
$definition -> replaceArgument ( 0 , ServiceLocatorTagPass :: register ( $container , $renderers ));
2015-08-17 17:00:26 -07:00
}
}