TemplatingRendererEngine.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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\Extension\Templating;
  11. use Symfony\Component\Form\AbstractRendererEngine;
  12. use Symfony\Component\Form\FormView;
  13. use Symfony\Component\Templating\EngineInterface;
  14. /**
  15. * @author Bernhard Schussek <bschussek@gmail.com>
  16. */
  17. class TemplatingRendererEngine extends AbstractRendererEngine
  18. {
  19. private $engine;
  20. public function __construct(EngineInterface $engine, array $defaultThemes = array())
  21. {
  22. parent::__construct($defaultThemes);
  23. $this->engine = $engine;
  24. }
  25. /**
  26. * {@inheritdoc}
  27. */
  28. public function renderBlock(FormView $view, $resource, $blockName, array $variables = array())
  29. {
  30. return trim($this->engine->render($resource, $variables));
  31. }
  32. /**
  33. * Loads the cache with the resource for a given block name.
  34. *
  35. * This implementation tries to load as few blocks as possible, since each block
  36. * is represented by a template on the file system.
  37. *
  38. * @see getResourceForBlock()
  39. *
  40. * @param string $cacheKey The cache key of the form view
  41. * @param FormView $view The form view for finding the applying themes
  42. * @param string $blockName The name of the block to load
  43. *
  44. * @return bool True if the resource could be loaded, false otherwise
  45. */
  46. protected function loadResourceForBlockName($cacheKey, FormView $view, $blockName)
  47. {
  48. // Recursively try to find the block in the themes assigned to $view,
  49. // then of its parent form, then of the parent form of the parent and so on.
  50. // When the root form is reached in this recursion, also the default
  51. // themes are taken into account.
  52. // Check each theme whether it contains the searched block
  53. if (isset($this->themes[$cacheKey])) {
  54. for ($i = \count($this->themes[$cacheKey]) - 1; $i >= 0; --$i) {
  55. if ($this->loadResourceFromTheme($cacheKey, $blockName, $this->themes[$cacheKey][$i])) {
  56. return true;
  57. }
  58. }
  59. }
  60. // Check the default themes once we reach the root form without success
  61. if (!$view->parent) {
  62. for ($i = \count($this->defaultThemes) - 1; $i >= 0; --$i) {
  63. if ($this->loadResourceFromTheme($cacheKey, $blockName, $this->defaultThemes[$i])) {
  64. return true;
  65. }
  66. }
  67. }
  68. // If we did not find anything in the themes of the current view, proceed
  69. // with the themes of the parent view
  70. if ($view->parent) {
  71. $parentCacheKey = $view->parent->vars[self::CACHE_KEY_VAR];
  72. if (!isset($this->resources[$parentCacheKey][$blockName])) {
  73. $this->loadResourceForBlockName($parentCacheKey, $view->parent, $blockName);
  74. }
  75. // If a template exists in the parent themes, cache that template
  76. // for the current theme as well to speed up further accesses
  77. if ($this->resources[$parentCacheKey][$blockName]) {
  78. $this->resources[$cacheKey][$blockName] = $this->resources[$parentCacheKey][$blockName];
  79. return true;
  80. }
  81. }
  82. // Cache that we didn't find anything to speed up further accesses
  83. $this->resources[$cacheKey][$blockName] = false;
  84. return false;
  85. }
  86. /**
  87. * Tries to load the resource for a block from a theme.
  88. *
  89. * @param string $cacheKey The cache key for storing the resource
  90. * @param string $blockName The name of the block to load a resource for
  91. * @param mixed $theme The theme to load the block from
  92. *
  93. * @return bool True if the resource could be loaded, false otherwise
  94. */
  95. protected function loadResourceFromTheme($cacheKey, $blockName, $theme)
  96. {
  97. if ($this->engine->exists($templateName = $theme.':'.$blockName.'.html.php')) {
  98. $this->resources[$cacheKey][$blockName] = $templateName;
  99. return true;
  100. }
  101. return false;
  102. }
  103. }