2 // src/Rapsys/PackBundle/Twig/PackTokenParser.php 
   3 namespace Rapsys\PackBundle\Twig
; 
   5 use Symfony\Component\HttpKernel\Config\FileLocator
; 
   6 use Symfony\Component\Asset\Packages
; 
   8 class PackTokenParser 
extends \Twig_TokenParser 
{ 
  14          * @param class         $fileLocator            The FileLocator instance 
  15          * @param class         $assetsPackages         The Assets Packages instance 
  16          * @param string        $config                 The config path 
  17          * @param string        $tag                    The tag name 
  18          * @param string        $output                 The default output string 
  19          * @param array         $filters                The default filters array 
  21         public function __construct(FileLocator 
$fileLocator, Packages 
$assetsPackages, $config, $tag, $output, $filters) { 
  22                 $this->fileLocator              
= $fileLocator; 
  23                 $this->assetsPackages           
= $assetsPackages; 
  26                 $this->prefix                   
= $config['prefix']; 
  29                 $this->name                     
= $config['name']; 
  32                 $this->scheme                   
= $config['scheme']; 
  35                 $this->timeout                  
= $config['timeout']; 
  38                 $this->agent                    
= $config['agent']; 
  41                 $this->redirect                 
= $config['redirect']; 
  47                 $this->output                   
= $output; 
  50                 $this->filters                  
= $filters; 
  53         public function getTag() { 
  57         public function parse(\Twig_Token 
$token) { 
  58                 $parser = $this->parser
; 
  59                 $stream = $this->parser
->getStream(); 
  63                 $output = $this->output
; 
  64                 $filters = $this->filters
; 
  68                 while (!$stream->test(\Twig_Token
::BLOCK_END_TYPE
)) { 
  69                         if ($stream->test(\Twig_Token
::STRING_TYPE
)) { 
  70                                 // '@jquery', 'js/src/core/*', 'js/src/extra.js' 
  71                                 $inputs[] = $stream->next()->getValue(); 
  72                         } elseif ($stream->test(\Twig_Token
::NAME_TYPE
, 'filters')) { 
  75                                 $stream->expect(\Twig_Token
::OPERATOR_TYPE
, '='); 
  76                                 $filters = array_merge($filters, array_filter(array_map('trim', explode(',', $stream->expect(\Twig_Token
::STRING_TYPE
)->getValue())))); 
  77                         } elseif ($stream->test(\Twig_Token
::NAME_TYPE
, 'output')) { 
  78                                 // output='js/packed/*.js' OR output='js/core.js' 
  80                                 $stream->expect(\Twig_Token
::OPERATOR_TYPE
, '='); 
  81                                 $output = $stream->expect(\Twig_Token
::STRING_TYPE
)->getValue(); 
  82                         } elseif ($stream->test(\Twig_Token
::NAME_TYPE
, 'name')) { 
  85                                 $stream->expect(\Twig_Token
::OPERATOR_TYPE
, '='); 
  86                                 $name = $stream->expect(\Twig_Token
::STRING_TYPE
)->getValue(); 
  88                                 $token = $stream->getCurrent(); 
  89                                 throw new \
Twig_Error_Syntax(sprintf('Unexpected token "%s" of value "%s"', \Twig_Token
::typeToEnglish($token->getType()), $token->getValue()), $token->getLine(), $stream->getSourceContext()); 
  93                 $stream->expect(\Twig_Token
::BLOCK_END_TYPE
); 
  95                 $body = $this->parser
->subparse(array($this, 'testEndTag'), true); 
  97                 $stream->expect(\Twig_Token
::BLOCK_END_TYPE
); 
  99                 //Replace star with sha1 
 100                 if (($pos = strpos($output, '*')) !== false) { 
 101                         #XXX: assetic code: substr(sha1(serialize($inputs).serialize($filters).serialize($options)), 0, 7) 
 102                         $output = substr($output, 0, $pos).sha1(serialize($inputs).serialize($filters)).substr($output, $pos + 
1); 
 106                 for($k = 0; $k < count($inputs); $k++
) { 
 107                         //Deal with generic url 
 108                         if (strpos($inputs[$k], '//') === 0) { 
 110                                 $inputs[$k] = $this->scheme
.substr($inputs[$k], 2); 
 111                         //Deal with non url path 
 112                         } elseif (strpos($inputs[$k], '://') === false) { 
 113                                 //Check if we have a bundle path 
 114                                 if ($inputs[$k][0] == '@') { 
 115                                         //Check that we don't have only a path 
 116                                         if (($pos = strpos($inputs[$k], '/')) === false) { 
 117                                                 #TODO: @jquery support (if we really want it) 
 118                                                 #header('Content-Type: text/plain'); 
 120                                                 #if ($inputs[0] == '@jquery') { 
 123                                                 throw new \
Twig_Error_Syntax(sprintf('Invalid input path "%s"', $inputs[$k]), $token->getLine(), $stream->getSourceContext()); 
 125                                         //Resolve bundle prefix 
 126                                         $inputs[$k] = $this->fileLocator
->locate(substr($inputs[$k], 0, $pos)).substr($inputs[$k], $pos + 
1); 
 129                                 if (strpos($inputs[$k], '*') !== false || (($a = strpos($inputs[$k], '{')) !== false && ($b = strpos($inputs[$k], ',', $a)) !== false && strpos($inputs[$k], '}', $b) !== false)) { 
 131                                         $replacement = glob($inputs[$k], GLOB_NOSORT
|GLOB_BRACE
); 
 132                                         //Check that these are working files 
 133                                         foreach($replacement as $input) { 
 134                                                 if (!is_file($input)) { 
 135                                                         throw new \
Twig_Error_Syntax(sprintf('Input path "%s" from "%s" is not a file', $input, $inputs[$k]), $token->getLine(), $stream->getSourceContext()); 
 138                                         //Replace with glob path 
 139                                         array_splice($inputs, $k, 1, $replacement); 
 141                                         $k +
= count($replacement) - 1; 
 142                                 //Check that it's a file 
 143                                 } elseif (!is_file($inputs[$k])) { 
 144                                         throw new \
Twig_Error_Syntax(sprintf('Input path "%s" is not a file', $inputs[$k]), $token->getLine(), $stream->getSourceContext()); 
 150                 $ctx = stream_context_create( 
 153                                         'timeout' => $this->timeout
, 
 154                                         'user_agent' => $this->agent
, 
 155                                         'redirect' => $this->redirect
, 
 161                 if (!empty($inputs)) { 
 162                         //Retrieve files content 
 163                         foreach($inputs as $input) { 
 164                                 //Try to retrieve content 
 165                                 if (($data = file_get_contents($input, false, $ctx)) === false) { 
 166                                         throw new \
Twig_Error_Syntax(sprintf('Unable to retrieve input path "%s"', $input), $token->getLine(), $stream->getSourceContext()); 
 172                         #TODO: trigger error about empty inputs ? 
 176                 if (!empty($filters)) { 
 178                         foreach($filters as $filter) { 
 180                                 $args = array($stream->getSourceContext(), $token->getLine()); 
 181                                 //Check if args is available 
 182                                 if (!empty($filter['args'])) { 
 183                                         //Append args if provided 
 184                                         $args +
= $filter['args']; 
 187                                 $reflection = new \
ReflectionClass($filter['class']); 
 189                                 $tool = $reflection->newInstanceArgs($args); 
 191                                 $content = $tool->process($content); 
 193                                 unset($tool, $reflection); 
 196                         #TODO: trigger error about empty filters ? 
 199                 //Create output dir on demand 
 200                 if (!is_dir($parent = $dir = dirname($this->prefix
.$output))) { 
 202                                 //XXX: set as 0777, symfony umask (0022) will reduce rights (0755) 
 203                                 mkdir($dir, 0777, true); 
 204                         } catch (\Exception 
$e) { 
 205                                 throw new \
Twig_Error_Syntax(sprintf('Unable to create directory: %s', $dir), $token->getLine(), $stream->getSourceContext()); 
 210                 //XXX: atomic rotation is required to avoid partial content in reverse cache 
 211                 if (file_put_contents($this->prefix
.$output.'.new', $content) === false) { 
 212                         throw new \
Twig_Error_Syntax(sprintf('Unable to write to: %s', $prefix.$output.'.new'), $token->getLine(), $stream->getSourceContext()); 
 216                 if (is_file($this->prefix
.$output) && unlink($this->prefix
.$output) === false) { 
 217                         throw new \
Twig_Error_Syntax(sprintf('Unable to unlink: %s', $prefix.$output), $token->getLine(), $stream->getSourceContext()); 
 221                 if (rename($this->prefix
.$output.'.new', $this->prefix
.$output) === false) { 
 222                         throw new \
Twig_Error_Syntax(sprintf('Unable to rename: %s to %s', $prefix.$output.'.new', $prefix.$output), $token->getLine(), $stream->getSourceContext()); 
 226                 //XXX: was next line to support module specific asset configuration 
 227                 #if (($output = $this->assetsPackages->getUrl($output, 'rapsys_pack')) === false) { 
 228                 if (($output = $this->assetsPackages
->getUrl($output)) === false) { 
 229                         #throw new \Twig_Error_Syntax(sprintf('Unable to get url for asset: %s with package %s', $output, 'rapsys_pack'), $token->getLine(), $stream->getSourceContext()); 
 230                         throw new \
Twig_Error_Syntax(sprintf('Unable to get url for asset: %s', $output), $token->getLine(), $stream->getSourceContext()); 
 234                 return new PackNode(array('value' => $body), array('inputs' => $inputs, 'filters' => $filters, 'name' => $name, 'output' => $output), $token->getLine(), $this->getTag()); 
 237         public function testEndTag(\Twig_Token 
$token) { 
 238                 return $token->test(array('end'.$this->getTag()));