Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
78.57% covered (warning)
78.57%
22 / 28
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
event
78.57% covered (warning)
78.57%
22 / 28
50.00% covered (danger)
50.00%
1 / 2
8.63
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 compile
76.00% covered (warning)
76.00%
19 / 25
0.00% covered (danger)
0.00%
0 / 1
7.68
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\node;
15
16class event extends \Twig\Node\Node
17{
18    /**
19    * The subdirectory in which all template listener files must be placed
20    * @var string
21    */
22    protected $listener_directory = 'event/';
23
24    /** @var \phpbb\template\twig\environment */
25    protected $environment;
26
27    /** @var array */
28    protected $template_event_priority_array;
29
30    public function __construct(\Twig\Node\Expression\AbstractExpression $expr, \phpbb\template\twig\environment $environment, $lineno, $tag = null, $template_event_priority_array = [])
31    {
32        $this->environment = $environment;
33        $this->template_event_priority_array = $template_event_priority_array;
34
35        parent::__construct(array('expr' => $expr), array(), $lineno, $tag);
36    }
37
38    /**
39    * Compiles the node to PHP.
40    *
41    * @param \Twig\Compiler A Twig\Compiler instance
42    */
43    public function compile(\Twig\Compiler $compiler)
44    {
45        $compiler->addDebugInfo($this);
46
47        $location = $this->listener_directory . $this->getNode('expr')->getAttribute('name');
48
49        $template_event_listeners = [];
50
51        // Group and sort extension template events in according to their priority (0 by default if not set)
52        foreach ($this->environment->get_phpbb_extensions() as $ext_namespace => $ext_path)
53        {
54            $ext_namespace = str_replace('/', '_', $ext_namespace);
55            $priority_key = intval($this->template_event_priority_array[$ext_namespace][$location] ?? 0);
56            $template_event_listeners[$priority_key][] = $ext_namespace;
57        }
58        krsort($template_event_listeners);
59
60        $template_event_listeners = array_merge(...$template_event_listeners);
61        foreach ($template_event_listeners as $ext_namespace)
62        {
63            if ($this->environment->isDebug())
64            {
65                // If debug mode is enabled, lets check for new/removed EVENT
66                //  templates on page load rather than at compile. This is
67                //  slower, but makes developing extensions easier (no need to
68                //  purge the cache when a new event template file is added)
69                $compiler
70                    ->write("if (\$this->env->getLoader()->exists('@{$ext_namespace}/{$location}.html')) {\n")
71                    ->indent();
72            }
73
74            if ($this->environment->isDebug() || $this->environment->getLoader()->exists('@' . $ext_namespace . '/' . $location . '.html'))
75            {
76                $compiler
77                    ->write("\$previous_look_up_order = \$this->env->getNamespaceLookUpOrder();\n")
78
79                    // We set the namespace lookup order to be this extension first, then the main path
80                    ->write("\$this->env->setNamespaceLookUpOrder(array('{$ext_namespace}', '__main__'));\n")
81                    ->write("\$this->env->loadTemplate(\$this->env->getTemplateClass('@{$ext_namespace}/{$location}.html'), '@{$ext_namespace}/{$location}.html')->display(\$context);\n")
82                    ->write("\$this->env->setNamespaceLookUpOrder(\$previous_look_up_order);\n");
83            }
84
85            if ($this->environment->isDebug())
86            {
87                $compiler
88                    ->outdent()
89                    ->write("}\n\n");
90            }
91        }
92    }
93}