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