vendor/se7enxweb/legacy-bridge/mvc/Templating/Twig/Environment.php line 33

Open in your IDE?
  1. <?php
  2. /**
  3. * @copyright Copyright (C) eZ Systems AS. All rights reserved.
  4. * @license For full copyright and license information view LICENSE file distributed with this source code.
  5. */
  6. namespace eZ\Publish\Core\MVC\Legacy\Templating\Twig;
  7. use eZ\Publish\Core\MVC\Legacy\Templating\LegacyEngine;
  8. use Twig\Environment as BaseEnvironment;
  9. use Twig\Error\LoaderError;
  10. use Twig\Template as BaseTemplate;
  11. class Environment extends BaseEnvironment
  12. {
  13. /**
  14. * @var \eZ\Publish\Core\MVC\Legacy\Templating\LegacyEngine
  15. */
  16. private $legacyEngine;
  17. /**
  18. * Template objects indexed by their identifier.
  19. *
  20. * @var \eZ\Publish\Core\MVC\Legacy\Templating\Twig\Template[]
  21. */
  22. protected $legacyTemplatesCache = [];
  23. public function setEzLegacyEngine(LegacyEngine $legacyEngine)
  24. {
  25. $this->legacyEngine = $legacyEngine;
  26. }
  27. public function loadTemplate(string $cls, string $name, ?int $index = null): BaseTemplate
  28. {
  29. // If legacy engine supports given template, delegate it.
  30. if (\is_string($name) && isset($this->legacyTemplatesCache[$name])) {
  31. return $this->legacyTemplatesCache[$name];
  32. }
  33. if (\is_string($name) && $this->legacyEngine !== null && $this->legacyEngine->supports($name)) {
  34. if (!$this->legacyEngine->exists($name)) {
  35. throw new LoaderError("Unable to find the template \"$name\"");
  36. }
  37. $this->legacyTemplatesCache[$name] = new Template($name, $this, $this->legacyEngine);
  38. return $this->legacyTemplatesCache[$name];
  39. }
  40. return parent::loadTemplate($cls, $name, $index);
  41. }
  42. }