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
|
|
|
* Método que parsea los nodos de la plantilla |
26
|
|
|
* @param \Twig_Token $token |
27
|
|
|
* @return AssetsNode |
28
|
|
|
* @throws \Twig_Error_Syntax |
29
|
|
|
* @throws \Twig_Error_Loader |
30
|
|
|
*/ |
31
|
2 |
|
public function parse(\Twig_Token $token) |
32
|
|
|
{ |
33
|
2 |
|
$hash = substr(md5($this->parser->getStream()->getSourceContext()->getPath()), 0, 8); |
34
|
2 |
|
$name = $token->getValue(); |
35
|
2 |
|
$this->extractTemplateNodes(); |
36
|
1 |
|
$node = $this->findTemplateNode(); |
37
|
1 |
|
return new AssetsNode($name, array("node" => $node, "hash" => $hash), $token->getLine(), $this->getTag(), $this->type); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Método que devuelve el tag a buscar en la plantilla |
42
|
|
|
* @return string |
43
|
|
|
*/ |
44
|
2 |
|
public function getTag() |
45
|
|
|
{ |
46
|
2 |
|
switch ($this->type) { |
47
|
|
|
default: |
48
|
2 |
|
case 'js': |
49
|
2 |
|
$return = 'scripts'; |
50
|
2 |
|
break; |
51
|
2 |
|
case 'css': |
52
|
2 |
|
$return = 'styles'; |
53
|
2 |
|
break; |
54
|
|
|
} |
55
|
2 |
|
return $return; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Método que revisa cada l�nea de la plantilla |
60
|
|
|
* @param \Twig_TokenStream $stream |
61
|
|
|
* @return \Twig_TokenStream |
62
|
|
|
*/ |
63
|
1 |
|
protected function checkTemplateLine(\Twig_TokenStream $stream) |
64
|
|
|
{ |
65
|
1 |
|
$value = $stream->getCurrent(); |
66
|
1 |
|
switch ($value->getType()) { |
67
|
1 |
|
case \Twig_Token::STRING_TYPE: |
68
|
1 |
|
$this->values[] = $this->parser->getExpressionParser()->parseExpression(); |
69
|
1 |
|
break; |
70
|
1 |
|
case \Twig_Token::BLOCK_END_TYPE: |
71
|
1 |
|
$this->end = true; |
72
|
1 |
|
$stream->next(); |
73
|
1 |
|
break; |
74
|
|
|
default: |
75
|
|
|
$stream->next(); |
76
|
|
|
break; |
77
|
|
|
} |
78
|
1 |
|
return $stream; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Método que procesa cada l�nea de la plantilla para extraer los nodos |
83
|
|
|
* @throws \Twig_Error_Syntax |
84
|
|
|
*/ |
85
|
2 |
|
protected function extractTemplateNodes() |
86
|
|
|
{ |
87
|
2 |
|
$stream = $this->parser->getStream(); |
88
|
2 |
|
while (!$this->end) { |
89
|
1 |
|
$stream = $this->checkTemplateLine($stream); |
90
|
|
|
} |
91
|
|
|
|
92
|
2 |
|
$stream->expect(\Twig_Token::BLOCK_START_TYPE); |
93
|
1 |
|
$stream->expect(\Twig_Token::NAME_TYPE); |
94
|
1 |
|
$stream->expect(\Twig_Token::BLOCK_END_TYPE); |
95
|
1 |
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Método que busca el nodo a parsear |
99
|
|
|
* @return \Twig_Node_Expression|null |
100
|
|
|
*/ |
101
|
1 |
|
protected function findTemplateNode() |
102
|
|
|
{ |
103
|
1 |
|
$node = null; |
104
|
1 |
|
if (0 < count($this->values)) { |
105
|
|
|
/** @var \Twig_Node_Expression|\Twig_Node_Expression_Conditional $value */ |
106
|
1 |
|
foreach ($this->values as $value) { |
107
|
1 |
|
list($tmp, $node) = $this->extractTmpAttribute($node, $value); |
108
|
1 |
|
$node->setAttribute("value", $tmp); |
109
|
|
|
} |
110
|
|
|
} |
111
|
1 |
|
return $node; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Método que extrae el valor del token |
116
|
|
|
* @param \Twig_Node_Expression|\Twig_Node_Expression_Conditional|null $node |
117
|
|
|
* |
118
|
|
|
* @return array |
119
|
|
|
*/ |
120
|
1 |
|
protected function getTmpAttribute($node = null) |
121
|
|
|
{ |
122
|
1 |
|
$tmp = $node->getAttribute("value"); |
|
|
|
|
123
|
1 |
|
if (!is_array($tmp)) { |
124
|
|
|
$tmp = array($tmp); |
125
|
|
|
} |
126
|
|
|
|
127
|
1 |
|
return $tmp; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Método |
132
|
|
|
* @param \Twig_Node_Expression|\Twig_Node_Expression_Conditional|null $node |
133
|
|
|
* @param \Twig_Node_Expression|\Twig_Node_Expression_Conditional|null $value |
134
|
|
|
* |
135
|
|
|
* @return array |
136
|
|
|
*/ |
137
|
1 |
|
protected function extractTmpAttribute($node = null, $value = null) |
138
|
|
|
{ |
139
|
1 |
|
$tmp = array(); |
140
|
1 |
|
if (NULL === $node) { |
141
|
1 |
|
$node = $value; |
142
|
|
|
} else { |
143
|
1 |
|
$tmp = $this->getTmpAttribute($node); |
144
|
|
|
} |
145
|
1 |
|
$tmp[] = $value->getAttribute("value"); |
|
|
|
|
146
|
|
|
|
147
|
1 |
|
return array($tmp, $node); |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.