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

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