]> Raphaël G. Git Repositories - packbundle/blobdiff - Parser/TokenParser.php
Define member variables
[packbundle] / Parser / TokenParser.php
index 7749ad28070bdc3bc4dbeaae5ed3dc5b1ae5d509..7d73117b432129a70fbfbfcb6f2972530846de40 100644 (file)
@@ -1,11 +1,29 @@
-<?php
+<?php declare(strict_types=1);
+
+/*
+ * This file is part of the Rapsys PackBundle package.
+ *
+ * (c) Raphaël Gertz <symfony@rapsys.eu>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
 
 namespace Rapsys\PackBundle\Parser;
 
+use Psr\Container\ContainerInterface;
+
+use Rapsys\PackBundle\RapsysPackBundle;
+use Rapsys\PackBundle\Util\SluggerUtil;
+
 use Symfony\Component\Asset\PackageInterface;
 use Symfony\Component\Filesystem\Exception\IOExceptionInterface;
 use Symfony\Component\Filesystem\Filesystem;
 use Symfony\Component\HttpKernel\Config\FileLocator;
+use Symfony\Component\Routing\Exception\InvalidParameterException;
+use Symfony\Component\Routing\Exception\MissingMandatoryParametersException;
+use Symfony\Component\Routing\Exception\RouteNotFoundException;
+use Symfony\Component\Routing\RouterInterface;
 
 use Twig\Error\Error;
 use Twig\Node\Expression\AssignNameExpression;
@@ -16,50 +34,54 @@ use Twig\Source;
 use Twig\Token;
 use Twig\TokenParser\AbstractTokenParser;
 
+/**
+ * {@inheritdoc}
+ */
 class TokenParser extends AbstractTokenParser {
-       ///The tag name
-       protected $tag;
+       /**
+        * Filters array
+        */
+       protected array $filters;
+
+       /**
+        * Output string
+        */
+       protected string $output;
+
+       /**
+        * Route string
+        */
+       protected string $route;
+
+       /**
+        * Token string
+        */
+       protected string $token;
 
        /**
         * Constructor
         *
+        * @param ContainerInterface $container The ContainerInterface instance
         * @param FileLocator $locator The FileLocator instance
-        * @param PackageInterface $package The Assets Package instance
-        * @param array $config The config path
+        * @param RouterInterface $router The RouterInterface instance
+        * @param SluggerUtil $slugger The SluggerUtil instance
+        * @param array $config The config
+        * @param mixed $ctx The context stream instance
+        * @param string $prefix The output prefix
         * @param string $tag The tag name
-        * @param string $output The default output string
-        * @param array $filters The default filters array
         */
-       public function __construct(FileLocator $locator, PackageInterface $package, array $config, string $tag, string $output, array $filters) {
-               //Save locator
-               $this->locator = $locator;
-
-               //Save assets package
-               $this->package = $package;
-
-               //Set name
-               $this->name = $config['name'];
-
-               //Set scheme
-               $this->scheme = $config['scheme'];
-
-               //Set timeout
-               $this->timeout = $config['timeout'];
-
-               //Set agent
-               $this->agent = $config['agent'];
-
-               //Set redirect
-               $this->redirect = $config['redirect'];
-
-               //Set tag
-               $this->tag = $tag;
+       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) {
+               //Set filters
+               $this->filters = $config['filters'][$prefix];
 
                //Set output
-               $this->output = $output;
+               $this->output = $config['public'].'/'.$config['prefixes']['pack'].'/'.$config['prefixes'][$prefix].'/*.'.$prefix;
 
-               //Set filters
-               $this->filters = $filters;
+               //Set route
+               $this->route = $config['routes'][$prefix];
+
+               //Set token
+               $this->token = $config['tokens'][$prefix];
        }
 
        /**
@@ -82,43 +104,53 @@ class TokenParser extends AbstractTokenParser {
         * @return Node The PackNode
         */
        public function parse(Token $token): Node {
+               //Get parser
                $parser = $this->parser;
+
+               //Get parser stream
                $stream = $this->parser->getStream();
 
+               //Set inputs array
                $inputs = [];
-               $name = $this->name;
-               $output = $this->output;
-               $filters = $this->filters;
 
+               //Set content
                $content = '';
 
                //Process the token block until end
                while (!$stream->test(Token::BLOCK_END_TYPE)) {
                        //The files to process
                        if ($stream->test(Token::STRING_TYPE)) {
-                               //'somewhere/somefile.(css,img,js)' 'somewhere/*' '@jquery'
+                               //'somewhere/somefile.(css|img|js)' 'somewhere/*' '@jquery'
                                $inputs[] = $stream->next()->getValue();
                        //The filters token
                        } elseif ($stream->test(Token::NAME_TYPE, 'filters')) {
                                //filter='yui_js'
                                $stream->next();
                                $stream->expect(Token::OPERATOR_TYPE, '=');
-                               $filters = array_merge($filters, array_filter(array_map('trim', explode(',', $stream->expect(Token::STRING_TYPE)->getValue()))));
+                               $this->filters = array_merge($this->filters, array_filter(array_map('trim', explode(',', $stream->expect(Token::STRING_TYPE)->getValue()))));
+                       //The route token
+                       } elseif ($stream->test(Token::NAME_TYPE, 'route')) {
+                               //output='rapsyspack_css' OR output='rapsyspack_js' OR output='rapsyspack_img'
+                               $stream->next();
+                               $stream->expect(Token::OPERATOR_TYPE, '=');
+                               $this->route = $stream->expect(Token::STRING_TYPE)->getValue();
                        //The output token
                        } elseif ($stream->test(Token::NAME_TYPE, 'output')) {
                                //output='js/packed/*.js' OR output='js/core.js'
                                $stream->next();
                                $stream->expect(Token::OPERATOR_TYPE, '=');
-                               $output = $stream->expect(Token::STRING_TYPE)->getValue();
-                       //The name token
-                       } elseif ($stream->test(Token::NAME_TYPE, 'name')) {
+                               $this->output = $stream->expect(Token::STRING_TYPE)->getValue();
+                       //TODO: add format ? jpeg|png|gif|webp|webm ???
+                       //The token name
+                       } elseif ($stream->test(Token::NAME_TYPE, 'token')) {
                                //name='core_js'
                                $stream->next();
                                $stream->expect(Token::OPERATOR_TYPE, '=');
-                               $name = $stream->expect(Token::STRING_TYPE)->getValue();
+                               $this->token = $stream->expect(Token::STRING_TYPE)->getValue();
                        //Unexpected token
                        } else {
                                $token = $stream->getCurrent();
+                               //Throw error
                                throw new Error(sprintf('Unexpected token "%s" of value "%s"', Token::typeToEnglish($token->getType()), $token->getValue()), $token->getLine(), $stream->getSourceContext());
                        }
                }
@@ -132,18 +164,31 @@ class TokenParser extends AbstractTokenParser {
                //Process end block
                $stream->expect(Token::BLOCK_END_TYPE);
 
-               //Replace star with sha1
-               if (($pos = strpos($output, '*')) !== false) {
-                       //XXX: assetic use substr(sha1(serialize($inputs).serialize($filters).serialize($options)), 0, 7)
-                       $output = substr($output, 0, $pos).sha1(serialize($inputs).serialize($filters)).substr($output, $pos + 1);
+               //Without valid output
+               if (($pos = strpos($this->output, '*')) === false || $pos !== strrpos($this->output, '*')) {
+                       //Throw error
+                       throw new Error(sprintf('Invalid output "%s"', $this->output), $token->getLine(), $stream->getSourceContext());
+               }
+
+               //Without existing route
+               if ($this->router->getRouteCollection()->get($this->route) === null) {
+                       //Throw error
+                       throw new Error(sprintf('Invalid route "%s"', $this->route), $token->getLine(), $stream->getSourceContext());
                }
 
+               //Set file
+               //XXX: assetic use substr(sha1(serialize($inputs).serialize($this->filters).serialize($this->output)), 0, 7)
+               $file = $this->slugger->hash([$inputs, $this->filters, $this->output, $this->route, $this->token]);
+
+               //Replace star by file
+               $this->output = substr($this->output, 0, $pos).$file.substr($this->output, $pos + 1);
+
                //Process inputs
                for($k = 0; $k < count($inputs); $k++) {
                        //Deal with generic url
                        if (strpos($inputs[$k], '//') === 0) {
                                //Fix url
-                               $inputs[$k] = $this->scheme.substr($inputs[$k], 2);
+                               $inputs[$k] = ($_ENV['RAPSYSPACK_SCHEME'] ?? 'https').'://'.substr($inputs[$k], 2);
                        //Deal with non url path
                        } elseif (strpos($inputs[$k], '://') === false) {
                                //Check if we have a bundle path
@@ -156,43 +201,41 @@ class TokenParser extends AbstractTokenParser {
                                if (strpos($inputs[$k], '*') !== false || (($a = strpos($inputs[$k], '{')) !== false && ($b = strpos($inputs[$k], ',', $a)) !== false && strpos($inputs[$k], '}', $b) !== false)) {
                                        //Get replacement
                                        $replacement = glob($inputs[$k], GLOB_NOSORT|GLOB_BRACE);
+
                                        //Check that these are working files
                                        foreach($replacement as $input) {
                                                //Check that it's a file
                                                if (!is_file($input)) {
+                                                       //Throw error
                                                        throw new Error(sprintf('Input path "%s" from "%s" is not a file', $input, $inputs[$k]), $token->getLine(), $stream->getSourceContext());
                                                }
                                        }
+
                                        //Replace with glob path
                                        array_splice($inputs, $k, 1, $replacement);
+
                                        //Fix current key
                                        $k += count($replacement) - 1;
                                //Check that it's a file
                                } elseif (!is_file($inputs[$k])) {
+                                       //Throw error
                                        throw new Error(sprintf('Input path "%s" is not a file', $inputs[$k]), $token->getLine(), $stream->getSourceContext());
                                }
                        }
                }
 
-               //Init context
-               $ctx = stream_context_create(
-                       [
-                               'http' => [
-                                       'timeout' => $this->timeout,
-                                       'user_agent' => $this->agent,
-                                       'redirect' => $this->redirect,
-                               ]
-                       ]
-               );
+               #TODO: move the inputs reading from here to inside the filters ?
 
                //Check inputs
                if (!empty($inputs)) {
                        //Retrieve files content
                        foreach($inputs as $input) {
                                //Try to retrieve content
-                               if (($data = file_get_contents($input, false, $ctx)) === false) {
+                               if (($data = file_get_contents($input, false, $this->ctx)) === false) {
+                                       //Throw error
                                        throw new Error(sprintf('Unable to retrieve input path "%s"', $input), $token->getLine(), $stream->getSourceContext());
                                }
+
                                //Append content
                                $content .= $data;
                        }
@@ -206,22 +249,27 @@ class TokenParser extends AbstractTokenParser {
                }
 
                //Check filters
-               if (!empty($filters)) {
+               if (!empty($this->filters)) {
                        //Apply all filters
-                       foreach($filters as $filter) {
+                       foreach($this->filters as $filter) {
                                //Init args
                                $args = [$stream->getSourceContext(), $token->getLine()];
+
                                //Check if args is available
                                if (!empty($filter['args'])) {
                                        //Append args if provided
                                        $args += $filter['args'];
                                }
+
                                //Init reflection
                                $reflection = new \ReflectionClass($filter['class']);
+
                                //Set instance args
                                $tool = $reflection->newInstanceArgs($args);
+
                                //Process content
                                $content = $tool->process($content);
+
                                //Remove object
                                unset($tool, $reflection);
                        }
@@ -231,23 +279,17 @@ class TokenParser extends AbstractTokenParser {
                        #throw new Error('Empty filters token', $token->getLine(), $stream->getSourceContext());
                }
 
-               //Retrieve asset uri
-               //XXX: this path is the merge of services.assets.path_package.arguments[0] and rapsys_pack.output.(css,img,js)
-               if (($outputUrl = $this->package->getUrl($output)) === false) {
-                       throw new Error(sprintf('Unable to get url for asset: %s', $output), $token->getLine(), $stream->getSourceContext());
-               }
-
                //Check if we have a bundle path
-               if ($output[0] == '@') {
+               if ($this->output[0] == '@') {
                        //Resolve it
-                       $output = $this->getLocated($output, $token->getLine(), $stream->getSourceContext());
+                       $this->output = $this->getLocated($this->output, $token->getLine(), $stream->getSourceContext());
                }
 
                //Get filesystem
                $filesystem = new Filesystem();
 
                //Create output dir if not present
-               if (!is_dir($dir = dirname($output))) {
+               if (!is_dir($dir = dirname($this->output))) {
                        try {
                                //Create dir
                                //XXX: set as 0775, symfony umask (0022) will reduce rights (0755)
@@ -263,17 +305,34 @@ class TokenParser extends AbstractTokenParser {
                        //Write content to file
                        //XXX: this call is (maybe) atomic
                        //XXX: see https://symfony.com/doc/current/components/filesystem.html#dumpfile
-                       $filesystem->dumpFile($output, $content);
+                       $filesystem->dumpFile($this->output, $content);
                } catch (IOExceptionInterface $e) {
                        //Throw error
-                       throw new Error(sprintf('Unable to write to: %s', $output), $token->getLine(), $stream->getSourceContext(), $e);
+                       throw new Error(sprintf('Unable to write "%s"', $this->output), $token->getLine(), $stream->getSourceContext(), $e);
+               }
+
+               //Without output file mtime
+               if (($mtime = filemtime($this->output)) === false) {
+                       //Throw error
+                       throw new Error(sprintf('Unable to get "%s" mtime', $this->output), $token->getLine(), $stream->getSourceContext(), $e);
+               }
+
+               //TODO: get mimetype for images ? and set _format ?
+
+               try {
+                       //Generate asset url
+                       $asset = $this->router->generate($this->route, [ 'file' => $file, 'u' => $mtime ]);
+               //Catch router exceptions
+               } catch (RouteNotFoundException|MissingMandatoryParametersException|InvalidParameterException $e) {
+                       //Throw error
+                       throw new Error(sprintf('Unable to generate asset route "%s"', $this->route), $token->getLine(), $stream->getSourceContext(), $e);
                }
 
                //Set name in context key
-               $ref = new AssignNameExpression($name, $token->getLine());
+               $ref = new AssignNameExpression($this->token, $token->getLine());
 
                //Set output in context value
-               $value = new TextNode($outputUrl, $token->getLine());
+               $value = new TextNode($asset, $token->getLine());
 
                //Send body with context set
                return new Node([
@@ -305,7 +364,7 @@ class TokenParser extends AbstractTokenParser {
         * @param Exception $prev The previous exception
         * @return string The resolved file path
         */
-       public function getLocated(string $file, int $lineno = 0, Source $source = null, \Exception $prev = null): string {
+       public function getLocated(string $file, int $lineno = 0, ?Source $source = null, ?\Exception $prev = null): string {
                /*TODO: add a @jquery magic feature ?
                if ($file == '@jquery') {
                        #header('Content-Type: text/plain');
@@ -314,62 +373,50 @@ class TokenParser extends AbstractTokenParser {
                        return $this->config['jquery'];
                }*/
 
-               //Check that we have a / separator between bundle name and path
-               if (($pos = strpos($file, '/')) === false) {
-                       throw new Error(sprintf('Invalid path "%s"', $file), $token->getLine(), $stream->getSourceContext());
+               //Extract bundle
+               if (($bundle = strstr($file, '/', true)) === false) {
+                       throw new Error(sprintf('Invalid bundle "%s"', $file), $lineno, $source);
                }
 
-               //Set bundle
-               $bundle = substr($file, 0, $pos);
-
-               //Set path
-               $path = substr($file, $pos + 1);
-
-               //Check for bundle suffix presence
-               //XXX: use "bundle templates automatic namespace" mimicked behaviour to find intended bundle and/or path
-               //XXX: see https://symfony.com/doc/4.3/templates.html#bundle-templates
-               if (strlen($bundle) < strlen('Bundle') || substr($bundle, -strlen('Bundle')) !== 'Bundle') {
-                       //Append Bundle in an attempt to fix it's naming for locator
-                       $bundle .= 'Bundle';
-
-                       //Check for public resource prefix presence
-                       if (strlen($path) < strlen('Resources/public') || substr($path, 0, strlen('Resources/public')) != 'Resources/public') {
-                               //Prepend standard public path
-                               $path = 'Resources/public/'.$path;
-                       }
+               //Extract path
+               if (($path = strstr($file, '/')) === false) {
+                       throw new Error(sprintf('Invalid path "%s"', $file), $lineno, $source);
                }
 
-               //Resolve bundle prefix
-               try {
-                       $prefix = $this->locator->locate($bundle);
-               //Catch bundle does not exist or is not enabled exception
-               } catch(\InvalidArgumentException $e) {
-                       //Fix lowercase first bundle character
-                       if ($bundle[1] > 'Z' || $bundle[1] < 'A') {
-                               $bundle[1] = strtoupper($bundle[1]);
-                       }
-
-                       //Detect double bundle suffix
-                       if (strlen($bundle) > strlen('_bundleBundle') && substr($bundle, -strlen('_bundleBundle')) == '_bundleBundle') {
-                               //Strip extra bundle
-                               $bundle = substr($bundle, 0, -strlen('Bundle'));
-                       }
+               //Extract alias
+               $alias = strtolower(substr($bundle, 1));
 
-                       //Convert snake case in camel case
-                       if (strpos($bundle, '_') !== false) {
-                               //Fix every first character following a _
-                               while(($cur = strpos($bundle, '_')) !== false) {
-                                       $bundle = substr($bundle, 0, $cur).ucfirst(substr($bundle, $cur + 1));
-                               }
+               //With public parameter
+               if ($this->container->hasParameter($alias.'.public')) {
+                       //Set prefix
+                       $prefix = $this->container->getParameter($alias.'.public');
+               //Without public parameter
+               } else {
+                       //Without bundle suffix presence
+                       //XXX: use "bundle templates automatic namespace" mimicked behaviour to find intended bundle and/or path
+                       //XXX: see https://symfony.com/doc/4.3/templates.html#bundle-templates
+                       if (strlen($bundle) < strlen('@Bundle') || substr($bundle, -strlen('Bundle')) !== 'Bundle') {
+                               //Append Bundle
+                               $bundle .= 'Bundle';
                        }
 
-                       //Resolve fixed bundle prefix
+                       //Try to resolve bundle prefix
                        try {
                                $prefix = $this->locator->locate($bundle);
-                               //Catch bundle does not exist or is not enabled exception again
+                       //Catch bundle does not exist or is not enabled exception
                        } catch(\InvalidArgumentException $e) {
-                               //Bail out as bundle or path is invalid and we have no way to know what was meant
-                               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);
+                               throw new Error(sprintf('Unlocatable bundle "%s"', $bundle), $lineno, $source, $e);
+                       }
+
+                       //With Resources/public subdirectory
+                       if (is_dir($prefix.'Resources/public')) {
+                               $prefix .= 'Resources/public';
+                       //With public subdirectory
+                       } elseif (is_dir($prefix.'public')) {
+                               $prefix .= 'public';
+                       //Without any public subdirectory
+                       } else {
+                               throw new Error(sprintf('Bundle "%s" lacks a public subdirectory', $bundle), $lineno, $source, $e);
                        }
                }