Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
94.00% covered (success)
94.00%
94 / 100
50.00% covered (danger)
50.00%
4 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 1
twig
94.00% covered (success)
94.00%
94 / 100
50.00% covered (danger)
50.00%
4 / 8
39.33
0.00% covered (danger)
0.00%
0 / 1
 __construct
94.12% covered (success)
94.12%
16 / 17
0.00% covered (danger)
0.00%
0 / 1
6.01
 get_user_style
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
3
 set_style
95.65% covered (success)
95.65%
22 / 23
0.00% covered (danger)
0.00%
0 / 1
12
 set_custom_style
90.32% covered (success)
90.32%
28 / 31
0.00% covered (danger)
0.00%
0 / 1
11.11
 display
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 assign_display
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 get_template_vars
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
1 / 1
3
 get_source_file_for_handle
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3*
4* This file is part of the phpBB Forum Software package.
5*
6* @copyright (c) phpBB Limited <https://www.phpbb.com>
7* @license GNU General Public License, version 2 (GPL-2.0)
8*
9* For full copyright and license information, please see
10* the docs/CREDITS.txt file.
11*
12*/
13
14namespace phpbb\template\twig;
15
16use phpbb\template\exception\user_object_not_available;
17
18/**
19* Twig Template class.
20*/
21class twig extends \phpbb\template\base
22{
23    /**
24     * Path of the cache directory for the template
25     * Cannot be changed during runtime.
26     *
27     * @var string
28     */
29    private $cachepath = '';
30
31    /** @var \phpbb\path_helper */
32    protected $path_helper;
33
34    /** @var string phpBB root path */
35    protected $phpbb_root_path;
36
37    /** @var string php File extension    */
38    protected $php_ext;
39
40    /** @var \phpbb\config\config */
41    protected $config;
42
43    /** @var \phpbb\user */
44    protected $user;
45
46    /** @var \phpbb\extension\manager */
47    protected $extension_manager;
48
49    /** @var environment */
50    protected $twig;
51
52    /** @var loader */
53    protected $loader;
54
55    /**
56    * Constructor.
57    *
58    * @param \phpbb\path_helper                $path_helper        Path helper object
59    * @param \phpbb\config\config            $config                Config object
60    * @param \phpbb\template\context        $context            Template context
61    * @param environment                    $twig_environment    Twig environment
62    * @param string                            $cache_path            Template's cache directory path
63    * @param null|\phpbb\user                $user                User object
64    * @param array|\ArrayAccess                $extensions            Template extensions
65    * @param null|\phpbb\extension\manager    $extension_manager    If null then template events will not be invoked
66    */
67    public function __construct(
68        \phpbb\path_helper $path_helper,
69        \phpbb\config\config $config,
70        \phpbb\template\context $context,
71        environment $twig_environment,
72        $cache_path,
73        \phpbb\user|null $user = null,
74        $extensions = [],
75        \phpbb\extension\manager|null $extension_manager = null
76    )
77    {
78        $this->path_helper = $path_helper;
79        $this->phpbb_root_path = $path_helper->get_phpbb_root_path();
80        $this->php_ext = $path_helper->get_php_ext();
81        $this->config = $config;
82        $this->user = $user;
83        $this->context = $context;
84        $this->extension_manager = $extension_manager;
85        $this->cachepath = $cache_path;
86        $this->twig = $twig_environment;
87        $this->loader = $twig_environment->getLoader();
88
89        foreach ($extensions as $extension)
90        {
91            if (!$this->twig->hasExtension(get_class($extension)))
92            {
93                $this->twig->addExtension($extension);
94            }
95        }
96
97        // Add admin namespace
98        if ($this->path_helper->get_adm_relative_path() !== null
99            && is_dir($this->phpbb_root_path . $this->path_helper->get_adm_relative_path() . 'style/')
100            && $this->loader instanceof \Twig\Loader\FilesystemLoader)
101        {
102            $this->loader->setPaths($this->phpbb_root_path . $this->path_helper->get_adm_relative_path() . 'style/', 'admin');
103        }
104    }
105
106    /**
107    * Get the style tree of the style preferred by the current user
108    *
109    * @return array Style tree, most specific first
110    *
111    * @throws user_object_not_available    When user service was not set
112    */
113    public function get_user_style()
114    {
115        if ($this->user === null)
116        {
117            throw new user_object_not_available();
118        }
119
120        $style_list = [
121            $this->user->style['style_path'],
122        ];
123
124        if ($this->user->style['style_parent_id'])
125        {
126            $style_list = array_merge($style_list, array_reverse(explode('/', $this->user->style['style_parent_tree'])));
127        }
128
129        return $style_list;
130    }
131
132    /**
133    * Set style location based on (current) user's chosen style.
134    *
135    * @param array $style_directories The directories to add style paths for
136    *     E.g. array('ext/foo/bar/styles', 'styles')
137    *     Default: array('styles') (phpBB's style directory)
138    * @return \phpbb\template\template $this
139    */
140    public function set_style($style_directories = ['styles'])
141    {
142        if ($style_directories !== ['styles'] && $this->loader->getPaths('core') === [])
143        {
144            // We should set up the core styles path since not already setup
145            $this->set_style();
146        }
147
148        $paths = [];
149
150        // Add 'all' folder to $names array
151        //    It allows extensions to load a template file from 'all' folder,
152        //    if a style doesn't include it.
153        $names = $this->get_user_style();
154        $names[] = 'all';
155
156        foreach ($style_directories as $directory)
157        {
158            foreach ($names as $name)
159            {
160                $path    = $this->phpbb_root_path . trim($directory, '/') . "/{$name}/";
161                $handle    = @opendir($path);
162                $valid    = false;
163
164                if ($handle)
165                {
166                    while (($file = readdir($handle)) !== false)
167                    {
168                        $dir = $path . $file;
169
170                        if ($file[0] !== '.' && is_dir($dir))
171                        {
172                            $paths[] = $dir;
173
174                            $valid = true;
175                        }
176                    }
177
178                    closedir($handle);
179                }
180
181                if ($valid)
182                {
183                    // Add the base style directory as a safe directory
184                    $this->loader->addSafeDirectory($path);
185                }
186            }
187        }
188
189        // If we're setting up the main phpBB styles directory and the core
190        // namespace isn't setup yet, we will set it up now
191        if ($style_directories === ['styles'] && $this->loader->getPaths('core') === [])
192        {
193            // Set up the core style paths namespace
194            $this->loader->setPaths($paths, 'core');
195        }
196
197        $this->set_custom_style($names, $paths);
198
199        return $this;
200    }
201
202    /**
203    * Set custom style location (able to use directory outside of phpBB).
204    *
205    * Note: Templates are still compiled to phpBB's cache directory.
206    *
207    * @param string|array $names Array of names (or detailed names) or string of name of template(s) in inheritance tree order, used by extensions.
208    *    E.g. array(
209    *            'name'         => 'adm',
210    *            'ext_path'     => 'adm/style/',
211    *        )
212    * @param string|array $paths Array of style paths, relative to current root directory
213    * @return \phpbb\template\template $this
214    */
215    public function set_custom_style($names, $paths)
216    {
217        $paths = (is_string($paths)) ? [$paths] : $paths;
218        $names = (is_string($names)) ? [$names] : $names;
219
220        // Set as __main__ namespace
221        $this->loader->setPaths($paths);
222
223        // Add all namespaces for all extensions
224        if ($this->extension_manager instanceof \phpbb\extension\manager)
225        {
226            $names[] = 'all';
227
228            foreach ($this->extension_manager->all_enabled() as $ext_namespace => $ext_path)
229            {
230                // namespaces cannot contain /
231                $namespace = str_replace('/', '_', $ext_namespace);
232                $paths = [];
233
234                foreach ($names as $template_dir)
235                {
236                    if (is_array($template_dir))
237                    {
238                        if (isset($template_dir['ext_path']))
239                        {
240                            $ext_style_template_path = $ext_path . $template_dir['ext_path'];
241                            $ext_style_path = dirname($ext_style_template_path);
242                            $ext_style_theme_path = $ext_style_path . 'theme/';
243                        }
244                        else
245                        {
246                            $ext_style_path = $ext_path . 'styles/' . $template_dir['name'] . '/';
247                            $ext_style_template_path = $ext_style_path . 'template/';
248                            $ext_style_theme_path = $ext_style_path . 'theme/';
249                        }
250                    }
251                    else
252                    {
253                        $ext_style_path = $ext_path . 'styles/' . $template_dir . '/';
254                        $ext_style_template_path = $ext_style_path . 'template/';
255                        $ext_style_theme_path = $ext_style_path . 'theme/';
256                    }
257
258                    $is_valid_dir = false;
259                    if (is_dir($ext_style_template_path))
260                    {
261                        $is_valid_dir = true;
262                        $paths[] = $ext_style_template_path;
263                    }
264                    if (is_dir($ext_style_theme_path))
265                    {
266                        $is_valid_dir = true;
267                        $paths[] = $ext_style_theme_path;
268                    }
269
270                    if ($is_valid_dir)
271                    {
272                        // Add the base style directory as a safe directory
273                        $this->loader->addSafeDirectory($ext_style_path);
274                    }
275                }
276
277                $this->loader->setPaths($paths, $namespace);
278            }
279        }
280
281        return $this;
282    }
283
284    /**
285    * Display a template for provided handle.
286    *
287    * The template will be loaded and compiled, if necessary, first.
288    *
289    * This function calls hooks.
290    *
291    * @param string $handle Handle to display
292    * @return \phpbb\template\template $this
293    */
294    public function display($handle)
295    {
296        $this->twig->display($this->get_filename_from_handle($handle), $this->get_template_vars());
297
298        return $this;
299    }
300
301    /**
302    * Display the handle and assign the output to a template variable
303    * or return the compiled result.
304    *
305    * @param string $handle Handle to operate on
306    * @param string $template_var Template variable to assign compiled handle to
307    * @param bool $return_content If true return compiled handle, otherwise assign to $template_var
308    * @return \phpbb\template\template|string if $return_content is true return string of the compiled handle, otherwise return $this
309    */
310    public function assign_display($handle, $template_var = '', $return_content = true)
311    {
312        if ($return_content)
313        {
314            return $this->twig->render($this->get_filename_from_handle($handle), $this->get_template_vars());
315        }
316
317        $this->assign_var($template_var, $this->twig->render($this->get_filename_from_handle($handle), $this->get_template_vars()));
318
319        return $this;
320    }
321
322    /**
323    * Get template vars in a format Twig will use (from the context)
324    *
325    * @return array
326    */
327    protected function get_template_vars()
328    {
329        $context_vars = $this->context->get_data_ref();
330
331        $vars = array_merge(
332            $context_vars['.'][0], // To get normal vars
333            [
334                'definition'    => new \phpbb\template\twig\definition(),
335                'loops'            => $context_vars, // To get loops
336            ]
337        );
338
339        if ($this->user instanceof \phpbb\user)
340        {
341            $vars['user'] = $this->user;
342        }
343
344        // cleanup
345        unset($vars['loops']['.']);
346
347        // Inject in the main context the value added by assign_block_vars() to be able to use directly the Twig loops.
348        foreach ($vars['loops'] as $key => &$value)
349        {
350            $vars[$key] = $value;
351        }
352
353        return $vars;
354    }
355
356    /**
357    * {@inheritdoc}
358    */
359    public function get_source_file_for_handle($handle)
360    {
361        return $this->loader->getCacheKey($this->get_filename_from_handle($handle));
362    }
363}