1 <?php 
declare(strict_types
=1); 
   4  * This file is part of the Rapsys PackBundle package. 
   6  * (c) Raphaël Gertz <symfony@rapsys.eu> 
   8  * For the full copyright and license information, please view the LICENSE 
   9  * file that was distributed with this source code. 
  12 namespace Rapsys\PackBundle\Parser
; 
  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
; 
  20 use Twig\Node\Expression\AssignNameExpression
; 
  22 use Twig\Node\SetNode
; 
  23 use Twig\Node\TextNode
; 
  26 use Twig\TokenParser\AbstractTokenParser
; 
  28 class TokenParser 
extends AbstractTokenParser 
{ 
  30          * The stream context instance 
  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 
  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) { 
  47                 $this->ctx 
= stream_context_create( 
  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', 
  66          * @return string This tag name 
  68         public function getTag(): string { 
  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 ? 
  79          * @param Token $token The \Twig\Token instance 
  80          * @return Node The PackNode 
  82         public function parse(Token 
$token): Node 
{ 
  83                 $parser = $this->parser
; 
  84                 $stream = $this->parser
->getStream(); 
  87                 $name = $this->config
['name']; 
  88                 $output = $this->output
; 
  89                 $filters = $this->filters
; 
  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(); 
 100                         } elseif ($stream->test(Token
::NAME_TYPE
, 'filters')) { 
 103                                 $stream->expect(Token
::OPERATOR_TYPE
, '='); 
 104                                 $filters = array_merge($filters, array_filter(array_map('trim', explode(',', $stream->expect(Token
::STRING_TYPE
)->getValue())))); 
 106                         } elseif ($stream->test(Token
::NAME_TYPE
, 'output')) { 
 107                                 //output='js/packed/*.js' OR output='js/core.js' 
 109                                 $stream->expect(Token
::OPERATOR_TYPE
, '='); 
 110                                 $output = $stream->expect(Token
::STRING_TYPE
)->getValue(); 
 112                         } elseif ($stream->test(Token
::NAME_TYPE
, 'name')) { 
 115                                 $stream->expect(Token
::OPERATOR_TYPE
, '='); 
 116                                 $name = $stream->expect(Token
::STRING_TYPE
)->getValue(); 
 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()); 
 125                 $stream->expect(Token
::BLOCK_END_TYPE
); 
 128                 $body = $this->parser
->subparse([$this, 'testEndTag'], true); 
 131                 $stream->expect(Token
::BLOCK_END_TYPE
); 
 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); 
 140                 for($k = 0; $k < count($inputs); $k++
) { 
 141                         //Deal with generic url 
 142                         if (strpos($inputs[$k], '//') === 0) { 
 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] == '@') { 
 151                                         $inputs[$k] = $this->getLocated($inputs[$k], $token->getLine(), $stream->getSourceContext()); 
 155                                 if (strpos($inputs[$k], '*') !== false || (($a = strpos($inputs[$k], '{')) !== false && ($b = strpos($inputs[$k], ',', $a)) !== false && strpos($inputs[$k], '}', $b) !== false)) { 
 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()); 
 165                                         //Replace with glob path 
 166                                         array_splice($inputs, $k, 1, $replacement); 
 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()); 
 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()); 
 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()); 
 192                         //Send an empty node without inputs 
 197                 if (!empty($filters)) { 
 199                         foreach($filters as $filter) { 
 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']; 
 208                                 $reflection = new \
ReflectionClass($filter['class']); 
 210                                 $tool = $reflection->newInstanceArgs($args); 
 212                                 $content = $tool->process($content); 
 214                                 unset($tool, $reflection); 
 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()); 
 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()); 
 228                 //Check if we have a bundle path 
 229                 if ($output[0] == '@') { 
 231                         $output = $this->getLocated($output, $token->getLine(), $stream->getSourceContext()); 
 235                 $filesystem = new Filesystem(); 
 237                 //Create output dir if not present 
 238                 if (!is_dir($dir = dirname($output))) { 
 241                                 //XXX: set as 0775, symfony umask (0022) will reduce rights (0755) 
 242                                 $filesystem->mkdir($dir, 0775); 
 243                         } catch (IOExceptionInterface 
$e) { 
 245                                 throw new Error(sprintf('Output directory "%s" do not exists and unable to create it', $dir), $token->getLine(), $stream->getSourceContext(), $e); 
 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) { 
 257                         throw new Error(sprintf('Unable to write to: %s', $output), $token->getLine(), $stream->getSourceContext(), $e); 
 260                 //Set name in context key 
 261                 $ref = new AssignNameExpression($name, $token->getLine()); 
 263                 //Set output in context value 
 264                 $value = new TextNode($outputUrl, $token->getLine()); 
 266                 //Send body with context set 
 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 
 278          * @param Token $token The \Twig\Token instance 
 279          * @return bool The token end test result 
 281         public function testEndTag(Token 
$token): bool { 
 282                 return $token->test(['end'.$this->getTag()]); 
 286          * Get path from bundled file 
 288          * @see https://symfony.com/doc/current/bundles.html#overridding-the-bundle-directory-structure 
 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 
 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'); 
 302                         return $this->config['jquery']; 
 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); 
 311                 $bundle = substr($file, 0, $pos); 
 314                 $path = substr($file, $pos + 
1); 
 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 
 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; 
 330                 //Resolve bundle prefix 
 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]); 
 340                         //Detect double bundle suffix 
 341                         if (strlen($bundle) > strlen('_bundleBundle') && substr($bundle, -strlen('_bundleBundle')) == '_bundleBundle') { 
 343                                 $bundle = substr($bundle, 0, -strlen('Bundle')); 
 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)); 
 354                         //Resolve fixed bundle prefix 
 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); 
 364                 //Return solved bundle prefix and path 
 365                 return $prefix.$path;