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