Completed
Push — master ( 7babd4...75f7eb )
by Nikola
03:21
created

TwigExtension::__construct()   B

Complexity

Conditions 6
Paths 2

Size

Total Lines 34
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 6

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 34
ccs 16
cts 16
cp 1
rs 8.439
cc 6
eloc 20
nc 2
nop 1
crap 6
1
<?php
2
/*
3
 * This file is part of the Twig Bufferized Template package, an RunOpenCode project.
4
 *
5
 * (c) 2017 RunOpenCode
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace RunOpenCode\Twig\BufferizedTemplate;
11
12
use RunOpenCode\Twig\BufferizedTemplate\Exception\InvalidArgumentException;
13
use RunOpenCode\Twig\BufferizedTemplate\Tag\Bufferize\Node;
14
use RunOpenCode\Twig\BufferizedTemplate\Tag\Bufferize\TokenParser;
15
16
/**
17
 * Class TwigExtension
18
 *
19
 * @package RunOpenCode\Twig\BufferizedTemplate
20
 */
21
class TwigExtension extends \Twig_Extension
22
{
23
    /**
24
     * @var array
25
     */
26
    private $settings;
27
28 10
    public function __construct(array $settings = array())
29
    {
30 10
        $this->settings = array_merge(array(
31 10
            'nodes' => [],
32
            'whitelist' => [],
33
            'blacklist' => [],
34
            'default_execution_priority' => 0,
35
            'node_visitor_priority' => 10
36
        ), $settings);
37
38 10
        $this->settings['nodes'][Node::class] = null;
39
40 10
        $this->settings['nodes'] = call_user_func(function($nodes) {
41
42 10
            $processed = [];
43
44 10
            foreach ($nodes as $fqcn => $priority) {
45
46 10
                if (is_string($priority) && class_exists($priority)) {
47 1
                    $fqcn = $priority;
48 1
                    $priority = null;
49
                }
50
51 10
                $processed[$fqcn] = $priority;
52
            }
53
54 10
            return $processed;
55
56 10
        }, $this->settings['nodes']);
57
58 10
        if (count($this->settings['blacklist']) > 0 && count($this->settings['whitelist'])) {
59 1
            throw new InvalidArgumentException('You can use either black list or white list setting or non for bufferizing templates, but not both.');
60
        }
61 9
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66 8
    public function getNodeVisitors()
67
    {
68
        return [
69 8
            new NodeVisitor($this->settings)
70
        ];
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76 8
    public function getTokenParsers()
77
    {
78
        return [
79 8
            new TokenParser()
80
        ];
81
    }
82
}
83