]> git.immae.eu Git - github/wallabag/wallabag.git/blob - vendor/symfony/form/Symfony/Component/Form/Extension/Templating/TemplatingRendererEngine.php
gitignore vendor
[github/wallabag/wallabag.git] / vendor / symfony / form / Symfony / Component / Form / Extension / Templating / TemplatingRendererEngine.php
1 <?php
2
3 /*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <fabien@symfony.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace Symfony\Component\Form\Extension\Templating;
13
14 use Symfony\Component\Form\AbstractRendererEngine;
15 use Symfony\Component\Form\FormView;
16 use Symfony\Component\Templating\EngineInterface;
17
18 /**
19 * @author Bernhard Schussek <bschussek@gmail.com>
20 */
21 class TemplatingRendererEngine extends AbstractRendererEngine
22 {
23 /**
24 * @var EngineInterface
25 */
26 private $engine;
27
28 public function __construct(EngineInterface $engine, array $defaultThemes = array())
29 {
30 parent::__construct($defaultThemes);
31
32 $this->engine = $engine;
33 }
34
35 /**
36 * {@inheritdoc}
37 */
38 public function renderBlock(FormView $view, $resource, $blockName, array $variables = array())
39 {
40 return trim($this->engine->render($resource, $variables));
41 }
42
43 /**
44 * Loads the cache with the resource for a given block name.
45 *
46 * This implementation tries to load as few blocks as possible, since each block
47 * is represented by a template on the file system.
48 *
49 * @see getResourceForBlock()
50 *
51 * @param string $cacheKey The cache key of the form view.
52 * @param FormView $view The form view for finding the applying themes.
53 * @param string $blockName The name of the block to load.
54 *
55 * @return Boolean True if the resource could be loaded, false otherwise.
56 */
57 protected function loadResourceForBlockName($cacheKey, FormView $view, $blockName)
58 {
59 // Recursively try to find the block in the themes assigned to $view,
60 // then of its parent form, then of the parent form of the parent and so on.
61 // When the root form is reached in this recursion, also the default
62 // themes are taken into account.
63
64 // Check each theme whether it contains the searched block
65 if (isset($this->themes[$cacheKey])) {
66 for ($i = count($this->themes[$cacheKey]) - 1; $i >= 0; --$i) {
67 if ($this->loadResourceFromTheme($cacheKey, $blockName, $this->themes[$cacheKey][$i])) {
68 return true;
69 }
70 }
71 }
72
73 // Check the default themes once we reach the root form without success
74 if (!$view->parent) {
75 for ($i = count($this->defaultThemes) - 1; $i >= 0; --$i) {
76 if ($this->loadResourceFromTheme($cacheKey, $blockName, $this->defaultThemes[$i])) {
77 return true;
78 }
79 }
80 }
81
82 // If we did not find anything in the themes of the current view, proceed
83 // with the themes of the parent view
84 if ($view->parent) {
85 $parentCacheKey = $view->parent->vars[self::CACHE_KEY_VAR];
86
87 if (!isset($this->resources[$parentCacheKey][$blockName])) {
88 $this->loadResourceForBlockName($parentCacheKey, $view->parent, $blockName);
89 }
90
91 // If a template exists in the parent themes, cache that template
92 // for the current theme as well to speed up further accesses
93 if ($this->resources[$parentCacheKey][$blockName]) {
94 $this->resources[$cacheKey][$blockName] = $this->resources[$parentCacheKey][$blockName];
95
96 return true;
97 }
98 }
99
100 // Cache that we didn't find anything to speed up further accesses
101 $this->resources[$cacheKey][$blockName] = false;
102
103 return false;
104 }
105
106 /**
107 * Tries to load the resource for a block from a theme.
108 *
109 * @param string $cacheKey The cache key for storing the resource.
110 * @param string $blockName The name of the block to load a resource for.
111 * @param mixed $theme The theme to load the block from.
112 *
113 * @return Boolean True if the resource could be loaded, false otherwise.
114 */
115 protected function loadResourceFromTheme($cacheKey, $blockName, $theme)
116 {
117 if ($this->engine->exists($templateName = $theme.':'.$blockName.'.html.php')) {
118 $this->resources[$cacheKey][$blockName] = $templateName;
119
120 return true;
121 }
122
123 return false;
124 }
125 }