AbstractRendererEngine.php 7.5 KB

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