]> Raphaël G. Git Repositories - packbundle/blob - Parser/TokenParser.php
Add to constructor font, size, width, fill, stroke and align arguments
[packbundle] / Parser / TokenParser.php
1 <?php declare(strict_types=1);
2
3 /*
4 * This file is part of the Rapsys PackBundle package.
5 *
6 * (c) Raphaël Gertz <symfony@rapsys.eu>
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 Rapsys\PackBundle\Parser;
13
14 use Symfony\Component\Asset\PackageInterface;
15 use Symfony\Component\Filesystem\Exception\IOExceptionInterface;
16 use Symfony\Component\Filesystem\Filesystem;
17 use Symfony\Component\HttpKernel\Config\FileLocator;
18
19 use Twig\Error\Error;
20 use Twig\Node\Expression\AssignNameExpression;
21 use Twig\Node\Node;
22 use Twig\Node\SetNode;
23 use Twig\Node\TextNode;
24 use Twig\Source;
25 use Twig\Token;
26 use Twig\TokenParser\AbstractTokenParser;
27
28 class TokenParser extends AbstractTokenParser {
29 ///The locator
30 protected $locator;
31
32 ///The package
33 protected $package;
34
35 ///The name
36 protected $name;
37
38 ///The scheme
39 protected $scheme;
40
41 ///The timeout
42 protected $timeout;
43
44 ///The agent
45 protected $agent;
46
47 ///The redirect
48 protected $redirect;
49
50 ///The tag name
51 protected $tag;
52
53 ///The output
54 protected $output;
55
56 ///The filters
57 protected $filters;
58
59 /**
60 * Constructor
61 *
62 * @param FileLocator $locator The FileLocator instance
63 * @param PackageInterface $package The Assets Package instance
64 * @param array $config The config path
65 * @param string $tag The tag name
66 * @param string $output The default output string
67 * @param array $filters The default filters array
68 */
69 public function __construct(FileLocator $locator, PackageInterface $package, array $config, string $tag, string $output, array $filters) {
70 //Save locator
71 $this->locator = $locator;
72
73 //Save assets package
74 $this->package = $package;
75
76 //Set name
77 $this->name = $config['name'];
78
79 //Set scheme
80 $this->scheme = $config['scheme'];
81
82 //Set timeout
83 $this->timeout = $config['timeout'];
84
85 //Set agent
86 $this->agent = $config['agent'];
87
88 //Set redirect
89 $this->redirect = $config['redirect'];
90
91 //Set tag
92 $this->tag = $tag;
93
94 //Set output
95 $this->output = $output;
96
97 //Set filters
98 $this->filters = $filters;
99 }
100
101 /**
102 * Get the tag name
103 *
104 * @return string This tag name
105 */
106 public function getTag(): string {
107 return $this->tag;
108 }
109
110 /**
111 * Parse the token
112 *
113 * @xxx Skip filter when debug mode is enabled is not possible
114 * @xxx This code is only run once when twig cache is enabled
115 * @xxx Twig cache value is not avaible in container parameters, maybe in twig env ?
116 *
117 * @param Token $token The \Twig\Token instance
118 * @return Node The PackNode
119 */
120 public function parse(Token $token): Node {
121 $parser = $this->parser;
122 $stream = $this->parser->getStream();
123
124 $inputs = [];
125 $name = $this->name;
126 $output = $this->output;
127 $filters = $this->filters;
128
129 $content = '';
130
131 //Process the token block until end
132 while (!$stream->test(Token::BLOCK_END_TYPE)) {
133 //The files to process
134 if ($stream->test(Token::STRING_TYPE)) {
135 //'somewhere/somefile.(css,img,js)' 'somewhere/*' '@jquery'
136 $inputs[] = $stream->next()->getValue();
137 //The filters token
138 } elseif ($stream->test(Token::NAME_TYPE, 'filters')) {
139 //filter='yui_js'
140 $stream->next();
141 $stream->expect(Token::OPERATOR_TYPE, '=');
142 $filters = array_merge($filters, array_filter(array_map('trim', explode(',', $stream->expect(Token::STRING_TYPE)->getValue()))));
143 //The output token
144 } elseif ($stream->test(Token::NAME_TYPE, 'output')) {
145 //output='js/packed/*.js' OR output='js/core.js'
146 $stream->next();
147 $stream->expect(Token::OPERATOR_TYPE, '=');
148 $output = $stream->expect(Token::STRING_TYPE)->getValue();
149 //The name token
150 } elseif ($stream->test(Token::NAME_TYPE, 'name')) {
151 //name='core_js'
152 $stream->next();
153 $stream->expect(Token::OPERATOR_TYPE, '=');
154 $name = $stream->expect(Token::STRING_TYPE)->getValue();
155 //Unexpected token
156 } else {
157 $token = $stream->getCurrent();
158 throw new Error(sprintf('Unexpected token "%s" of value "%s"', Token::typeToEnglish($token->getType()), $token->getValue()), $token->getLine(), $stream->getSourceContext());
159 }
160 }
161
162 //Process end block
163 $stream->expect(Token::BLOCK_END_TYPE);
164
165 //Process body
166 $body = $this->parser->subparse([$this, 'testEndTag'], true);
167
168 //Process end block
169 $stream->expect(Token::BLOCK_END_TYPE);
170
171 //Replace star with sha1
172 if (($pos = strpos($output, '*')) !== false) {
173 //XXX: assetic use substr(sha1(serialize($inputs).serialize($filters).serialize($options)), 0, 7)
174 $output = substr($output, 0, $pos).sha1(serialize($inputs).serialize($filters)).substr($output, $pos + 1);
175 }
176
177 //Process inputs
178 for($k = 0; $k < count($inputs); $k++) {
179 //Deal with generic url
180 if (strpos($inputs[$k], '//') === 0) {
181 //Fix url
182 $inputs[$k] = $this->scheme.substr($inputs[$k], 2);
183 //Deal with non url path
184 } elseif (strpos($inputs[$k], '://') === false) {
185 //Check if we have a bundle path
186 if ($inputs[$k][0] == '@') {
187 //Resolve it
188 $inputs[$k] = $this->getLocated($inputs[$k], $token->getLine(), $stream->getSourceContext());
189 }
190
191 //Deal with globs
192 if (strpos($inputs[$k], '*') !== false || (($a = strpos($inputs[$k], '{')) !== false && ($b = strpos($inputs[$k], ',', $a)) !== false && strpos($inputs[$k], '}', $b) !== false)) {
193 //Get replacement
194 $replacement = glob($inputs[$k], GLOB_NOSORT|GLOB_BRACE);
195 //Check that these are working files
196 foreach($replacement as $input) {
197 //Check that it's a file
198 if (!is_file($input)) {
199 throw new Error(sprintf('Input path "%s" from "%s" is not a file', $input, $inputs[$k]), $token->getLine(), $stream->getSourceContext());
200 }
201 }
202 //Replace with glob path
203 array_splice($inputs, $k, 1, $replacement);
204 //Fix current key
205 $k += count($replacement) - 1;
206 //Check that it's a file
207 } elseif (!is_file($inputs[$k])) {
208 throw new Error(sprintf('Input path "%s" is not a file', $inputs[$k]), $token->getLine(), $stream->getSourceContext());
209 }
210 }
211 }
212
213 //Init context
214 $ctx = stream_context_create(
215 [
216 'http' => [
217 'timeout' => $this->timeout,
218 'user_agent' => $this->agent,
219 'redirect' => $this->redirect,
220 ]
221 ]
222 );
223
224 //Check inputs
225 if (!empty($inputs)) {
226 //Retrieve files content
227 foreach($inputs as $input) {
228 //Try to retrieve content
229 if (($data = file_get_contents($input, false, $ctx)) === false) {
230 throw new Error(sprintf('Unable to retrieve input path "%s"', $input), $token->getLine(), $stream->getSourceContext());
231 }
232 //Append content
233 $content .= $data;
234 }
235 } else {
236 //Trigger error about empty inputs ?
237 //XXX: There may be a legitimate case where we want an empty file or an error, feel free to contact the author in such case
238 #throw new Error('Empty inputs token', $token->getLine(), $stream->getSourceContext());
239
240 //Send an empty node without inputs
241 return new Node();
242 }
243
244 //Check filters
245 if (!empty($filters)) {
246 //Apply all filters
247 foreach($filters as $filter) {
248 //Init args
249 $args = [$stream->getSourceContext(), $token->getLine()];
250 //Check if args is available
251 if (!empty($filter['args'])) {
252 //Append args if provided
253 $args += $filter['args'];
254 }
255 //Init reflection
256 $reflection = new \ReflectionClass($filter['class']);
257 //Set instance args
258 $tool = $reflection->newInstanceArgs($args);
259 //Process content
260 $content = $tool->process($content);
261 //Remove object
262 unset($tool, $reflection);
263 }
264 } else {
265 //Trigger error about empty filters ?
266 //XXX: There may be a legitimate case where we want only a merged file or an error, feel free to contact the author in such case
267 #throw new Error('Empty filters token', $token->getLine(), $stream->getSourceContext());
268 }
269
270 //Retrieve asset uri
271 //XXX: this path is the merge of services.assets.path_package.arguments[0] and rapsys_pack.output.(css,img,js)
272 if (($outputUrl = $this->package->getUrl($output)) === false) {
273 throw new Error(sprintf('Unable to get url for asset: %s', $output), $token->getLine(), $stream->getSourceContext());
274 }
275
276 //Check if we have a bundle path
277 if ($output[0] == '@') {
278 //Resolve it
279 $output = $this->getLocated($output, $token->getLine(), $stream->getSourceContext());
280 }
281
282 //Get filesystem
283 $filesystem = new Filesystem();
284
285 //Create output dir if not present
286 if (!is_dir($dir = dirname($output))) {
287 try {
288 //Create dir
289 //XXX: set as 0775, symfony umask (0022) will reduce rights (0755)
290 $filesystem->mkdir($dir, 0775);
291 } catch (IOExceptionInterface $e) {
292 //Throw error
293 throw new Error(sprintf('Output directory "%s" do not exists and unable to create it', $dir), $token->getLine(), $stream->getSourceContext(), $e);
294 }
295 }
296
297 //Send file content
298 try {
299 //Write content to file
300 //XXX: this call is (maybe) atomic
301 //XXX: see https://symfony.com/doc/current/components/filesystem.html#dumpfile
302 $filesystem->dumpFile($output, $content);
303 } catch (IOExceptionInterface $e) {
304 //Throw error
305 throw new Error(sprintf('Unable to write to: %s', $output), $token->getLine(), $stream->getSourceContext(), $e);
306 }
307
308 //Set name in context key
309 $ref = new AssignNameExpression($name, $token->getLine());
310
311 //Set output in context value
312 $value = new TextNode($outputUrl, $token->getLine());
313
314 //Send body with context set
315 return new Node([
316 //This define name in twig template by prepending $context['<name>'] = '<output>';
317 new SetNode(true, $ref, $value, $token->getLine(), $this->getTag()),
318 //The tag captured body
319 $body
320 ]);
321 }
322
323 /**
324 * Test for tag end
325 *
326 * @param Token $token The \Twig\Token instance
327 * @return bool The token end test result
328 */
329 public function testEndTag(Token $token): bool {
330 return $token->test(['end'.$this->getTag()]);
331 }
332
333 /**
334 * Get path from bundled file
335 *
336 * @see https://symfony.com/doc/current/bundles.html#overridding-the-bundle-directory-structure
337 *
338 * @param string $file The bundled file path
339 * @param int $lineno The template line where the error occurred
340 * @param Source $source The source context where the error occurred
341 * @param Exception $prev The previous exception
342 * @return string The resolved file path
343 */
344 public function getLocated(string $file, int $lineno = 0, Source $source = null, \Exception $prev = null): string {
345 /*TODO: add a @jquery magic feature ?
346 if ($file == '@jquery') {
347 #header('Content-Type: text/plain');
348 #var_dump($inputs);
349 #exit;
350 return $this->config['jquery'];
351 }*/
352
353 //Check that we have a / separator between bundle name and path
354 if (($pos = strpos($file, '/')) === false) {
355 throw new Error(sprintf('Invalid path "%s"', $file), $token->getLine(), $stream->getSourceContext());
356 }
357
358 //Set bundle
359 $bundle = substr($file, 0, $pos);
360
361 //Set path
362 $path = substr($file, $pos + 1);
363
364 //Check for bundle suffix presence
365 //XXX: use "bundle templates automatic namespace" mimicked behaviour to find intended bundle and/or path
366 //XXX: see https://symfony.com/doc/4.3/templates.html#bundle-templates
367 if (strlen($bundle) < strlen('Bundle') || substr($bundle, -strlen('Bundle')) !== 'Bundle') {
368 //Append Bundle in an attempt to fix it's naming for locator
369 $bundle .= 'Bundle';
370
371 //Check for public resource prefix presence
372 if (strlen($path) < strlen('Resources/public') || substr($path, 0, strlen('Resources/public')) != 'Resources/public') {
373 //Prepend standard public path
374 $path = 'Resources/public/'.$path;
375 }
376 }
377
378 //Resolve bundle prefix
379 try {
380 $prefix = $this->locator->locate($bundle);
381 //Catch bundle does not exist or is not enabled exception
382 } catch(\InvalidArgumentException $e) {
383 //Fix lowercase first bundle character
384 if ($bundle[1] > 'Z' || $bundle[1] < 'A') {
385 $bundle[1] = strtoupper($bundle[1]);
386 }
387
388 //Detect double bundle suffix
389 if (strlen($bundle) > strlen('_bundleBundle') && substr($bundle, -strlen('_bundleBundle')) == '_bundleBundle') {
390 //Strip extra bundle
391 $bundle = substr($bundle, 0, -strlen('Bundle'));
392 }
393
394 //Convert snake case in camel case
395 if (strpos($bundle, '_') !== false) {
396 //Fix every first character following a _
397 while(($cur = strpos($bundle, '_')) !== false) {
398 $bundle = substr($bundle, 0, $cur).ucfirst(substr($bundle, $cur + 1));
399 }
400 }
401
402 //Resolve fixed bundle prefix
403 try {
404 $prefix = $this->locator->locate($bundle);
405 //Catch bundle does not exist or is not enabled exception again
406 } catch(\InvalidArgumentException $e) {
407 //Bail out as bundle or path is invalid and we have no way to know what was meant
408 throw new Error(sprintf('Invalid bundle name "%s" in path "%s". Maybe you meant "%s"', substr($file, 1, $pos - 1), $file, $bundle.'/'.$path), $token->getLine(), $stream->getSourceContext(), $e);
409 }
410 }
411
412 //Return solved bundle prefix and path
413 return $prefix.$path;
414 }
415 }