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