Verzeichnisstruktur phpBB-3.3.15
- Veröffentlicht
- 28.08.2024
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 use Twig\Environment;
017 use Twig\Template;
018
019 /**
020 * @author Bernhard Schussek <bschussek@gmail.com>
021 */
022 class TwigRendererEngine extends AbstractRendererEngine implements TwigRendererEngineInterface
023 {
024 /**
025 * @var Environment
026 */
027 private $environment;
028
029 /**
030 * @var Template
031 */
032 private $template;
033
034 public function __construct(array $defaultThemes = [], Environment $environment = null)
035 {
036 if (null === $environment) {
037 @trigger_error(sprintf('Not passing a Twig Environment as the second argument for "%s" constructor is deprecated since Symfony 3.2 and won\'t be possible in 4.0.', static::class), \E_USER_DEPRECATED);
038 }
039
040 parent::__construct($defaultThemes);
041 $this->environment = $environment;
042 }
043
044 /**
045 * {@inheritdoc}
046 *
047 * @deprecated since version 3.3, to be removed in 4.0
048 */
049 public function setEnvironment(Environment $environment)
050 {
051 if ($this->environment) {
052 @trigger_error(sprintf('The "%s()" method is deprecated since Symfony 3.3 and will be removed in 4.0. Pass the Twig Environment as second argument of the constructor instead.', __METHOD__), \E_USER_DEPRECATED);
053 }
054
055 $this->environment = $environment;
056 }
057
058 /**
059 * {@inheritdoc}
060 */
061 public function renderBlock(FormView $view, $resource, $blockName, array $variables = [])
062 {
063 $cacheKey = $view->vars[self::CACHE_KEY_VAR];
064
065 $context = $this->environment->mergeGlobals($variables);
066
067 ob_start();
068
069 // By contract,This method can only be called after getting the resource
070 // (which is passed to the method). Getting a resource for the first time
071 // (with an empty cache) is guaranteed to invoke loadResourcesFromTheme(),
072 // where the property $template is initialized.
073
074 // We do not call renderBlock here to avoid too many nested level calls
075 // (XDebug limits the level to 100 by default)
076 $this->template->displayBlock($blockName, $context, $this->resources[$cacheKey]);
077
078 return ob_get_clean();
079 }
080
081 /**
082 * Loads the cache with the resource for a given block name.
083 *
084 * This implementation eagerly loads all blocks of the themes assigned to the given view
085 * and all of its ancestors views. This is necessary, because Twig receives the
086 * list of blocks later. At that point, all blocks must already be loaded, for the
087 * case that the function "block()" is used in the Twig template.
088 *
089 * @see getResourceForBlock()
090 *
091 * @param string $cacheKey The cache key of the form view
092 * @param FormView $view The form view for finding the applying themes
093 * @param string $blockName The name of the block to load
094 *
095 * @return bool True if the resource could be loaded, false otherwise
096 */
097 protected function loadResourceForBlockName($cacheKey, FormView $view, $blockName)
098 {
099 // The caller guarantees that $this->resources[$cacheKey][$block] is
100 // not set, but it doesn't have to check whether $this->resources[$cacheKey]
101 // is set. If $this->resources[$cacheKey] is set, all themes for this
102 // $cacheKey are already loaded (due to the eager population, see doc comment).
103 if (isset($this->resources[$cacheKey])) {
104 // As said in the previous, the caller guarantees that
105 // $this->resources[$cacheKey][$block] is not set. Since the themes are
106 // already loaded, it can only be a non-existing block.
107 $this->resources[$cacheKey][$blockName] = false;
108
109 return false;
110 }
111
112 // Recursively try to find the block in the themes assigned to $view,
113 // then of its parent view, then of the parent view of the parent and so on.
114 // When the root view is reached in this recursion, also the default
115 // themes are taken into account.
116
117 // Check each theme whether it contains the searched block
118 if (isset($this->themes[$cacheKey])) {
119 for ($i = \count($this->themes[$cacheKey]) - 1; $i >= 0; --$i) {
120 $this->loadResourcesFromTheme($cacheKey, $this->themes[$cacheKey][$i]);
121 // CONTINUE LOADING (see doc comment)
122 }
123 }
124
125 // Check the default themes once we reach the root view without success
126 if (!$view->parent) {
127 if (!isset($this->useDefaultThemes[$cacheKey]) || $this->useDefaultThemes[$cacheKey]) {
128 for ($i = \count($this->defaultThemes) - 1; $i >= 0; --$i) {
129 $this->loadResourcesFromTheme($cacheKey, $this->defaultThemes[$i]);
130 // CONTINUE LOADING (see doc comment)
131 }
132 }
133 }
134
135 // Proceed with the themes of the parent view
136 if ($view->parent) {
137 $parentCacheKey = $view->parent->vars[self::CACHE_KEY_VAR];
138
139 if (!isset($this->resources[$parentCacheKey])) {
140 $this->loadResourceForBlockName($parentCacheKey, $view->parent, $blockName);
141 }
142
143 // EAGER CACHE POPULATION (see doc comment)
144 foreach ($this->resources[$parentCacheKey] as $nestedBlockName => $resource) {
145 if (!isset($this->resources[$cacheKey][$nestedBlockName])) {
146 $this->resources[$cacheKey][$nestedBlockName] = $resource;
147 }
148 }
149 }
150
151 // Even though we loaded the themes, it can happen that none of them
152 // contains the searched block
153 if (!isset($this->resources[$cacheKey][$blockName])) {
154 // Cache that we didn't find anything to speed up further accesses
155 $this->resources[$cacheKey][$blockName] = false;
156 }
157
158 return false !== $this->resources[$cacheKey][$blockName];
159 }
160
161 /**
162 * Loads the resources for all blocks in a theme.
163 *
164 * @param string $cacheKey The cache key for storing the resource
165 * @param mixed $theme The theme to load the block from. This parameter
166 * is passed by reference, because it might be necessary
167 * to initialize the theme first. Any changes made to
168 * this variable will be kept and be available upon
169 * further calls to this method using the same theme.
170 */
171 protected function loadResourcesFromTheme($cacheKey, &$theme)
172 {
173 if (!$theme instanceof Template) {
174 /* @var Template $theme */
175 $theme = $this->environment->loadTemplate($theme);
176 }
177
178 if (null === $this->template) {
179 // Store the first Template instance that we find so that
180 // we can call displayBlock() later on. It doesn't matter *which*
181 // template we use for that, since we pass the used blocks manually
182 // anyway.
183 $this->template = $theme;
184 }
185
186 // Use a separate variable for the inheritance traversal, because
187 // theme is a reference and we don't want to change it.
188 $currentTheme = $theme;
189
190 $context = $this->environment->mergeGlobals([]);
191
192 // The do loop takes care of template inheritance.
193 // Add blocks from all templates in the inheritance tree, but avoid
194 // overriding blocks already set.
195 do {
196 foreach ($currentTheme->getBlocks() as $block => $blockData) {
197 if (!isset($this->resources[$cacheKey][$block])) {
198 // The resource given back is the key to the bucket that
199 // contains this block.
200 $this->resources[$cacheKey][$block] = $blockData;
201 }
202 }
203 } while (false !== $currentTheme = $currentTheme->getParent($context));
204 }
205 }
206