]> git.immae.eu Git - github/wallabag/wallabag.git/blame - vendor/symfony/form/Symfony/Component/Form/AbstractRendererEngine.php
gitignore vendor
[github/wallabag/wallabag.git] / vendor / symfony / form / Symfony / Component / Form / AbstractRendererEngine.php
CommitLineData
4f5b44bd
NL
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
12namespace Symfony\Component\Form;
13
14/**
15 * Default implementation of {@link FormRendererEngineInterface}.
16 *
17 * @author Bernhard Schussek <bschussek@gmail.com>
18 */
19abstract class AbstractRendererEngine implements FormRendererEngineInterface
20{
21 /**
22 * The variable in {@link FormView} used as cache key.
23 */
24 const CACHE_KEY_VAR = 'cache_key';
25
26 /**
27 * @var array
28 */
29 protected $defaultThemes;
30
31 /**
32 * @var array
33 */
34 protected $themes = array();
35
36 /**
37 * @var array
38 */
39 protected $resources = array();
40
41 /**
42 * @var array
43 */
44 private $resourceHierarchyLevels = array();
45
46 /**
47 * Creates a new renderer engine.
48 *
49 * @param array $defaultThemes The default themes. The type of these
50 * themes is open to the implementation.
51 */
52 public function __construct(array $defaultThemes = array())
53 {
54 $this->defaultThemes = $defaultThemes;
55 }
56
57 /**
58 * {@inheritdoc}
59 */
60 public function setTheme(FormView $view, $themes)
61 {
62 $cacheKey = $view->vars[self::CACHE_KEY_VAR];
63
64 // Do not cast, as casting turns objects into arrays of properties
65 $this->themes[$cacheKey] = is_array($themes) ? $themes : array($themes);
66
67 // Unset instead of resetting to an empty array, in order to allow
68 // implementations (like TwigRendererEngine) to check whether $cacheKey
69 // is set at all.
70 unset($this->resources[$cacheKey]);
71 unset($this->resourceHierarchyLevels[$cacheKey]);
72 }
73
74 /**
75 * {@inheritdoc}
76 */
77 public function getResourceForBlockName(FormView $view, $blockName)
78 {
79 $cacheKey = $view->vars[self::CACHE_KEY_VAR];
80
81 if (!isset($this->resources[$cacheKey][$blockName])) {
82 $this->loadResourceForBlockName($cacheKey, $view, $blockName);
83 }
84
85 return $this->resources[$cacheKey][$blockName];
86 }
87
88 /**
89 * {@inheritdoc}
90 */
91 public function getResourceForBlockNameHierarchy(FormView $view, array $blockNameHierarchy, $hierarchyLevel)
92 {
93 $cacheKey = $view->vars[self::CACHE_KEY_VAR];
94 $blockName = $blockNameHierarchy[$hierarchyLevel];
95
96 if (!isset($this->resources[$cacheKey][$blockName])) {
97 $this->loadResourceForBlockNameHierarchy($cacheKey, $view, $blockNameHierarchy, $hierarchyLevel);
98 }
99
100 return $this->resources[$cacheKey][$blockName];
101 }
102
103 /**
104 * {@inheritdoc}
105 */
106 public function getResourceHierarchyLevel(FormView $view, array $blockNameHierarchy, $hierarchyLevel)
107 {
108 $cacheKey = $view->vars[self::CACHE_KEY_VAR];
109 $blockName = $blockNameHierarchy[$hierarchyLevel];
110
111 if (!isset($this->resources[$cacheKey][$blockName])) {
112 $this->loadResourceForBlockNameHierarchy($cacheKey, $view, $blockNameHierarchy, $hierarchyLevel);
113 }
114
115 // If $block was previously rendered loaded with loadTemplateForBlock(), the template
116 // is cached but the hierarchy level is not. In this case, we know that the block
117 // exists at this very hierarchy level, so we can just set it.
118 if (!isset($this->resourceHierarchyLevels[$cacheKey][$blockName])) {
119 $this->resourceHierarchyLevels[$cacheKey][$blockName] = $hierarchyLevel;
120 }
121
122 return $this->resourceHierarchyLevels[$cacheKey][$blockName];
123 }
124
125 /**
126 * Loads the cache with the resource for a given block name.
127 *
128 * @see getResourceForBlock()
129 *
130 * @param string $cacheKey The cache key of the form view.
131 * @param FormView $view The form view for finding the applying themes.
132 * @param string $blockName The name of the block to load.
133 *
134 * @return Boolean True if the resource could be loaded, false otherwise.
135 */
136 abstract protected function loadResourceForBlockName($cacheKey, FormView $view, $blockName);
137
138 /**
139 * Loads the cache with the resource for a specific level of a block hierarchy.
140 *
141 * @see getResourceForBlockHierarchy()
142 *
143 * @param string $cacheKey The cache key used for storing the
144 * resource.
145 * @param FormView $view The form view for finding the applying
146 * themes.
147 * @param array $blockNameHierarchy The block hierarchy, with the most
148 * specific block name at the end.
149 * @param integer $hierarchyLevel The level in the block hierarchy that
150 * should be loaded.
151 *
152 * @return Boolean True if the resource could be loaded, false otherwise.
153 */
154 private function loadResourceForBlockNameHierarchy($cacheKey, FormView $view, array $blockNameHierarchy, $hierarchyLevel)
155 {
156 $blockName = $blockNameHierarchy[$hierarchyLevel];
157
158 // Try to find a template for that block
159 if ($this->loadResourceForBlockName($cacheKey, $view, $blockName)) {
160 // If loadTemplateForBlock() returns true, it was able to populate the
161 // cache. The only missing thing is to set the hierarchy level at which
162 // the template was found.
163 $this->resourceHierarchyLevels[$cacheKey][$blockName] = $hierarchyLevel;
164
165 return true;
166 }
167
168 if ($hierarchyLevel > 0) {
169 $parentLevel = $hierarchyLevel - 1;
170 $parentBlockName = $blockNameHierarchy[$parentLevel];
171
172 // The next two if statements contain slightly duplicated code. This is by intention
173 // and tries to avoid execution of unnecessary checks in order to increase performance.
174
175 if (isset($this->resources[$cacheKey][$parentBlockName])) {
176 // It may happen that the parent block is already loaded, but its level is not.
177 // In this case, the parent block must have been loaded by loadResourceForBlock(),
178 // which does not check the hierarchy of the block. Subsequently the block must have
179 // been found directly on the parent level.
180 if (!isset($this->resourceHierarchyLevels[$cacheKey][$parentBlockName])) {
181 $this->resourceHierarchyLevels[$cacheKey][$parentBlockName] = $parentLevel;
182 }
183
184 // Cache the shortcuts for further accesses
185 $this->resources[$cacheKey][$blockName] = $this->resources[$cacheKey][$parentBlockName];
186 $this->resourceHierarchyLevels[$cacheKey][$blockName] = $this->resourceHierarchyLevels[$cacheKey][$parentBlockName];
187
188 return true;
189 }
190
191 if ($this->loadResourceForBlockNameHierarchy($cacheKey, $view, $blockNameHierarchy, $parentLevel)) {
192 // Cache the shortcuts for further accesses
193 $this->resources[$cacheKey][$blockName] = $this->resources[$cacheKey][$parentBlockName];
194 $this->resourceHierarchyLevels[$cacheKey][$blockName] = $this->resourceHierarchyLevels[$cacheKey][$parentBlockName];
195
196 return true;
197 }
198 }
199
200 // Cache the result for further accesses
201 $this->resources[$cacheKey][$blockName] = false;
202 $this->resourceHierarchyLevels[$cacheKey][$blockName] = false;
203
204 return false;
205 }
206}