AbstractRendererEngine.php 6.6 KB

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