|
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
|
|
|
protected $type; |
|
12
|
|
|
private $values = array(); |
|
13
|
|
|
private $end = false; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @param string $type |
|
17
|
|
|
*/ |
|
18
|
1 |
|
public function __construct($type = 'js') { |
|
19
|
1 |
|
$this->type = $type; |
|
20
|
1 |
|
} |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Método que parsea los nodos de la plantilla |
|
24
|
|
|
* @param \Twig_Token $token |
|
25
|
|
|
* @return AssetsNode |
|
26
|
|
|
* @throws \Twig_Error_Syntax |
|
27
|
|
|
* @throws \Twig_Error_Loader |
|
28
|
|
|
*/ |
|
29
|
|
|
public function parse(\Twig_Token $token) { |
|
30
|
|
|
$hash = substr(md5($this->parser->getStream()->getSourceContext()->getPath()), 0, 8); |
|
31
|
|
|
$name = $token->getValue(); |
|
32
|
|
|
$this->extractTemplateNodes(); |
|
33
|
|
|
$node = $this->findTemplateNode(); |
|
34
|
|
|
return new AssetsNode($name, array("node" => $node, "hash" => $hash), $token->getLine(), $this->getTag(), $this->type); |
|
|
|
|
|
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Método que devuelve el tag a buscar en la plantilla |
|
39
|
|
|
* @return string |
|
40
|
|
|
*/ |
|
41
|
1 |
|
public function getTag() |
|
42
|
|
|
{ |
|
43
|
1 |
|
switch ($this->type) |
|
44
|
|
|
{ |
|
45
|
1 |
|
default: |
|
46
|
1 |
|
case 'js': $return = 'scripts'; break; |
|
|
|
|
|
|
47
|
1 |
|
case 'css': $return = 'styles'; break; |
|
|
|
|
|
|
48
|
1 |
|
} |
|
49
|
1 |
|
return $return; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Método que revisa cada l�nea de la plantilla |
|
54
|
|
|
* @param \Twig_TokenStream $stream |
|
55
|
|
|
* @return \Twig_TokenStream |
|
56
|
|
|
*/ |
|
57
|
|
|
protected function checkTemplateLine(\Twig_TokenStream $stream) |
|
58
|
|
|
{ |
|
59
|
|
|
$value = $stream->getCurrent(); |
|
60
|
|
|
switch ($value->getType()) { |
|
61
|
|
|
case \Twig_Token::STRING_TYPE: |
|
62
|
|
|
$this->values[] = $this->parser->getExpressionParser()->parseExpression(); |
|
63
|
|
|
break; |
|
64
|
|
|
case \Twig_Token::BLOCK_END_TYPE: |
|
65
|
|
|
$this->end = true; |
|
66
|
|
|
$stream->next(); |
|
67
|
|
|
break; |
|
68
|
|
|
default: |
|
69
|
|
|
$stream->next(); |
|
70
|
|
|
break; |
|
71
|
|
|
} |
|
72
|
|
|
return $stream; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Método que procesa cada l�nea de la plantilla para extraer los nodos |
|
77
|
|
|
* @throws \Twig_Error_Syntax |
|
78
|
|
|
*/ |
|
79
|
|
|
protected function extractTemplateNodes() |
|
80
|
|
|
{ |
|
81
|
|
|
$stream = $this->parser->getStream(); |
|
82
|
|
|
while (!$this->end) { |
|
83
|
|
|
$stream = $this->checkTemplateLine($stream); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
$stream->expect(\Twig_Token::BLOCK_START_TYPE); |
|
87
|
|
|
$stream->expect(\Twig_Token::NAME_TYPE); |
|
88
|
|
|
$stream->expect(\Twig_Token::BLOCK_END_TYPE); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Método que busca el nodo a parsear |
|
93
|
|
|
* @return \Twig_Node_Expression|null |
|
94
|
|
|
*/ |
|
95
|
|
|
protected function findTemplateNode() |
|
96
|
|
|
{ |
|
97
|
|
|
$node = null; |
|
98
|
|
|
if (0 < count($this->values)) { |
|
99
|
|
|
/** @var \Twig_Node_Expression|\Twig_Node_Expression_Conditional $value */ |
|
100
|
|
|
foreach ($this->values as $value) { |
|
101
|
|
|
list($tmp, $node) = $this->extractTmpAttribute($node, $value); |
|
102
|
|
|
$node->setAttribute("value", $tmp); |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
return $node; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Método que extrae el valor del token |
|
110
|
|
|
* @param \Twig_Node_Expression|\Twig_Node_Expression_Conditional|null $node |
|
111
|
|
|
* |
|
112
|
|
|
* @return array |
|
113
|
|
|
*/ |
|
114
|
|
|
protected function getTmpAttribute($node = null) |
|
115
|
|
|
{ |
|
116
|
|
|
$tmp = $node->getAttribute("value"); |
|
117
|
|
|
if (!is_array($tmp)) { |
|
118
|
|
|
$tmp = array($tmp); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
return $tmp; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Método |
|
126
|
|
|
* @param \Twig_Node_Expression|\Twig_Node_Expression_Conditional|null $node |
|
127
|
|
|
* @param \Twig_Node_Expression|\Twig_Node_Expression_Conditional|null $value |
|
128
|
|
|
* |
|
129
|
|
|
* @return array |
|
|
|
|
|
|
130
|
|
|
*/ |
|
131
|
|
|
protected function extractTmpAttribute($node = null, $value = null) |
|
132
|
|
|
{ |
|
133
|
|
|
$tmp = array(); |
|
134
|
|
|
if (NULL === $node) { |
|
135
|
|
|
$node = $value; |
|
136
|
|
|
}else { |
|
137
|
|
|
$tmp = $this->getTmpAttribute($node); |
|
138
|
|
|
} |
|
139
|
|
|
$tmp[] = $value->getAttribute("value"); |
|
140
|
|
|
|
|
141
|
|
|
return array($tmp, $node); |
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
|
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.