4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Form
;
15 * Default implementation of {@link FormRendererEngineInterface}.
17 * @author Bernhard Schussek <bschussek@gmail.com>
19 abstract class AbstractRendererEngine
implements FormRendererEngineInterface
22 * The variable in {@link FormView} used as cache key.
24 const CACHE_KEY_VAR
= 'cache_key';
29 protected $defaultThemes;
34 protected $themes = array();
39 protected $resources = array();
44 private $resourceHierarchyLevels = array();
47 * Creates a new renderer engine.
49 * @param array $defaultThemes The default themes. The type of these
50 * themes is open to the implementation.
52 public function __construct(array $defaultThemes = array())
54 $this->defaultThemes
= $defaultThemes;
60 public function setTheme(FormView
$view, $themes)
62 $cacheKey = $view->vars
[self
::CACHE_KEY_VAR
];
64 // Do not cast, as casting turns objects into arrays of properties
65 $this->themes
[$cacheKey] = is_array($themes) ? $themes : array($themes);
67 // Unset instead of resetting to an empty array, in order to allow
68 // implementations (like TwigRendererEngine) to check whether $cacheKey
70 unset($this->resources
[$cacheKey]);
71 unset($this->resourceHierarchyLevels
[$cacheKey]);
77 public function getResourceForBlockName(FormView
$view, $blockName)
79 $cacheKey = $view->vars
[self
::CACHE_KEY_VAR
];
81 if (!isset($this->resources
[$cacheKey][$blockName])) {
82 $this->loadResourceForBlockName($cacheKey, $view, $blockName);
85 return $this->resources
[$cacheKey][$blockName];
91 public function getResourceForBlockNameHierarchy(FormView
$view, array $blockNameHierarchy, $hierarchyLevel)
93 $cacheKey = $view->vars
[self
::CACHE_KEY_VAR
];
94 $blockName = $blockNameHierarchy[$hierarchyLevel];
96 if (!isset($this->resources
[$cacheKey][$blockName])) {
97 $this->loadResourceForBlockNameHierarchy($cacheKey, $view, $blockNameHierarchy, $hierarchyLevel);
100 return $this->resources
[$cacheKey][$blockName];
106 public function getResourceHierarchyLevel(FormView
$view, array $blockNameHierarchy, $hierarchyLevel)
108 $cacheKey = $view->vars
[self
::CACHE_KEY_VAR
];
109 $blockName = $blockNameHierarchy[$hierarchyLevel];
111 if (!isset($this->resources
[$cacheKey][$blockName])) {
112 $this->loadResourceForBlockNameHierarchy($cacheKey, $view, $blockNameHierarchy, $hierarchyLevel);
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;
122 return $this->resourceHierarchyLevels
[$cacheKey][$blockName];
126 * Loads the cache with the resource for a given block name.
128 * @see getResourceForBlock()
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.
134 * @return Boolean True if the resource could be loaded, false otherwise.
136 abstract protected function loadResourceForBlockName($cacheKey, FormView
$view, $blockName);
139 * Loads the cache with the resource for a specific level of a block hierarchy.
141 * @see getResourceForBlockHierarchy()
143 * @param string $cacheKey The cache key used for storing the
145 * @param FormView $view The form view for finding the applying
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
152 * @return Boolean True if the resource could be loaded, false otherwise.
154 private function loadResourceForBlockNameHierarchy($cacheKey, FormView
$view, array $blockNameHierarchy, $hierarchyLevel)
156 $blockName = $blockNameHierarchy[$hierarchyLevel];
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;
168 if ($hierarchyLevel > 0) {
169 $parentLevel = $hierarchyLevel - 1;
170 $parentBlockName = $blockNameHierarchy[$parentLevel];
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.
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;
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];
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];
200 // Cache the result for further accesses
201 $this->resources
[$cacheKey][$blockName] = false;
202 $this->resourceHierarchyLevels
[$cacheKey][$blockName] = false;