]> Raphaël G. Git Repositories - packbundle/blobdiff - Parser/TokenParser.php
Version 0.5.0
[packbundle] / Parser / TokenParser.php
index 9f2bf145421fe4ba5e67b2e74c7b4614d66e3510..67e87eb52f6bad193e0add9d6b8fc9f1a8aac6d5 100644 (file)
@@ -1,11 +1,23 @@
-<?php
+<?php declare(strict_types=1);
 
 
-namespace Rapsys\PackBundle\Twig;
+/*
+ * 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 Rapsys\PackBundle\RapsysPackBundle;
 
 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\Asset\PackageInterface;
 use Symfony\Component\Filesystem\Exception\IOExceptionInterface;
 use Symfony\Component\Filesystem\Filesystem;
 use Symfony\Component\HttpKernel\Config\FileLocator;
+
 use Twig\Error\Error;
 use Twig\Node\Expression\AssignNameExpression;
 use Twig\Node\Node;
 use Twig\Error\Error;
 use Twig\Node\Expression\AssignNameExpression;
 use Twig\Node\Node;
@@ -15,50 +27,37 @@ use Twig\Source;
 use Twig\Token;
 use Twig\TokenParser\AbstractTokenParser;
 
 use Twig\Token;
 use Twig\TokenParser\AbstractTokenParser;
 
-class PackTokenParser extends AbstractTokenParser {
-       ///The tag name
-       protected $tag;
+/**
+ * {@inheritdoc}
+ */
+class TokenParser extends AbstractTokenParser {
+       /**
+        * The stream context instance
+        */
+       protected mixed $ctx;
 
        /**
         * Constructor
         *
 
        /**
         * Constructor
         *
-        * @param FileLocator      locator The FileLocator instance
-        * @param PackageInterface package The Assets Package instance
-        * @param array            config  The config path
-        * @param string           tag     The tag name
-        * @param string           output  The default output string
-        * @param array            filters The default filters array
+        * @param FileLocator $locator The FileLocator instance
+        * @param PackageInterface $package The Assets Package instance
+        * @param string $token The token name
+        * @param string $tag The tag name
+        * @param string $output The default output string
+        * @param array $filters The default filter array
         */
         */
-       public function __construct(FileLocator $locator, PackageInterface $package, $config, $tag, $output, $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;
-
-               //Set output
-               $this->output = $output;
-
-               //Set filters
-               $this->filters = $filters;
+       public function __construct(protected FileLocator $locator, protected PackageInterface $package, protected string $token, protected string $tag, protected string $output, protected array $filters) {
+               //Set ctx
+               $this->ctx = stream_context_create(
+                       [
+                               'http' => [
+                                       #'header' => ['Referer: https://www.openstreetmap.org/'],
+                                       'max_redirects' => $_ENV['RAPSYSPACK_REDIRECT'] ?? 20,
+                                       'timeout' => $_ENV['RAPSYSPACK_TIMEOUT'] ?? (($timeout = ini_get('default_socket_timeout')) !== false && $timeout !== "" ? (float)$timeout : 60),
+                                       'user_agent' => $_ENV['RAPSYSPACK_AGENT'] ?? (($agent = ini_get('user_agent')) !== false && $agent !== "" ? (string)$agent : RapsysPackBundle::getAlias().'/'.RapsysPackBundle::getVersion())
+                               ]
+                       ]
+               );
        }
 
        /**
        }
 
        /**
@@ -66,38 +65,31 @@ class PackTokenParser extends AbstractTokenParser {
         *
         * @return string This tag name
         */
         *
         * @return string This tag name
         */
-       public function getTag() {
+       public function getTag(): string {
                return $this->tag;
        }
 
        /**
         * Parse the token
         *
                return $this->tag;
        }
 
        /**
         * Parse the token
         *
-        * @param Token token The \Twig\Token instance
+        * @xxx Skip filter when debug mode is enabled is not possible
+        * @xxx This code is only run once when twig cache is enabled
+        * @xxx Twig cache value is not avaible in container parameters, maybe in twig env ?
         *
         *
+        * @param Token $token The \Twig\Token instance
         * @return Node The PackNode
         * @return Node The PackNode
-        *
-        * @todo see if we can't add a debug mode behaviour
-        *
-        * If twig.debug or env=dev (or rapsys_pack.config.debug?) is set, it should be possible to loop on each input
-        * and process the captured body without applying requested filter.
-        *
-        * @todo list:
-        * - detect debug mode
-        * - retrieve fixe link from input s%@(Name)Bundle/Resources/public(/somewhere/file.ext)%/bundles/\L\1\E\2%
-        * - for each inputs:
-        *   - generate a set asset_url=x
-        *   - generate a body
         */
         */
-       public function parse(Token $token) {
+       public function parse(Token $token): Node {
+               //Get parser
                $parser = $this->parser;
                $parser = $this->parser;
+
+               //Get parser stream
                $stream = $this->parser->getStream();
 
                $stream = $this->parser->getStream();
 
+               //Set inputs array
                $inputs = [];
                $inputs = [];
-               $name = $this->name;
-               $output = $this->output;
-               $filters = $this->filters;
 
 
+               //Set content
                $content = '';
 
                //Process the token block until end
                $content = '';
 
                //Process the token block until end
@@ -111,19 +103,19 @@ class PackTokenParser extends AbstractTokenParser {
                                //filter='yui_js'
                                $stream->next();
                                $stream->expect(Token::OPERATOR_TYPE, '=');
                                //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 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, '=');
                        //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();
+                       //The token name
+                       } elseif ($stream->test(Token::NAME_TYPE, 'token')) {
                                //name='core_js'
                                $stream->next();
                                $stream->expect(Token::OPERATOR_TYPE, '=');
                                //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();
                        //Unexpected token
                        } else {
                                $token = $stream->getCurrent();
@@ -140,12 +132,10 @@ class PackTokenParser extends AbstractTokenParser {
                //Process end block
                $stream->expect(Token::BLOCK_END_TYPE);
 
                //Process end block
                $stream->expect(Token::BLOCK_END_TYPE);
 
-               //TODO: debug mode should be inserted here before the output variable is rewritten
-
                //Replace star with sha1
                //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);
+               if (($pos = strpos($this->output, '*')) !== false) {
+                       //XXX: assetic use substr(sha1(serialize($inputs).serialize($this->filters).serialize($this->output)), 0, 7)
+                       $this->output = substr($this->output, 0, $pos).sha1(serialize($inputs).serialize($this->filters)).substr($this->output, $pos + 1);
                }
 
                //Process inputs
                }
 
                //Process inputs
@@ -153,7 +143,7 @@ class PackTokenParser extends AbstractTokenParser {
                        //Deal with generic url
                        if (strpos($inputs[$k], '//') === 0) {
                                //Fix url
                        //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
                        //Deal with non url path
                        } elseif (strpos($inputs[$k], '://') === false) {
                                //Check if we have a bundle path
@@ -166,6 +156,7 @@ class PackTokenParser 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);
                                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
                                        //Check that these are working files
                                        foreach($replacement as $input) {
                                                //Check that it's a file
@@ -173,8 +164,10 @@ class PackTokenParser extends AbstractTokenParser {
                                                        throw new Error(sprintf('Input path "%s" from "%s" is not a file', $input, $inputs[$k]), $token->getLine(), $stream->getSourceContext());
                                                }
                                        }
                                                        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);
                                        //Replace with glob path
                                        array_splice($inputs, $k, 1, $replacement);
+
                                        //Fix current key
                                        $k += count($replacement) - 1;
                                //Check that it's a file
                                        //Fix current key
                                        $k += count($replacement) - 1;
                                //Check that it's a file
@@ -184,25 +177,15 @@ class PackTokenParser extends AbstractTokenParser {
                        }
                }
 
                        }
                }
 
-               //Init context
-               $ctx = stream_context_create(
-                       [
-                               'http' => [
-                                       'timeout' => $this->timeout,
-                                       'user_agent' => $this->agent,
-                                       'redirect' => $this->redirect,
-                               ]
-                       ]
-               );
-
                //Check inputs
                if (!empty($inputs)) {
                        //Retrieve files content
                        foreach($inputs as $input) {
                                //Try to retrieve content
                //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 new Error(sprintf('Unable to retrieve input path "%s"', $input), $token->getLine(), $stream->getSourceContext());
                                }
                                        throw new Error(sprintf('Unable to retrieve input path "%s"', $input), $token->getLine(), $stream->getSourceContext());
                                }
+
                                //Append content
                                $content .= $data;
                        }
                                //Append content
                                $content .= $data;
                        }
@@ -216,22 +199,27 @@ class PackTokenParser extends AbstractTokenParser {
                }
 
                //Check filters
                }
 
                //Check filters
-               if (!empty($filters)) {
+               if (!empty($this->filters)) {
                        //Apply all filters
                        //Apply all filters
-                       foreach($filters as $filter) {
+                       foreach($this->filters as $filter) {
                                //Init args
                                $args = [$stream->getSourceContext(), $token->getLine()];
                                //Init args
                                $args = [$stream->getSourceContext(), $token->getLine()];
+
                                //Check if args is available
                                if (!empty($filter['args'])) {
                                        //Append args if provided
                                        $args += $filter['args'];
                                }
                                //Check if args is available
                                if (!empty($filter['args'])) {
                                        //Append args if provided
                                        $args += $filter['args'];
                                }
+
                                //Init reflection
                                $reflection = new \ReflectionClass($filter['class']);
                                //Init reflection
                                $reflection = new \ReflectionClass($filter['class']);
+
                                //Set instance args
                                $tool = $reflection->newInstanceArgs($args);
                                //Set instance args
                                $tool = $reflection->newInstanceArgs($args);
+
                                //Process content
                                $content = $tool->process($content);
                                //Process content
                                $content = $tool->process($content);
+
                                //Remove object
                                unset($tool, $reflection);
                        }
                                //Remove object
                                unset($tool, $reflection);
                        }
@@ -242,26 +230,26 @@ class PackTokenParser extends AbstractTokenParser {
                }
 
                //Retrieve asset uri
                }
 
                //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());
+               //XXX: this path is the merge of services.assets.path_package.arguments[0] and rapsyspack.output.(css,img,js)
+               if (($outputUrl = $this->package->getUrl($this->output)) === false) {
+                       throw new Error(sprintf('Unable to get url for asset: %s', $this->output), $token->getLine(), $stream->getSourceContext());
                }
 
                //Check if we have a bundle path
                }
 
                //Check if we have a bundle path
-               if ($output[0] == '@') {
+               if ($this->output[0] == '@') {
                        //Resolve it
                        //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
                }
 
                //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)
                        try {
                                //Create dir
                                //XXX: set as 0775, symfony umask (0022) will reduce rights (0755)
-                           $filesystem->mkdir($dir, 0775);
+                               $filesystem->mkdir($dir, 0775);
                        } catch (IOExceptionInterface $e) {
                                //Throw error
                                throw new Error(sprintf('Output directory "%s" do not exists and unable to create it', $dir), $token->getLine(), $stream->getSourceContext(), $e);
                        } catch (IOExceptionInterface $e) {
                                //Throw error
                                throw new Error(sprintf('Output directory "%s" do not exists and unable to create it', $dir), $token->getLine(), $stream->getSourceContext(), $e);
@@ -273,14 +261,14 @@ class PackTokenParser extends AbstractTokenParser {
                        //Write content to file
                        //XXX: this call is (maybe) atomic
                        //XXX: see https://symfony.com/doc/current/components/filesystem.html#dumpfile
                        //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
                } 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 to: %s', $this->output), $token->getLine(), $stream->getSourceContext(), $e);
                }
 
                //Set name in context key
                }
 
                //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());
 
                //Set output in context value
                $value = new TextNode($outputUrl, $token->getLine());
@@ -297,28 +285,25 @@ class PackTokenParser extends AbstractTokenParser {
        /**
         * Test for tag end
         *
        /**
         * Test for tag end
         *
-        * @param Token token The \Twig\Token instance
-        *
-        * @return bool
+        * @param Token $token The \Twig\Token instance
+        * @return bool The token end test result
         */
         */
-       public function testEndTag(Token $token) {
+       public function testEndTag(Token $token): bool {
                return $token->test(['end'.$this->getTag()]);
        }
 
        /**
         * Get path from bundled file
         *
                return $token->test(['end'.$this->getTag()]);
        }
 
        /**
         * Get path from bundled file
         *
-        * @param string     file   The bundled file path
-        * @param int        lineno The template line where the error occurred
-        * @param Source     source The source context where the error occurred
-        * @param \Exception prev   The previous exception
+        * @see https://symfony.com/doc/current/bundles.html#overridding-the-bundle-directory-structure
         *
         *
+        * @param string $file The bundled file path
+        * @param int $lineno The template line where the error occurred
+        * @param Source $source The source context where the error occurred
+        * @param Exception $prev The previous exception
         * @return string The resolved file path
         * @return string The resolved file path
-        *
-        * @todo Try retrive public dir from the member function BundleNameBundle::getPublicDir() return value ?
-        * @xxx see https://symfony.com/doc/current/bundles.html#overridding-the-bundle-directory-structure
         */
         */
-       public function getLocated($file, int $lineno = 0, Source $source = null, \Exception $prev = null) {
+       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');
                /*TODO: add a @jquery magic feature ?
                if ($file == '@jquery') {
                        #header('Content-Type: text/plain');
@@ -329,7 +314,7 @@ class PackTokenParser extends AbstractTokenParser {
 
                //Check that we have a / separator between bundle name and path
                if (($pos = strpos($file, '/')) === false) {
 
                //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());
+                       throw new Error(sprintf('Invalid path "%s"', $file), $lineno, $source);
                }
 
                //Set bundle
                }
 
                //Set bundle
@@ -382,7 +367,7 @@ class PackTokenParser extends AbstractTokenParser {
                                //Catch bundle does not exist or is not enabled exception again
                        } catch(\InvalidArgumentException $e) {
                                //Bail out as bundle or path is invalid and we have no way to know what was meant
                                //Catch bundle does not exist or is not enabled exception again
                        } 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('Invalid bundle name "%s" in path "%s". Maybe you meant "%s"', substr($file, 1, $pos - 1), $file, $bundle.'/'.$path), $lineno, $source, $e);
                        }
                }
 
                        }
                }