AbstractRendererEngine.php 7.3 KB

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