1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Shoot\Shoot; |
5
|
|
|
|
6
|
|
|
use Shoot\Shoot\Twig\NodeVisitor\ModelNodeVisitor; |
7
|
|
|
use Shoot\Shoot\Twig\TokenParser\ModelTokenParser; |
8
|
|
|
use Shoot\Shoot\Twig\TokenParser\OptionalTokenParser; |
9
|
|
|
use Twig\Extension\ExtensionInterface; |
10
|
|
|
use Twig\NodeVisitor\NodeVisitorInterface; |
11
|
|
|
use Twig\TokenParser\TokenParserInterface; |
12
|
|
|
use Twig\TwigFilter; |
13
|
|
|
use Twig\TwigFunction; |
14
|
|
|
use Twig\TwigTest; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* This extension for Twig will enable the use of Shoot. |
18
|
|
|
* |
19
|
|
|
* @internal |
20
|
|
|
*/ |
21
|
|
|
final class Extension implements ExtensionInterface |
22
|
|
|
{ |
23
|
|
|
/** @var Pipeline */ |
24
|
|
|
private $pipeline; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Constructs a new instance of Extension. Takes an instance of the Shoot pipeline. |
28
|
|
|
* |
29
|
|
|
* @param Pipeline $pipeline |
30
|
|
|
*/ |
31
|
16 |
|
public function __construct(Pipeline $pipeline) |
32
|
|
|
{ |
33
|
16 |
|
$this->pipeline = $pipeline; |
34
|
16 |
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param View $view |
38
|
|
|
* |
39
|
|
|
* @return void |
40
|
|
|
* |
41
|
|
|
* @internal |
42
|
|
|
*/ |
43
|
13 |
|
public function process(View $view): void |
44
|
|
|
{ |
45
|
13 |
|
$this->pipeline->process($view); |
46
|
11 |
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Returns a list of filters to add to the existing list. |
50
|
|
|
* |
51
|
|
|
* @return TwigFilter[] |
52
|
|
|
*/ |
53
|
8 |
|
public function getFilters(): array |
54
|
|
|
{ |
55
|
|
|
return [ |
56
|
|
|
new TwigFilter('variables', function (PresentationModel $presentationModel): array { |
57
|
2 |
|
return $presentationModel->getVariables(); |
58
|
8 |
|
}), |
59
|
|
|
]; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Returns a list of functions to add to the existing list. |
64
|
|
|
* |
65
|
|
|
* @return TwigFunction[] |
66
|
|
|
*/ |
67
|
7 |
|
public function getFunctions(): array |
68
|
|
|
{ |
69
|
7 |
|
return []; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Returns the node visitor instances to add to the existing list. |
74
|
|
|
* |
75
|
|
|
* @return NodeVisitorInterface[] |
76
|
|
|
*/ |
77
|
7 |
|
public function getNodeVisitors(): array |
78
|
|
|
{ |
79
|
7 |
|
return [new ModelNodeVisitor()]; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Returns a list of operators to add to the existing list. |
84
|
|
|
* |
85
|
|
|
* @return array[] First array of unary operators, second array of binary operators |
86
|
|
|
*/ |
87
|
7 |
|
public function getOperators(): array |
88
|
|
|
{ |
89
|
7 |
|
return []; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Returns a list of tests to add to the existing list. |
94
|
|
|
* |
95
|
|
|
* @return TwigTest[] |
96
|
|
|
*/ |
97
|
8 |
|
public function getTests(): array |
98
|
|
|
{ |
99
|
|
|
return [ |
100
|
|
|
new TwigTest('model', function ($value): bool { |
101
|
1 |
|
return $value instanceof PresentationModel; |
102
|
8 |
|
}), |
103
|
|
|
]; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Returns the token parser instances to add to the existing list. |
108
|
|
|
* |
109
|
|
|
* @return TokenParserInterface[] |
110
|
|
|
*/ |
111
|
7 |
|
public function getTokenParsers(): array |
112
|
|
|
{ |
113
|
|
|
return [ |
114
|
7 |
|
new ModelTokenParser(), |
115
|
7 |
|
new OptionalTokenParser(), |
116
|
|
|
]; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|