1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the puli/twig-puli-extension package. |
5
|
|
|
* |
6
|
|
|
* (c) Bernhard Schussek <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Puli\TwigExtension\NodeVisitor; |
13
|
|
|
|
14
|
|
|
use Puli\TwigExtension\Node\LoadedByPuliNode; |
15
|
|
|
use Twig_BaseNodeVisitor; |
16
|
|
|
use Twig_Environment; |
17
|
|
|
use Twig_Node; |
18
|
|
|
use Twig_Node_Body; |
19
|
|
|
use Twig_Node_Module; |
20
|
|
|
use Webmozart\PathUtil\Path; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Adds the "puli-dir" attribute to all {@link Twig_Module} nodes that were |
24
|
|
|
* loaded through the Puli loader. |
25
|
|
|
* |
26
|
|
|
* This attribute can be used to convert relative paths in the template to |
27
|
|
|
* absolute paths. |
28
|
|
|
* |
29
|
|
|
* @since 1.0 |
30
|
|
|
* |
31
|
|
|
* @author Bernhard Schussek <[email protected]> |
32
|
|
|
*/ |
33
|
|
|
class PuliDirTagger extends Twig_BaseNodeVisitor |
34
|
|
|
{ |
35
|
|
|
/** |
36
|
|
|
* @var Twig_Node_Module|null |
37
|
|
|
*/ |
38
|
|
|
private $moduleNode; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Called before child nodes are visited. |
42
|
|
|
* |
43
|
|
|
* @param Twig_Node $node The node to visit |
44
|
|
|
* @param Twig_Environment $env The Twig environment instance |
45
|
|
|
* |
46
|
|
|
* @return Twig_Node The modified node |
47
|
|
|
*/ |
48
|
23 |
|
protected function doEnterNode(Twig_Node $node, Twig_Environment $env) |
49
|
|
|
{ |
50
|
23 |
|
if ($node instanceof Twig_Node_Module) { |
51
|
23 |
|
$this->moduleNode = $node; |
52
|
|
|
} |
53
|
|
|
|
54
|
23 |
|
return $node; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Called after child nodes are visited. |
59
|
|
|
* |
60
|
|
|
* @param Twig_Node $node The node to visit |
61
|
|
|
* @param Twig_Environment $env The Twig environment instance |
62
|
|
|
* |
63
|
|
|
* @return Twig_Node|false The modified node or false if the node must be removed |
64
|
|
|
*/ |
65
|
23 |
|
protected function doLeaveNode(Twig_Node $node, Twig_Environment $env) |
66
|
|
|
{ |
67
|
|
|
// Tag the node if it contains a LoadedByPuliNode |
68
|
|
|
// This cannot be done in enterNode(), because only leaveNode() may |
69
|
|
|
// return false in order to remove a node |
70
|
23 |
|
if ($node instanceof LoadedByPuliNode) { |
71
|
19 |
|
if (null !== $this->moduleNode) { |
72
|
19 |
|
$this->moduleNode->setAttribute( |
73
|
19 |
|
'puli-dir', |
74
|
19 |
|
Path::getDirectory($this->moduleNode->getAttribute('filename')) |
75
|
|
|
); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
// Remove that node from the final tree |
79
|
19 |
|
return false; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
// Special case: Empty files that contained only the LoadedByPuliNode |
83
|
|
|
// now contain no nodes anymore. Twig, however, expects Twig_Node_Body |
84
|
|
|
// instances to have at least one (even if empty) node with name 0. |
85
|
23 |
|
if ($node instanceof Twig_Node_Body) { |
86
|
23 |
|
if (0 === $node->count()) { |
87
|
5 |
|
$node->setNode(0, new Twig_Node(array(), array(), 1)); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
23 |
|
return $node; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Returns the priority for this visitor. |
96
|
|
|
* |
97
|
|
|
* Priority should be between -10 and 10 (0 is the default). |
98
|
|
|
* |
99
|
|
|
* @return int The priority level |
100
|
|
|
*/ |
101
|
23 |
|
public function getPriority() |
102
|
|
|
{ |
103
|
|
|
// Should be launched very early on so that other visitors don't have |
104
|
|
|
// to deal with the LoadedByPuliNode |
105
|
23 |
|
return -10; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|