Verzeichnisstruktur phpBB-3.2.0
- Veröffentlicht
- 06.01.2017
So funktioniert es
|
Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück |
Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis. |
|
(Beispiel Datei-Icons)
|
Auf das Icon klicken um den Quellcode anzuzeigen |
TwigRendererEngine.php
001 <?php
002
003 /*
004 * This file is part of the Symfony package.
005 *
006 * (c) Fabien Potencier <fabien@symfony.com>
007 *
008 * For the full copyright and license information, please view the LICENSE
009 * file that was distributed with this source code.
010 */
011
012 namespace Symfony\Bridge\Twig\Form;
013
014 use Symfony\Component\Form\AbstractRendererEngine;
015 use Symfony\Component\Form\FormView;
016
017 /**
018 * @author Bernhard Schussek <bschussek@gmail.com>
019 */
020 class TwigRendererEngine extends AbstractRendererEngine implements TwigRendererEngineInterface
021 {
022 /**
023 * @var \Twig_Environment
024 */
025 private $environment;
026
027 /**
028 * @var \Twig_Template
029 */
030 private $template;
031
032 /**
033 * {@inheritdoc}
034 */
035 public function setEnvironment(\Twig_Environment $environment)
036 {
037 $this->environment = $environment;
038 }
039
040 /**
041 * {@inheritdoc}
042 */
043 public function renderBlock(FormView $view, $resource, $blockName, array $variables = array())
044 {
045 $cacheKey = $view->vars[self::CACHE_KEY_VAR];
046
047 $context = $this->environment->mergeGlobals($variables);
048
049 ob_start();
050
051 // By contract,This method can only be called after getting the resource
052 // (which is passed to the method). Getting a resource for the first time
053 // (with an empty cache) is guaranteed to invoke loadResourcesFromTheme(),
054 // where the property $template is initialized.
055
056 // We do not call renderBlock here to avoid too many nested level calls
057 // (XDebug limits the level to 100 by default)
058 $this->template->displayBlock($blockName, $context, $this->resources[$cacheKey]);
059
060 return ob_get_clean();
061 }
062
063 /**
064 * Loads the cache with the resource for a given block name.
065 *
066 * This implementation eagerly loads all blocks of the themes assigned to the given view
067 * and all of its ancestors views. This is necessary, because Twig receives the
068 * list of blocks later. At that point, all blocks must already be loaded, for the
069 * case that the function "block()" is used in the Twig template.
070 *
071 * @see getResourceForBlock()
072 *
073 * @param string $cacheKey The cache key of the form view
074 * @param FormView $view The form view for finding the applying themes
075 * @param string $blockName The name of the block to load
076 *
077 * @return bool True if the resource could be loaded, false otherwise
078 */
079 protected function loadResourceForBlockName($cacheKey, FormView $view, $blockName)
080 {
081 // The caller guarantees that $this->resources[$cacheKey][$block] is
082 // not set, but it doesn't have to check whether $this->resources[$cacheKey]
083 // is set. If $this->resources[$cacheKey] is set, all themes for this
084 // $cacheKey are already loaded (due to the eager population, see doc comment).
085 if (isset($this->resources[$cacheKey])) {
086 // As said in the previous, the caller guarantees that
087 // $this->resources[$cacheKey][$block] is not set. Since the themes are
088 // already loaded, it can only be a non-existing block.
089 $this->resources[$cacheKey][$blockName] = false;
090
091 return false;
092 }
093
094 // Recursively try to find the block in the themes assigned to $view,
095 // then of its parent view, then of the parent view of the parent and so on.
096 // When the root view is reached in this recursion, also the default
097 // themes are taken into account.
098
099 // Check each theme whether it contains the searched block
100 if (isset($this->themes[$cacheKey])) {
101 for ($i = count($this->themes[$cacheKey]) - 1; $i >= 0; --$i) {
102 $this->loadResourcesFromTheme($cacheKey, $this->themes[$cacheKey][$i]);
103 // CONTINUE LOADING (see doc comment)
104 }
105 }
106
107 // Check the default themes once we reach the root view without success
108 if (!$view->parent) {
109 for ($i = count($this->defaultThemes) - 1; $i >= 0; --$i) {
110 $this->loadResourcesFromTheme($cacheKey, $this->defaultThemes[$i]);
111 // CONTINUE LOADING (see doc comment)
112 }
113 }
114
115 // Proceed with the themes of the parent view
116 if ($view->parent) {
117 $parentCacheKey = $view->parent->vars[self::CACHE_KEY_VAR];
118
119 if (!isset($this->resources[$parentCacheKey])) {
120 $this->loadResourceForBlockName($parentCacheKey, $view->parent, $blockName);
121 }
122
123 // EAGER CACHE POPULATION (see doc comment)
124 foreach ($this->resources[$parentCacheKey] as $nestedBlockName => $resource) {
125 if (!isset($this->resources[$cacheKey][$nestedBlockName])) {
126 $this->resources[$cacheKey][$nestedBlockName] = $resource;
127 }
128 }
129 }
130
131 // Even though we loaded the themes, it can happen that none of them
132 // contains the searched block
133 if (!isset($this->resources[$cacheKey][$blockName])) {
134 // Cache that we didn't find anything to speed up further accesses
135 $this->resources[$cacheKey][$blockName] = false;
136 }
137
138 return false !== $this->resources[$cacheKey][$blockName];
139 }
140
141 /**
142 * Loads the resources for all blocks in a theme.
143 *
144 * @param string $cacheKey The cache key for storing the resource
145 * @param mixed $theme The theme to load the block from. This parameter
146 * is passed by reference, because it might be necessary
147 * to initialize the theme first. Any changes made to
148 * this variable will be kept and be available upon
149 * further calls to this method using the same theme.
150 */
151 protected function loadResourcesFromTheme($cacheKey, &$theme)
152 {
153 if (!$theme instanceof \Twig_Template) {
154 /* @var \Twig_Template $theme */
155 $theme = $this->environment->loadTemplate($theme);
156 }
157
158 if (null === $this->template) {
159 // Store the first \Twig_Template instance that we find so that
160 // we can call displayBlock() later on. It doesn't matter *which*
161 // template we use for that, since we pass the used blocks manually
162 // anyway.
163 $this->template = $theme;
164 }
165
166 // Use a separate variable for the inheritance traversal, because
167 // theme is a reference and we don't want to change it.
168 $currentTheme = $theme;
169
170 $context = $this->environment->mergeGlobals(array());
171
172 // The do loop takes care of template inheritance.
173 // Add blocks from all templates in the inheritance tree, but avoid
174 // overriding blocks already set.
175 do {
176 foreach ($currentTheme->getBlocks() as $block => $blockData) {
177 if (!isset($this->resources[$cacheKey][$block])) {
178 // The resource given back is the key to the bucket that
179 // contains this block.
180 $this->resources[$cacheKey][$block] = $blockData;
181 }
182 }
183 } while (false !== $currentTheme = $currentTheme->getParent($context));
184 }
185 }
186