3 namespace Rapsys\PackBundle\Parser
;
5 use Symfony\Component\Asset\PackageInterface
;
6 use Symfony\Component\Filesystem\Exception\IOExceptionInterface
;
7 use Symfony\Component\Filesystem\Filesystem
;
8 use Symfony\Component\HttpKernel\Config\FileLocator
;
11 use Twig\Node\Expression\AssignNameExpression
;
13 use Twig\Node\SetNode
;
14 use Twig\Node\TextNode
;
17 use Twig\TokenParser\AbstractTokenParser
;
19 class TokenParser
extends AbstractTokenParser
{
26 * @param FileLocator $locator The FileLocator instance
27 * @param PackageInterface $package The Assets Package instance
28 * @param array $config The config path
29 * @param string $tag The tag name
30 * @param string $output The default output string
31 * @param array $filters The default filters array
33 public function __construct(FileLocator
$locator, PackageInterface
$package, array $config, string $tag, string $output, array $filters) {
35 $this->locator
= $locator;
38 $this->package
= $package;
41 $this->name
= $config['name'];
44 $this->scheme
= $config['scheme'];
47 $this->timeout
= $config['timeout'];
50 $this->agent
= $config['agent'];
53 $this->redirect
= $config['redirect'];
59 $this->output
= $output;
62 $this->filters
= $filters;
68 * @return string This tag name
70 public function getTag(): string {
77 * @xxx Skip filter when debug mode is enabled is not possible
78 * @xxx This code is only run once when twig cache is enabled
79 * @xxx Twig cache value is not avaible in container parameters, maybe in twig env ?
81 * @param Token $token The \Twig\Token instance
82 * @return Node The PackNode
84 public function parse(Token
$token): Node
{
85 $parser = $this->parser
;
86 $stream = $this->parser
->getStream();
90 $output = $this->output
;
91 $filters = $this->filters
;
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();
102 } elseif ($stream->test(Token
::NAME_TYPE
, 'filters')) {
105 $stream->expect(Token
::OPERATOR_TYPE
, '=');
106 $filters = array_merge($filters, array_filter(array_map('trim', explode(',', $stream->expect(Token
::STRING_TYPE
)->getValue()))));
108 } elseif ($stream->test(Token
::NAME_TYPE
, 'output')) {
109 //output='js/packed/*.js' OR output='js/core.js'
111 $stream->expect(Token
::OPERATOR_TYPE
, '=');
112 $output = $stream->expect(Token
::STRING_TYPE
)->getValue();
114 } elseif ($stream->test(Token
::NAME_TYPE
, 'name')) {
117 $stream->expect(Token
::OPERATOR_TYPE
, '=');
118 $name = $stream->expect(Token
::STRING_TYPE
)->getValue();
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());
127 $stream->expect(Token
::BLOCK_END_TYPE
);
130 $body = $this->parser
->subparse([$this, 'testEndTag'], true);
133 $stream->expect(Token
::BLOCK_END_TYPE
);
135 //Replace star with sha1
136 if (($pos = strpos($output, '*')) !== false) {
137 //XXX: assetic use substr(sha1(serialize($inputs).serialize($filters).serialize($options)), 0, 7)
138 $output = substr($output, 0, $pos).sha1(serialize($inputs).serialize($filters)).substr($output, $pos +
1);
142 for($k = 0; $k < count($inputs); $k++
) {
143 //Deal with generic url
144 if (strpos($inputs[$k], '//') === 0) {
146 $inputs[$k] = $this->scheme
.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] == '@') {
152 $inputs[$k] = $this->getLocated($inputs[$k], $token->getLine(), $stream->getSourceContext());
156 if (strpos($inputs[$k], '*') !== false || (($a = strpos($inputs[$k], '{')) !== false && ($b = strpos($inputs[$k], ',', $a)) !== false && strpos($inputs[$k], '}', $b) !== false)) {
158 $replacement = glob($inputs[$k], GLOB_NOSORT
|GLOB_BRACE
);
159 //Check that these are working files
160 foreach($replacement as $input) {
161 //Check that it's a file
162 if (!is_file($input)) {
163 throw new Error(sprintf('Input path "%s" from "%s" is not a file', $input, $inputs[$k]), $token->getLine(), $stream->getSourceContext());
166 //Replace with glob path
167 array_splice($inputs, $k, 1, $replacement);
169 $k +
= count($replacement) - 1;
170 //Check that it's a file
171 } elseif (!is_file($inputs[$k])) {
172 throw new Error(sprintf('Input path "%s" is not a file', $inputs[$k]), $token->getLine(), $stream->getSourceContext());
178 $ctx = stream_context_create(
181 'timeout' => $this->timeout
,
182 'user_agent' => $this->agent
,
183 'redirect' => $this->redirect
,
189 if (!empty($inputs)) {
190 //Retrieve files content
191 foreach($inputs as $input) {
192 //Try to retrieve content
193 if (($data = file_get_contents($input, false, $ctx)) === false) {
194 throw new Error(sprintf('Unable to retrieve input path "%s"', $input), $token->getLine(), $stream->getSourceContext());
200 //Trigger error about empty inputs ?
201 //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
202 #throw new Error('Empty inputs token', $token->getLine(), $stream->getSourceContext());
204 //Send an empty node without inputs
209 if (!empty($filters)) {
211 foreach($filters as $filter) {
213 $args = [$stream->getSourceContext(), $token->getLine()];
214 //Check if args is available
215 if (!empty($filter['args'])) {
216 //Append args if provided
217 $args +
= $filter['args'];
220 $reflection = new \
ReflectionClass($filter['class']);
222 $tool = $reflection->newInstanceArgs($args);
224 $content = $tool->process($content);
226 unset($tool, $reflection);
229 //Trigger error about empty filters ?
230 //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
231 #throw new Error('Empty filters token', $token->getLine(), $stream->getSourceContext());
235 //XXX: this path is the merge of services.assets.path_package.arguments[0] and rapsys_pack.output.(css,img,js)
236 if (($outputUrl = $this->package
->getUrl($output)) === false) {
237 throw new Error(sprintf('Unable to get url for asset: %s', $output), $token->getLine(), $stream->getSourceContext());
240 //Check if we have a bundle path
241 if ($output[0] == '@') {
243 $output = $this->getLocated($output, $token->getLine(), $stream->getSourceContext());
247 $filesystem = new Filesystem();
249 //Create output dir if not present
250 if (!is_dir($dir = dirname($output))) {
253 //XXX: set as 0775, symfony umask (0022) will reduce rights (0755)
254 $filesystem->mkdir($dir, 0775);
255 } catch (IOExceptionInterface
$e) {
257 throw new Error(sprintf('Output directory "%s" do not exists and unable to create it', $dir), $token->getLine(), $stream->getSourceContext(), $e);
263 //Write content to file
264 //XXX: this call is (maybe) atomic
265 //XXX: see https://symfony.com/doc/current/components/filesystem.html#dumpfile
266 $filesystem->dumpFile($output, $content);
267 } catch (IOExceptionInterface
$e) {
269 throw new Error(sprintf('Unable to write to: %s', $output), $token->getLine(), $stream->getSourceContext(), $e);
272 //Set name in context key
273 $ref = new AssignNameExpression($name, $token->getLine());
275 //Set output in context value
276 $value = new TextNode($outputUrl, $token->getLine());
278 //Send body with context set
280 //This define name in twig template by prepending $context['<name>'] = '<output>';
281 new SetNode(true, $ref, $value, $token->getLine(), $this->getTag()),
282 //The tag captured body
290 * @param Token $token The \Twig\Token instance
291 * @return bool The token end test result
293 public function testEndTag(Token
$token): bool {
294 return $token->test(['end'.$this->getTag()]);
298 * Get path from bundled file
300 * @see https://symfony.com/doc/current/bundles.html#overridding-the-bundle-directory-structure
302 * @param string $file The bundled file path
303 * @param int $lineno The template line where the error occurred
304 * @param Source $source The source context where the error occurred
305 * @param Exception $prev The previous exception
306 * @return string The resolved file path
308 public function getLocated(string $file, int $lineno = 0, Source
$source = null, \Exception
$prev = null): string {
309 /*TODO: add a @jquery magic feature ?
310 if ($file == '@jquery') {
311 #header('Content-Type: text/plain');
314 return $this->config['jquery'];
317 //Check that we have a / separator between bundle name and path
318 if (($pos = strpos($file, '/')) === false) {
319 throw new Error(sprintf('Invalid path "%s"', $file), $token->getLine(), $stream->getSourceContext());
323 $bundle = substr($file, 0, $pos);
326 $path = substr($file, $pos +
1);
328 //Check for bundle suffix presence
329 //XXX: use "bundle templates automatic namespace" mimicked behaviour to find intended bundle and/or path
330 //XXX: see https://symfony.com/doc/4.3/templates.html#bundle-templates
331 if (strlen($bundle) < strlen('Bundle') || substr($bundle, -strlen('Bundle')) !== 'Bundle') {
332 //Append Bundle in an attempt to fix it's naming for locator
335 //Check for public resource prefix presence
336 if (strlen($path) < strlen('Resources/public') || substr($path, 0, strlen('Resources/public')) != 'Resources/public') {
337 //Prepend standard public path
338 $path = 'Resources/public/'.$path;
342 //Resolve bundle prefix
344 $prefix = $this->locator
->locate($bundle);
345 //Catch bundle does not exist or is not enabled exception
346 } catch(\InvalidArgumentException
$e) {
347 //Fix lowercase first bundle character
348 if ($bundle[1] > 'Z' || $bundle[1] < 'A') {
349 $bundle[1] = strtoupper($bundle[1]);
352 //Detect double bundle suffix
353 if (strlen($bundle) > strlen('_bundleBundle') && substr($bundle, -strlen('_bundleBundle')) == '_bundleBundle') {
355 $bundle = substr($bundle, 0, -strlen('Bundle'));
358 //Convert snake case in camel case
359 if (strpos($bundle, '_') !== false) {
360 //Fix every first character following a _
361 while(($cur = strpos($bundle, '_')) !== false) {
362 $bundle = substr($bundle, 0, $cur).ucfirst(substr($bundle, $cur +
1));
366 //Resolve fixed bundle prefix
368 $prefix = $this->locator
->locate($bundle);
369 //Catch bundle does not exist or is not enabled exception again
370 } catch(\InvalidArgumentException
$e) {
371 //Bail out as bundle or path is invalid and we have no way to know what was meant
372 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);
376 //Return solved bundle prefix and path
377 return $prefix.$path;