Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
66.67% covered (warning)
66.67%
16 / 24
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
event
66.67% covered (warning)
66.67%
16 / 24
50.00% covered (danger)
50.00%
1 / 2
8.81
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 compile
63.64% covered (warning)
63.64%
14 / 22
0.00% covered (danger)
0.00%
0 / 1
7.73
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    public function __construct(\Twig\Node\Expression\AbstractExpression $expr, \phpbb\template\twig\environment $environment, $lineno, $tag = null)
28    {
29        $this->environment = $environment;
30
31        parent::__construct(array('expr' => $expr), array(), $lineno, $tag);
32    }
33
34    /**
35    * Compiles the node to PHP.
36    *
37    * @param \Twig\Compiler A Twig\Compiler instance
38    */
39    public function compile(\Twig\Compiler $compiler)
40    {
41        $compiler->addDebugInfo($this);
42
43        $location = $this->listener_directory . $this->getNode('expr')->getAttribute('name');
44
45        foreach ($this->environment->get_phpbb_extensions() as $ext_namespace => $ext_path)
46        {
47            $ext_namespace = str_replace('/', '_', $ext_namespace);
48
49            if ($this->environment->isDebug())
50            {
51                // If debug mode is enabled, lets check for new/removed EVENT
52                //  templates on page load rather than at compile. This is
53                //  slower, but makes developing extensions easier (no need to
54                //  purge the cache when a new event template file is added)
55                $compiler
56                    ->write("if (\$this->env->getLoader()->exists('@{$ext_namespace}/{$location}.html')) {\n")
57                    ->indent()
58                ;
59            }
60
61            if ($this->environment->isDebug() || $this->environment->getLoader()->exists('@' . $ext_namespace . '/' . $location . '.html'))
62            {
63                $compiler
64                    ->write("\$previous_look_up_order = \$this->env->getNamespaceLookUpOrder();\n")
65
66                    // We set the namespace lookup order to be this extension first, then the main path
67                    ->write("\$this->env->setNamespaceLookUpOrder(array('{$ext_namespace}', '__main__'));\n")
68                    ->write("\$this->env->loadTemplate(\$this->env->getTemplateClass('@{$ext_namespace}/{$location}.html'), '@{$ext_namespace}/{$location}.html')->display(\$context);\n")
69                    ->write("\$this->env->setNamespaceLookUpOrder(\$previous_look_up_order);\n")
70                ;
71            }
72
73            if ($this->environment->isDebug())
74            {
75                $compiler
76                    ->outdent()
77                    ->write("}\n\n")
78                ;
79            }
80        }
81    }
82}