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 Psr\Container\ContainerInterface ;
2015-08-17 17:00:26 -07:00
use Symfony\Component\HttpFoundation\RequestStack ;
use Symfony\Component\HttpKernel\Fragment\FragmentHandler ;
/**
* Lazily loads fragment renderers from the dependency injection container .
*
* @ author Fabien Potencier < fabien @ symfony . com >
*/
class LazyLoadingFragmentHandler extends FragmentHandler
{
private $container ;
2018-11-23 12:29:20 +00:00
/**
* @ deprecated since version 3.3 , to be removed in 4.0
*/
2015-08-17 17:00:26 -07:00
private $rendererIds = array ();
2018-11-23 12:29:20 +00:00
private $initialized = array ();
2015-08-17 17:00:26 -07:00
2016-04-20 09:56:34 -07:00
/**
* @ param ContainerInterface $container A container
* @ param RequestStack $requestStack The Request stack that controls the lifecycle of requests
* @ param bool $debug Whether the debug mode is enabled or not
*/
2018-11-23 12:29:20 +00:00
public function __construct ( ContainerInterface $container , RequestStack $requestStack , $debug = false )
2015-08-17 17:00:26 -07:00
{
$this -> container = $container ;
2016-04-20 09:56:34 -07:00
parent :: __construct ( $requestStack , array (), $debug );
2015-08-17 17:00:26 -07:00
}
/**
* Adds a service as a fragment renderer .
*
2017-02-02 16:28:38 -08:00
* @ param string $name The service name
2015-08-17 17:00:26 -07:00
* @ param string $renderer The render service id
2018-11-23 12:29:20 +00:00
*
* @ deprecated since version 3.3 , to be removed in 4.0
2015-08-17 17:00:26 -07:00
*/
public function addRendererService ( $name , $renderer )
{
2018-11-23 12:29:20 +00:00
@ trigger_error ( sprintf ( 'The %s() method is deprecated since Symfony 3.3 and will be removed in 4.0.' , __METHOD__ ), E_USER_DEPRECATED );
2015-08-17 17:00:26 -07:00
$this -> rendererIds [ $name ] = $renderer ;
}
/**
* { @ inheritdoc }
*/
public function render ( $uri , $renderer = 'inline' , array $options = array ())
{
2018-11-23 12:29:20 +00:00
// BC 3.x, to be removed in 4.0
2015-08-17 17:00:26 -07:00
if ( isset ( $this -> rendererIds [ $renderer ])) {
$this -> addRenderer ( $this -> container -> get ( $this -> rendererIds [ $renderer ]));
unset ( $this -> rendererIds [ $renderer ]);
2018-11-23 12:29:20 +00:00
return parent :: render ( $uri , $renderer , $options );
}
if ( ! isset ( $this -> initialized [ $renderer ]) && $this -> container -> has ( $renderer )) {
$this -> addRenderer ( $this -> container -> get ( $renderer ));
$this -> initialized [ $renderer ] = true ;
2015-08-17 17:00:26 -07:00
}
return parent :: render ( $uri , $renderer , $options );
}
}