vendor/symfony/twig-bridge/Form/TwigRendererEngine.php line 63

  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\Bridge\Twig\Form;
  11. use Symfony\Component\Form\AbstractRendererEngine;
  12. use Symfony\Component\Form\FormView;
  13. use Twig\Environment;
  14. use Twig\Template;
  15. /**
  16.  * @author Bernhard Schussek <bschussek@gmail.com>
  17.  */
  18. class TwigRendererEngine extends AbstractRendererEngine
  19. {
  20.     private Environment $environment;
  21.     private Template $template;
  22.     public function __construct(array $defaultThemesEnvironment $environment)
  23.     {
  24.         parent::__construct($defaultThemes);
  25.         $this->environment $environment;
  26.     }
  27.     public function renderBlock(FormView $viewmixed $resourcestring $blockName, array $variables = []): string
  28.     {
  29.         $cacheKey $view->vars[self::CACHE_KEY_VAR];
  30.         $context $this->environment->mergeGlobals($variables);
  31.         ob_start();
  32.         // By contract,This method can only be called after getting the resource
  33.         // (which is passed to the method). Getting a resource for the first time
  34.         // (with an empty cache) is guaranteed to invoke loadResourcesFromTheme(),
  35.         // where the property $template is initialized.
  36.         // We do not call renderBlock here to avoid too many nested level calls
  37.         // (XDebug limits the level to 100 by default)
  38.         $this->template->displayBlock($blockName$context$this->resources[$cacheKey]);
  39.         return ob_get_clean();
  40.     }
  41.     /**
  42.      * Loads the cache with the resource for a given block name.
  43.      *
  44.      * This implementation eagerly loads all blocks of the themes assigned to the given view
  45.      * and all of its ancestors views. This is necessary, because Twig receives the
  46.      * list of blocks later. At that point, all blocks must already be loaded, for the
  47.      * case that the function "block()" is used in the Twig template.
  48.      *
  49.      * @see getResourceForBlock()
  50.      */
  51.     protected function loadResourceForBlockName(string $cacheKeyFormView $viewstring $blockName): bool
  52.     {
  53.         // The caller guarantees that $this->resources[$cacheKey][$block] is
  54.         // not set, but it doesn't have to check whether $this->resources[$cacheKey]
  55.         // is set. If $this->resources[$cacheKey] is set, all themes for this
  56.         // $cacheKey are already loaded (due to the eager population, see doc comment).
  57.         if (isset($this->resources[$cacheKey])) {
  58.             // As said in the previous, the caller guarantees that
  59.             // $this->resources[$cacheKey][$block] is not set. Since the themes are
  60.             // already loaded, it can only be a non-existing block.
  61.             $this->resources[$cacheKey][$blockName] = false;
  62.             return false;
  63.         }
  64.         // Recursively try to find the block in the themes assigned to $view,
  65.         // then of its parent view, then of the parent view of the parent and so on.
  66.         // When the root view is reached in this recursion, also the default
  67.         // themes are taken into account.
  68.         // Check each theme whether it contains the searched block
  69.         if (isset($this->themes[$cacheKey])) {
  70.             for ($i \count($this->themes[$cacheKey]) - 1$i >= 0; --$i) {
  71.                 $this->loadResourcesFromTheme($cacheKey$this->themes[$cacheKey][$i]);
  72.                 // CONTINUE LOADING (see doc comment)
  73.             }
  74.         }
  75.         // Check the default themes once we reach the root view without success
  76.         if (!$view->parent) {
  77.             if (!isset($this->useDefaultThemes[$cacheKey]) || $this->useDefaultThemes[$cacheKey]) {
  78.                 for ($i \count($this->defaultThemes) - 1$i >= 0; --$i) {
  79.                     $this->loadResourcesFromTheme($cacheKey$this->defaultThemes[$i]);
  80.                     // CONTINUE LOADING (see doc comment)
  81.                 }
  82.             }
  83.         }
  84.         // Proceed with the themes of the parent view
  85.         if ($view->parent) {
  86.             $parentCacheKey $view->parent->vars[self::CACHE_KEY_VAR];
  87.             if (!isset($this->resources[$parentCacheKey])) {
  88.                 $this->loadResourceForBlockName($parentCacheKey$view->parent$blockName);
  89.             }
  90.             // EAGER CACHE POPULATION (see doc comment)
  91.             foreach ($this->resources[$parentCacheKey] as $nestedBlockName => $resource) {
  92.                 if (!isset($this->resources[$cacheKey][$nestedBlockName])) {
  93.                     $this->resources[$cacheKey][$nestedBlockName] = $resource;
  94.                 }
  95.             }
  96.         }
  97.         // Even though we loaded the themes, it can happen that none of them
  98.         // contains the searched block
  99.         if (!isset($this->resources[$cacheKey][$blockName])) {
  100.             // Cache that we didn't find anything to speed up further accesses
  101.             $this->resources[$cacheKey][$blockName] = false;
  102.         }
  103.         return false !== $this->resources[$cacheKey][$blockName];
  104.     }
  105.     /**
  106.      * Loads the resources for all blocks in a theme.
  107.      *
  108.      * @param mixed $theme The theme to load the block from. This parameter
  109.      *                     is passed by reference, because it might be necessary
  110.      *                     to initialize the theme first. Any changes made to
  111.      *                     this variable will be kept and be available upon
  112.      *                     further calls to this method using the same theme.
  113.      */
  114.     protected function loadResourcesFromTheme(string $cacheKeymixed &$theme)
  115.     {
  116.         if (!$theme instanceof Template) {
  117.             $theme $this->environment->load($theme)->unwrap();
  118.         }
  119.         // Store the first Template instance that we find so that
  120.         // we can call displayBlock() later on. It doesn't matter *which*
  121.         // template we use for that, since we pass the used blocks manually
  122.         // anyway.
  123.         $this->template ??= $theme;
  124.         // Use a separate variable for the inheritance traversal, because
  125.         // theme is a reference and we don't want to change it.
  126.         $currentTheme $theme;
  127.         $context $this->environment->mergeGlobals([]);
  128.         // The do loop takes care of template inheritance.
  129.         // Add blocks from all templates in the inheritance tree, but avoid
  130.         // overriding blocks already set.
  131.         do {
  132.             foreach ($currentTheme->getBlocks() as $block => $blockData) {
  133.                 if (!isset($this->resources[$cacheKey][$block])) {
  134.                     // The resource given back is the key to the bucket that
  135.                     // contains this block.
  136.                     $this->resources[$cacheKey][$block] = $blockData;
  137.                 }
  138.             }
  139.         } while (false !== $currentTheme $currentTheme->getParent($context));
  140.     }
  141. }