|
0
|
1 |
<?php |
|
|
2 |
|
|
|
3 |
/* |
|
|
4 |
* This file is part of the Assetic package, an OpenSky project. |
|
|
5 |
* |
|
|
6 |
* (c) 2010-2011 OpenSky Project Inc |
|
|
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 Assetic\Extension\Twig; |
|
|
13 |
|
|
|
14 |
use Assetic\Asset\AssetInterface; |
|
|
15 |
use Assetic\Factory\AssetFactory; |
|
|
16 |
|
|
|
17 |
class AsseticTokenParser extends \Twig_TokenParser |
|
|
18 |
{ |
|
|
19 |
private $factory; |
|
|
20 |
private $tag; |
|
|
21 |
private $output; |
|
|
22 |
private $single; |
|
|
23 |
private $extensions; |
|
|
24 |
|
|
|
25 |
/** |
|
|
26 |
* Constructor. |
|
|
27 |
* |
|
|
28 |
* Attributes can be added to the tag by passing names as the options |
|
|
29 |
* array. These values, if found, will be passed to the factory and node. |
|
|
30 |
* |
|
|
31 |
* @param AssetFactory $factory The asset factory |
|
|
32 |
* @param string $tag The tag name |
|
|
33 |
* @param string $output The default output string |
|
|
34 |
* @param Boolean $single Whether to force a single asset |
|
|
35 |
* @param array $extensions Additional attribute names to look for |
|
|
36 |
*/ |
|
|
37 |
public function __construct(AssetFactory $factory, $tag, $output, $single = false, array $extensions = array()) |
|
|
38 |
{ |
|
|
39 |
$this->factory = $factory; |
|
|
40 |
$this->tag = $tag; |
|
|
41 |
$this->output = $output; |
|
|
42 |
$this->single = $single; |
|
|
43 |
$this->extensions = $extensions; |
|
|
44 |
} |
|
|
45 |
|
|
|
46 |
public function parse(\Twig_Token $token) |
|
|
47 |
{ |
|
|
48 |
$inputs = array(); |
|
|
49 |
$filters = array(); |
|
|
50 |
$name = null; |
|
|
51 |
$attributes = array( |
|
|
52 |
'output' => $this->output, |
|
|
53 |
'var_name' => 'asset_url', |
|
|
54 |
); |
|
|
55 |
|
|
|
56 |
$stream = $this->parser->getStream(); |
|
|
57 |
while (!$stream->test(\Twig_Token::BLOCK_END_TYPE)) { |
|
|
58 |
if ($stream->test(\Twig_Token::STRING_TYPE)) { |
|
|
59 |
// '@jquery', 'js/src/core/*', 'js/src/extra.js' |
|
|
60 |
$inputs[] = $stream->next()->getValue(); |
|
|
61 |
} elseif ($stream->test(\Twig_Token::NAME_TYPE, 'filter')) { |
|
|
62 |
// filter='yui_js' |
|
|
63 |
$stream->next(); |
|
|
64 |
$stream->expect(\Twig_Token::OPERATOR_TYPE, '='); |
|
|
65 |
$filters = array_merge($filters, array_filter(array_map('trim', explode(',', $stream->expect(\Twig_Token::STRING_TYPE)->getValue())))); |
|
|
66 |
} elseif ($stream->test(\Twig_Token::NAME_TYPE, 'output')) { |
|
|
67 |
// output='js/packed/*.js' OR output='js/core.js' |
|
|
68 |
$stream->next(); |
|
|
69 |
$stream->expect(\Twig_Token::OPERATOR_TYPE, '='); |
|
|
70 |
$attributes['output'] = $stream->expect(\Twig_Token::STRING_TYPE)->getValue(); |
|
|
71 |
} elseif ($stream->test(\Twig_Token::NAME_TYPE, 'name')) { |
|
|
72 |
// name='core_js' |
|
|
73 |
$stream->next(); |
|
|
74 |
$stream->expect(\Twig_Token::OPERATOR_TYPE, '='); |
|
|
75 |
$name = $stream->expect(\Twig_Token::STRING_TYPE)->getValue(); |
|
|
76 |
} elseif ($stream->test(\Twig_Token::NAME_TYPE, 'as')) { |
|
|
77 |
// as='the_url' |
|
|
78 |
$stream->next(); |
|
|
79 |
$stream->expect(\Twig_Token::OPERATOR_TYPE, '='); |
|
|
80 |
$attributes['var_name'] = $stream->expect(\Twig_Token::STRING_TYPE)->getValue(); |
|
|
81 |
} elseif ($stream->test(\Twig_Token::NAME_TYPE, 'debug')) { |
|
|
82 |
// debug=true |
|
|
83 |
$stream->next(); |
|
|
84 |
$stream->expect(\Twig_Token::OPERATOR_TYPE, '='); |
|
|
85 |
$attributes['debug'] = 'true' == $stream->expect(\Twig_Token::NAME_TYPE, array('true', 'false'))->getValue(); |
|
|
86 |
} elseif ($stream->test(\Twig_Token::NAME_TYPE, 'combine')) { |
|
|
87 |
// combine=true |
|
|
88 |
$stream->next(); |
|
|
89 |
$stream->expect(\Twig_Token::OPERATOR_TYPE, '='); |
|
|
90 |
$attributes['combine'] = 'true' == $stream->expect(\Twig_Token::NAME_TYPE, array('true', 'false'))->getValue(); |
|
|
91 |
} elseif ($stream->test(\Twig_Token::NAME_TYPE, $this->extensions)) { |
|
|
92 |
// an arbitrary configured attribute |
|
|
93 |
$key = $stream->next()->getValue(); |
|
|
94 |
$stream->expect(\Twig_Token::OPERATOR_TYPE, '='); |
|
|
95 |
$attributes[$key] = $stream->expect(\Twig_Token::STRING_TYPE)->getValue(); |
|
|
96 |
} else { |
|
|
97 |
$token = $stream->getCurrent(); |
|
|
98 |
throw new \Twig_Error_Syntax(sprintf('Unexpected token "%s" of value "%s"', \Twig_Token::typeToEnglish($token->getType(), $token->getLine()), $token->getValue()), $token->getLine()); |
|
|
99 |
} |
|
|
100 |
} |
|
|
101 |
|
|
|
102 |
$stream->expect(\Twig_Token::BLOCK_END_TYPE); |
|
|
103 |
|
|
|
104 |
$endtag = 'end'.$this->getTag(); |
|
|
105 |
$test = function(\Twig_Token $token) use($endtag) { return $token->test($endtag); }; |
|
|
106 |
$body = $this->parser->subparse($test, true); |
|
|
107 |
|
|
|
108 |
$stream->expect(\Twig_Token::BLOCK_END_TYPE); |
|
|
109 |
|
|
|
110 |
if ($this->single && 1 < count($inputs)) { |
|
|
111 |
$inputs = array_slice($inputs, -1); |
|
|
112 |
} |
|
|
113 |
|
|
|
114 |
if (!$name) { |
|
|
115 |
$name = $this->factory->generateAssetName($inputs, $filters, $attributes); |
|
|
116 |
} |
|
|
117 |
|
|
|
118 |
$asset = $this->factory->createAsset($inputs, $filters, $attributes + array('name' => $name)); |
|
|
119 |
|
|
|
120 |
return $this->createNode($asset, $body, $inputs, $filters, $name, $attributes, $token->getLine(), $this->getTag()); |
|
|
121 |
} |
|
|
122 |
|
|
|
123 |
public function getTag() |
|
|
124 |
{ |
|
|
125 |
return $this->tag; |
|
|
126 |
} |
|
|
127 |
|
|
|
128 |
protected function createNode(AssetInterface $asset, \Twig_NodeInterface $body, array $inputs, array $filters, $name, array $attributes = array(), $lineno = 0, $tag = null) |
|
|
129 |
{ |
|
|
130 |
return new AsseticNode($asset, $body, $inputs, $filters, $name, $attributes, $lineno, $tag); |
|
|
131 |
} |
|
|
132 |
} |