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 |
twig.php
001 <?php
002 /**
003 *
004 * This file is part of the phpBB Forum Software package.
005 *
006 * @copyright (c) phpBB Limited <https://www.phpbb.com>
007 * @license GNU General Public License, version 2 (GPL-2.0)
008 *
009 * For full copyright and license information, please see
010 * the docs/CREDITS.txt file.
011 *
012 */
013
014 namespace phpbb\template\twig;
015
016 use phpbb\template\exception\user_object_not_available;
017
018 /**
019 * Twig Template class.
020 */
021 class twig extends \phpbb\template\base
022 {
023 /**
024 * Path of the cache directory for the template
025 *
026 * Cannot be changed during runtime.
027 *
028 * @var string
029 */
030 private $cachepath = '';
031
032 /**
033 * phpBB path helper
034 * @var \phpbb\path_helper
035 */
036 protected $path_helper;
037
038 /**
039 * phpBB root path
040 * @var string
041 */
042 protected $phpbb_root_path;
043
044 /**
045 * PHP file extension
046 * @var string
047 */
048 protected $php_ext;
049
050 /**
051 * phpBB config instance
052 * @var \phpbb\config\config
053 */
054 protected $config;
055
056 /**
057 * Current user
058 * @var \phpbb\user
059 */
060 protected $user;
061
062 /**
063 * Extension manager.
064 *
065 * @var \phpbb\extension\manager
066 */
067 protected $extension_manager;
068
069 /**
070 * Twig Environment
071 *
072 * @var \Twig_Environment
073 */
074 protected $twig;
075
076 /**
077 * Constructor.
078 *
079 * @param \phpbb\path_helper $path_helper
080 * @param \phpbb\config\config $config
081 * @param \phpbb\template\context $context template context
082 * @param \phpbb\template\twig\environment $twig_environment
083 * @param string $cache_path
084 * @param \phpbb\user|null $user
085 * @param array|\ArrayAccess $extensions
086 * @param \phpbb\extension\manager $extension_manager extension manager, if null then template events will not be invoked
087 */
088 public function __construct(\phpbb\path_helper $path_helper, $config, \phpbb\template\context $context, \phpbb\template\twig\environment $twig_environment, $cache_path, \phpbb\user $user = null, $extensions = array(), \phpbb\extension\manager $extension_manager = null)
089 {
090 $this->path_helper = $path_helper;
091 $this->phpbb_root_path = $path_helper->get_phpbb_root_path();
092 $this->php_ext = $path_helper->get_php_ext();
093 $this->config = $config;
094 $this->user = $user;
095 $this->context = $context;
096 $this->extension_manager = $extension_manager;
097 $this->cachepath = $cache_path;
098 $this->twig = $twig_environment;
099
100 foreach ($extensions as $extension)
101 {
102 $this->twig->addExtension($extension);
103 }
104
105 // Add admin namespace
106 if ($this->path_helper->get_adm_relative_path() !== null && is_dir($this->phpbb_root_path . $this->path_helper->get_adm_relative_path() . 'style/'))
107 {
108 $this->twig->getLoader()->setPaths($this->phpbb_root_path . $this->path_helper->get_adm_relative_path() . 'style/', 'admin');
109 }
110 }
111
112 /**
113 * Clear the cache
114 *
115 * @return \phpbb\template\template
116 */
117 public function clear_cache()
118 {
119 if (is_dir($this->cachepath))
120 {
121 $this->twig->clearCacheFiles();
122 }
123
124 return $this;
125 }
126
127 /**
128 * Get the style tree of the style preferred by the current user
129 *
130 * @return array Style tree, most specific first
131 *
132 * @throws \phpbb\template\exception\user_object_not_available When user service was not set
133 */
134 public function get_user_style()
135 {
136 if ($this->user === null)
137 {
138 throw new user_object_not_available();
139 }
140
141 $style_list = array(
142 $this->user->style['style_path'],
143 );
144
145 if ($this->user->style['style_parent_id'])
146 {
147 $style_list = array_merge($style_list, array_reverse(explode('/', $this->user->style['style_parent_tree'])));
148 }
149
150 return $style_list;
151 }
152
153 /**
154 * Set style location based on (current) user's chosen style.
155 *
156 * @param array $style_directories The directories to add style paths for
157 * E.g. array('ext/foo/bar/styles', 'styles')
158 * Default: array('styles') (phpBB's style directory)
159 * @return \phpbb\template\template $this
160 */
161 public function set_style($style_directories = array('styles'))
162 {
163 if ($style_directories !== array('styles') && $this->twig->getLoader()->getPaths('core') === array())
164 {
165 // We should set up the core styles path since not already setup
166 $this->set_style();
167 }
168
169 $names = $this->get_user_style();
170 // Add 'all' folder to $names array
171 // It allows extensions to load a template file from 'all' folder,
172 // if a style doesn't include it.
173 $names[] = 'all';
174
175 $paths = array();
176 foreach ($style_directories as $directory)
177 {
178 foreach ($names as $name)
179 {
180 $path = $this->phpbb_root_path . trim($directory, '/') . "/{$name}/";
181 $template_path = $path . 'template/';
182 $theme_path = $path . 'theme/';
183
184 $is_valid_dir = false;
185 if (is_dir($template_path))
186 {
187 $is_valid_dir = true;
188 $paths[] = $template_path;
189 }
190 if (is_dir($theme_path))
191 {
192 $is_valid_dir = true;
193 $paths[] = $theme_path;
194 }
195
196 if ($is_valid_dir)
197 {
198 // Add the base style directory as a safe directory
199 $this->twig->getLoader()->addSafeDirectory($path);
200 }
201 }
202 }
203
204 // If we're setting up the main phpBB styles directory and the core
205 // namespace isn't setup yet, we will set it up now
206 if ($style_directories === array('styles') && $this->twig->getLoader()->getPaths('core') === array())
207 {
208 // Set up the core style paths namespace
209 $this->twig->getLoader()->setPaths($paths, 'core');
210 }
211
212 $this->set_custom_style($names, $paths);
213
214 return $this;
215 }
216
217 /**
218 * Set custom style location (able to use directory outside of phpBB).
219 *
220 * Note: Templates are still compiled to phpBB's cache directory.
221 *
222 * @param string|array $names Array of names (or detailed names) or string of name of template(s) in inheritance tree order, used by extensions.
223 * E.g. array(
224 * 'name' => 'adm',
225 * 'ext_path' => 'adm/style/',
226 * )
227 * @param string|array of string $paths Array of style paths, relative to current root directory
228 * @return \phpbb\template\template $this
229 */
230 public function set_custom_style($names, $paths)
231 {
232 $paths = (is_string($paths)) ? array($paths) : $paths;
233 $names = (is_string($names)) ? array($names) : $names;
234
235 // Set as __main__ namespace
236 $this->twig->getLoader()->setPaths($paths);
237
238 // Add all namespaces for all extensions
239 if ($this->extension_manager instanceof \phpbb\extension\manager)
240 {
241 $names[] = 'all';
242
243 foreach ($this->extension_manager->all_enabled() as $ext_namespace => $ext_path)
244 {
245 // namespaces cannot contain /
246 $namespace = str_replace('/', '_', $ext_namespace);
247 $paths = array();
248
249 foreach ($names as $template_dir)
250 {
251 if (is_array($template_dir))
252 {
253 if (isset($template_dir['ext_path']))
254 {
255 $ext_style_template_path = $ext_path . $template_dir['ext_path'];
256 $ext_style_path = dirname($ext_style_template_path);
257 $ext_style_theme_path = $ext_style_path . 'theme/';
258 }
259 else
260 {
261 $ext_style_path = $ext_path . 'styles/' . $template_dir['name'] . '/';
262 $ext_style_template_path = $ext_style_path . 'template/';
263 $ext_style_theme_path = $ext_style_path . 'theme/';
264 }
265 }
266 else
267 {
268 $ext_style_path = $ext_path . 'styles/' . $template_dir . '/';
269 $ext_style_template_path = $ext_style_path . 'template/';
270 $ext_style_theme_path = $ext_style_path . 'theme/';
271 }
272
273 $is_valid_dir = false;
274 if (is_dir($ext_style_template_path))
275 {
276 $is_valid_dir = true;
277 $paths[] = $ext_style_template_path;
278 }
279 if (is_dir($ext_style_theme_path))
280 {
281 $is_valid_dir = true;
282 $paths[] = $ext_style_theme_path;
283 }
284
285 if ($is_valid_dir)
286 {
287 // Add the base style directory as a safe directory
288 $this->twig->getLoader()->addSafeDirectory($ext_style_path);
289 }
290 }
291
292 $this->twig->getLoader()->setPaths($paths, $namespace);
293 }
294 }
295
296 return $this;
297 }
298
299 /**
300 * Display a template for provided handle.
301 *
302 * The template will be loaded and compiled, if necessary, first.
303 *
304 * This function calls hooks.
305 *
306 * @param string $handle Handle to display
307 * @return \phpbb\template\template $this
308 */
309 public function display($handle)
310 {
311 $result = $this->call_hook($handle, __FUNCTION__);
312 if ($result !== false)
313 {
314 return $result[0];
315 }
316
317 $this->twig->display($this->get_filename_from_handle($handle), $this->get_template_vars());
318
319 return $this;
320 }
321
322 /**
323 * Display the handle and assign the output to a template variable
324 * or return the compiled result.
325 *
326 * @param string $handle Handle to operate on
327 * @param string $template_var Template variable to assign compiled handle to
328 * @param bool $return_content If true return compiled handle, otherwise assign to $template_var
329 * @return \phpbb\template\template|string if $return_content is true return string of the compiled handle, otherwise return $this
330 */
331 public function assign_display($handle, $template_var = '', $return_content = true)
332 {
333 if ($return_content)
334 {
335 return $this->twig->render($this->get_filename_from_handle($handle), $this->get_template_vars());
336 }
337
338 $this->assign_var($template_var, $this->twig->render($this->get_filename_from_handle($handle), $this->get_template_vars()));
339
340 return $this;
341 }
342
343 /**
344 * Get template vars in a format Twig will use (from the context)
345 *
346 * @return array
347 */
348 protected function get_template_vars()
349 {
350 $context_vars = $this->context->get_data_ref();
351
352 $vars = array_merge(
353 $context_vars['.'][0], // To get normal vars
354 array(
355 'definition' => new \phpbb\template\twig\definition(),
356 'loops' => $context_vars, // To get loops
357 )
358 );
359
360 if ($this->user instanceof \phpbb\user)
361 {
362 $vars['user'] = $this->user;
363 }
364
365 // cleanup
366 unset($vars['loops']['.']);
367
368 // Inject in the main context the value added by assign_block_vars() to be able to use directly the Twig loops.
369 foreach ($vars['loops'] as $key => &$value)
370 {
371 $vars[$key] = $value;
372 }
373
374 return $vars;
375 }
376
377 /**
378 * {@inheritdoc}
379 */
380 public function get_source_file_for_handle($handle)
381 {
382 return $this->twig->getLoader()->getCacheKey($this->get_filename_from_handle($handle));
383 }
384 }
385