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