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
|
12 |
|
public function __construct(array $settings = array()) |
29
|
|
|
{ |
30
|
12 |
|
$this->settings = array_merge(array( |
31
|
12 |
|
'nodes' => [], |
32
|
|
|
'whitelist' => [], |
33
|
|
|
'blacklist' => [], |
34
|
|
|
'default_execution_priority' => 0, |
35
|
|
|
'node_visitor_priority' => 10 |
36
|
|
|
), $settings); |
37
|
|
|
|
38
|
12 |
|
$this->settings['nodes'][Node::class] = null; |
39
|
|
|
|
40
|
12 |
|
$this->settings['nodes'] = call_user_func(function($nodes) { |
41
|
|
|
|
42
|
12 |
|
$processed = []; |
43
|
|
|
|
44
|
12 |
|
foreach ($nodes as $fqcn => $priority) { |
45
|
|
|
|
46
|
12 |
|
if (is_string($priority) && class_exists($priority)) { |
47
|
1 |
|
$fqcn = $priority; |
48
|
1 |
|
$priority = null; |
49
|
|
|
} |
50
|
|
|
|
51
|
12 |
|
$processed[$fqcn] = $priority; |
52
|
|
|
} |
53
|
|
|
|
54
|
12 |
|
return $processed; |
55
|
|
|
|
56
|
12 |
|
}, $this->settings['nodes']); |
57
|
|
|
|
58
|
12 |
|
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
|
11 |
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* {@inheritdoc} |
65
|
|
|
*/ |
66
|
10 |
|
public function getNodeVisitors() |
67
|
|
|
{ |
68
|
|
|
return [ |
69
|
10 |
|
new NodeVisitor($this->settings) |
70
|
|
|
]; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* {@inheritdoc} |
75
|
|
|
*/ |
76
|
10 |
|
public function getTokenParsers() |
77
|
|
|
{ |
78
|
|
|
return [ |
79
|
10 |
|
new TokenParser() |
80
|
|
|
]; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|