Passed
Push — master ( 80705d...652bb5 )
by Guido
02:00
created

TemplateFunctions   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 12
dl 0
loc 50
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A injectInto() 0 24 3
1
<?php
2
/**
3
 * This file is part of the Twig WordPress package.
4
 *
5
 * (c) Guido Scialfa <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
declare(strict_types=1);
12
13
namespace TwigWp\Module;
14
15
class TemplateFunctions implements Injectable
16
{
17
    use Helper;
18
19
    const FILTER_FUNCTIONS_LIST = 'twigwp.template_functions_list';
20
21
    /**
22
     * Kses Functions list
23
     *
24
     * @var array The list of kses functions to register in twig
25
     */
26
    private $functions;
27
28
    /**
29
     * Kses constructor
30
     *
31
     * @param array $functions The list of kses functions to register into twig
32
     */
33
    public function __construct(array $functions = [])
34
    {
35
        $this->functions = $functions;
36
    }
37
38
    /**
39
     * @inheritdoc
40
     */
41
    public function injectInto(\Twig\Environment $twig): \Twig\Environment
42
    {
43
        $functions = $this->functions;
44
45
        if (function_exists('apply_filters')) {
46
            /**
47
             * Filter Kses List to register
48
             *
49
             * @since 1.0.0
50
             *
51
             * @param array $functions The current kses list.
52
             * @param \Twig\Environment $twig The twig environment instance.
53
             */
54
            $functions = apply_filters(self::FILTER_FUNCTIONS_LIST, $functions, $twig);
55
        }
56
57
        foreach ($functions as $key => $k) {
58
            // Looking for options.
59
            list($k, $options) = $this->extractConfiguration((array)$k);
60
61
            $twig->addFunction(new \Twig\TwigFunction($key, $k, $options));
62
        }
63
64
        return $twig;
65
    }
66
}
67