LazyLoadingFragmentHandler.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\HttpKernel\DependencyInjection;
  11. use Psr\Container\ContainerInterface;
  12. use Symfony\Component\HttpFoundation\RequestStack;
  13. use Symfony\Component\HttpKernel\Fragment\FragmentHandler;
  14. /**
  15. * Lazily loads fragment renderers from the dependency injection container.
  16. *
  17. * @author Fabien Potencier <fabien@symfony.com>
  18. */
  19. class LazyLoadingFragmentHandler extends FragmentHandler
  20. {
  21. private $container;
  22. private $initialized = [];
  23. public function __construct(ContainerInterface $container, RequestStack $requestStack, bool $debug = false)
  24. {
  25. $this->container = $container;
  26. parent::__construct($requestStack, [], $debug);
  27. }
  28. /**
  29. * {@inheritdoc}
  30. */
  31. public function render($uri, string $renderer = 'inline', array $options = [])
  32. {
  33. if (!isset($this->initialized[$renderer]) && $this->container->has($renderer)) {
  34. $this->addRenderer($this->container->get($renderer));
  35. $this->initialized[$renderer] = true;
  36. }
  37. return parent::render($uri, $renderer, $options);
  38. }
  39. }