1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PSFS\base\extension; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class AssetsTokenParser |
7
|
|
|
* @package PSFS\base\extension |
8
|
|
|
*/ |
9
|
|
|
class AssetsTokenParser extends \Twig_TokenParser |
10
|
|
|
{ |
11
|
|
|
|
12
|
|
|
protected $type; |
13
|
|
|
private $values = array(); |
14
|
|
|
private $end = false; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @param string $type |
18
|
|
|
*/ |
19
|
1 |
|
public function __construct($type = 'js') |
20
|
|
|
{ |
21
|
1 |
|
$this->type = $type; |
22
|
1 |
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param \Twig_Token $token |
26
|
|
|
* @return AssetsNode|\Twig_Node |
27
|
|
|
* @throws \Twig_Error_Syntax |
28
|
|
|
*/ |
29
|
2 |
|
public function parse(\Twig_Token $token) |
30
|
|
|
{ |
31
|
2 |
|
$hash = substr(md5($this->parser->getStream()->getSourceContext()->getPath()), 0, 8); |
32
|
2 |
|
$name = $token->getValue(); |
33
|
2 |
|
$this->extractTemplateNodes(); |
34
|
1 |
|
$node = $this->findTemplateNode(); |
35
|
1 |
|
return new AssetsNode($name, array('node' => $node, 'hash' => $hash), $token->getLine(), $this->getTag(), $this->type); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Método que devuelve el tag a buscar en la plantilla |
40
|
|
|
* @return string |
41
|
|
|
*/ |
42
|
2 |
|
public function getTag() |
43
|
|
|
{ |
44
|
2 |
|
switch ($this->type) { |
45
|
|
|
default: |
46
|
|
|
case 'js': |
47
|
2 |
|
$return = 'scripts'; |
48
|
2 |
|
break; |
49
|
|
|
case 'css': |
50
|
2 |
|
$return = 'styles'; |
51
|
2 |
|
break; |
52
|
|
|
} |
53
|
2 |
|
return $return; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param \Twig_TokenStream $stream |
58
|
|
|
* @return \Twig_TokenStream |
59
|
|
|
* @throws \Twig_Error_Syntax |
60
|
|
|
*/ |
61
|
1 |
|
protected function checkTemplateLine(\Twig_TokenStream $stream) |
62
|
|
|
{ |
63
|
1 |
|
$value = $stream->getCurrent(); |
64
|
1 |
|
switch ($value->getType()) { |
65
|
1 |
|
case \Twig_Token::STRING_TYPE: |
66
|
1 |
|
$this->values[] = $this->parser->getExpressionParser()->parseExpression(); |
67
|
1 |
|
break; |
68
|
1 |
|
case \Twig_Token::BLOCK_END_TYPE: |
69
|
1 |
|
$this->end = true; |
70
|
1 |
|
$stream->next(); |
71
|
1 |
|
break; |
72
|
|
|
default: |
73
|
|
|
$stream->next(); |
74
|
|
|
break; |
75
|
|
|
} |
76
|
1 |
|
return $stream; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @throws \Twig_Error_Syntax |
81
|
|
|
*/ |
82
|
2 |
|
protected function extractTemplateNodes() |
83
|
|
|
{ |
84
|
2 |
|
$stream = $this->parser->getStream(); |
85
|
2 |
|
while (!$this->end) { |
86
|
1 |
|
$stream = $this->checkTemplateLine($stream); |
87
|
|
|
} |
88
|
|
|
|
89
|
2 |
|
$stream->expect(\Twig_Token::BLOCK_START_TYPE); |
90
|
1 |
|
$stream->expect(\Twig_Token::NAME_TYPE); |
91
|
1 |
|
$stream->expect(\Twig_Token::BLOCK_END_TYPE); |
92
|
1 |
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @return mixed|null |
96
|
|
|
*/ |
97
|
1 |
|
protected function findTemplateNode() |
98
|
|
|
{ |
99
|
1 |
|
$node = null; |
100
|
1 |
|
if (0 < count($this->values)) { |
101
|
|
|
/** @var \Twig_Node_Expression|\Twig_Node_Expression_Conditional $value */ |
102
|
1 |
|
foreach ($this->values as $value) { |
103
|
1 |
|
list($tmp, $node) = $this->extractTmpAttribute($node, $value); |
104
|
1 |
|
$node->setAttribute('value', $tmp); |
105
|
|
|
} |
106
|
|
|
} |
107
|
1 |
|
return $node; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param null $node |
|
|
|
|
112
|
|
|
* @return array |
113
|
|
|
*/ |
114
|
1 |
|
protected function getTmpAttribute($node = null) |
115
|
|
|
{ |
116
|
1 |
|
$tmp = []; |
117
|
1 |
|
if(null !== $node) { |
|
|
|
|
118
|
1 |
|
$tmp = $node->getAttribute('value'); |
119
|
1 |
|
if (!is_array($tmp)) { |
120
|
|
|
$tmp = [$tmp]; |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
124
|
1 |
|
return $tmp; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Método |
129
|
|
|
* @param \Twig_Node_Expression|\Twig_Node_Expression_Conditional|null $node |
130
|
|
|
* @param \Twig_Node_Expression|\Twig_Node_Expression_Conditional|null $value |
131
|
|
|
* |
132
|
|
|
* @return array |
133
|
|
|
*/ |
134
|
1 |
|
protected function extractTmpAttribute($node = null, $value = null) |
135
|
|
|
{ |
136
|
1 |
|
$tmp = []; |
137
|
1 |
|
if (null === $node) { |
138
|
1 |
|
$node = $value; |
139
|
|
|
} else { |
140
|
1 |
|
$tmp = $this->getTmpAttribute($node); |
141
|
|
|
} |
142
|
1 |
|
if(null !== $node) { |
143
|
1 |
|
$tmp[] = $value->getAttribute('value'); |
|
|
|
|
144
|
|
|
} |
145
|
1 |
|
return [$tmp, $node]; |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|