vendor/symfony/symfony/src/Symfony/Component/Form/Extension/Templating/TemplatingRendererEngine.php line 14

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\Component\Form\Extension\Templating;
  11. @trigger_error('The '.TemplatingRendererEngine::class.' class is deprecated since version 4.3 and will be removed in 5.0; use Twig instead.', \E_USER_DEPRECATED);
  12. use Symfony\Component\Form\AbstractRendererEngine;
  13. use Symfony\Component\Form\FormView;
  14. use Symfony\Component\Templating\EngineInterface;
  15. /**
  16.  * @author Bernhard Schussek <bschussek@gmail.com>
  17.  *
  18.  * @deprecated since version 4.3, to be removed in 5.0; use Twig instead.
  19.  */
  20. class TemplatingRendererEngine extends AbstractRendererEngine
  21. {
  22.     private $engine;
  23.     public function __construct(EngineInterface $engine, array $defaultThemes = [])
  24.     {
  25.         parent::__construct($defaultThemes);
  26.         $this->engine $engine;
  27.     }
  28.     /**
  29.      * {@inheritdoc}
  30.      */
  31.     public function renderBlock(FormView $view$resource$blockName, array $variables = [])
  32.     {
  33.         return trim($this->engine->render($resource$variables));
  34.     }
  35.     /**
  36.      * Loads the cache with the resource for a given block name.
  37.      *
  38.      * This implementation tries to load as few blocks as possible, since each block
  39.      * is represented by a template on the file system.
  40.      *
  41.      * @see getResourceForBlock()
  42.      *
  43.      * @param string   $cacheKey  The cache key of the form view
  44.      * @param FormView $view      The form view for finding the applying themes
  45.      * @param string   $blockName The name of the block to load
  46.      *
  47.      * @return bool True if the resource could be loaded, false otherwise
  48.      */
  49.     protected function loadResourceForBlockName($cacheKeyFormView $view$blockName)
  50.     {
  51.         // Recursively try to find the block in the themes assigned to $view,
  52.         // then of its parent form, then of the parent form of the parent and so on.
  53.         // When the root form is reached in this recursion, also the default
  54.         // themes are taken into account.
  55.         // Check each theme whether it contains the searched block
  56.         if (isset($this->themes[$cacheKey])) {
  57.             for ($i = \count($this->themes[$cacheKey]) - 1$i >= 0; --$i) {
  58.                 if ($this->loadResourceFromTheme($cacheKey$blockName$this->themes[$cacheKey][$i])) {
  59.                     return true;
  60.                 }
  61.             }
  62.         }
  63.         // Check the default themes once we reach the root form without success
  64.         if (!$view->parent) {
  65.             if (!isset($this->useDefaultThemes[$cacheKey]) || $this->useDefaultThemes[$cacheKey]) {
  66.                 for ($i = \count($this->defaultThemes) - 1$i >= 0; --$i) {
  67.                     if ($this->loadResourceFromTheme($cacheKey$blockName$this->defaultThemes[$i])) {
  68.                         return true;
  69.                     }
  70.                 }
  71.             }
  72.         }
  73.         // If we did not find anything in the themes of the current view, proceed
  74.         // with the themes of the parent view
  75.         if ($view->parent) {
  76.             $parentCacheKey $view->parent->vars[self::CACHE_KEY_VAR];
  77.             if (!isset($this->resources[$parentCacheKey][$blockName])) {
  78.                 $this->loadResourceForBlockName($parentCacheKey$view->parent$blockName);
  79.             }
  80.             // If a template exists in the parent themes, cache that template
  81.             // for the current theme as well to speed up further accesses
  82.             if ($this->resources[$parentCacheKey][$blockName]) {
  83.                 $this->resources[$cacheKey][$blockName] = $this->resources[$parentCacheKey][$blockName];
  84.                 return true;
  85.             }
  86.         }
  87.         // Cache that we didn't find anything to speed up further accesses
  88.         $this->resources[$cacheKey][$blockName] = false;
  89.         return false;
  90.     }
  91.     /**
  92.      * Tries to load the resource for a block from a theme.
  93.      *
  94.      * @param string $cacheKey  The cache key for storing the resource
  95.      * @param string $blockName The name of the block to load a resource for
  96.      * @param mixed  $theme     The theme to load the block from
  97.      *
  98.      * @return bool True if the resource could be loaded, false otherwise
  99.      */
  100.     protected function loadResourceFromTheme($cacheKey$blockName$theme)
  101.     {
  102.         if ($this->engine->exists($templateName $theme.':'.$blockName.'.html.php')) {
  103.             $this->resources[$cacheKey][$blockName] = $templateName;
  104.             return true;
  105.         }
  106.         return false;
  107.     }
  108. }