1 | <?php |
||
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 | public function __construct($type = 'js') |
||
20 | { |
||
21 | $this->type = $type; |
||
22 | } |
||
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 | 1 | public function parse(\Twig_Token $token) |
|
32 | { |
||
33 | 1 | $hash = substr(md5($this->parser->getStream()->getSourceContext()->getPath()), 0, 8); |
|
34 | 1 | $name = $token->getValue(); |
|
35 | 1 | $this->extractTemplateNodes(); |
|
36 | $node = $this->findTemplateNode(); |
||
37 | 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 | public function getTag() |
||
45 | { |
||
46 | switch ($this->type) { |
||
47 | default: |
||
48 | case 'js': |
||
49 | $return = 'scripts'; |
||
50 | break; |
||
51 | case 'css': |
||
52 | $return = 'styles'; |
||
53 | break; |
||
54 | } |
||
55 | 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 | protected function checkTemplateLine(\Twig_TokenStream $stream) |
||
80 | |||
81 | /** |
||
82 | * Método que procesa cada l�nea de la plantilla para extraer los nodos |
||
83 | * @throws \Twig_Error_Syntax |
||
84 | */ |
||
85 | 1 | protected function extractTemplateNodes() |
|
86 | { |
||
87 | 1 | $stream = $this->parser->getStream(); |
|
88 | 1 | while (!$this->end) { |
|
89 | $stream = $this->checkTemplateLine($stream); |
||
90 | } |
||
91 | |||
92 | 1 | $stream->expect(\Twig_Token::BLOCK_START_TYPE); |
|
93 | $stream->expect(\Twig_Token::NAME_TYPE); |
||
94 | $stream->expect(\Twig_Token::BLOCK_END_TYPE); |
||
95 | } |
||
96 | |||
97 | /** |
||
98 | * Método que busca el nodo a parsear |
||
99 | * @return \Twig_Node_Expression|null |
||
100 | */ |
||
101 | protected function findTemplateNode() |
||
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 | protected function getTmpAttribute($node = null) |
||
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 | protected function extractTmpAttribute($node = null, $value = null) |
||
149 | } |
||
150 |
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.