]> Raphaƫl G. Git Repositories - packbundle/blobdiff - Twig/PackTokenParser.php
Cleanup
[packbundle] / Twig / PackTokenParser.php
index 24a43e991ddc8b86d2172634c6b8c796553bbdc0..54133cffb0d3caf44c2147d6e72c8b833a74e015 100644 (file)
@@ -3,7 +3,7 @@
 namespace Rapsys\PackBundle\Twig;
 
 use Symfony\Component\HttpKernel\Config\FileLocator;
-use Symfony\Component\DependencyInjection\ContainerInterface;
+use Symfony\Component\Asset\Packages;
 
 class PackTokenParser extends \Twig_TokenParser {
        private $tag;
@@ -13,52 +13,41 @@ class PackTokenParser extends \Twig_TokenParser {
         *
         * @param class         $fileLocator            The FileLocator instance
         * @param class         $assetsPackages         The Assets Packages instance
-        * @param string        $prefix                 The prefix path
+        * @param string        $config                 The config path
         * @param string        $tag                    The tag name
         * @param string        $output                 The default output string
         * @param array         $filters                The default filters array
         */
-       public function __construct(FileLocator $fileLocator, ContainerInterface $containerInterface, $prefix, $tag, $output, $filters) {
+       public function __construct(FileLocator $fileLocator, Packages $assetsPackages, $config, $tag, $output, $filters) {
                $this->fileLocator              = $fileLocator;
-               $this->containerInterface       = $containerInterface;
-               $this->prefix                   = $prefix;
+               $this->assetsPackages           = $assetsPackages;
+
+               //Set prefix
+               $this->prefix                   = $config['prefix'];
+
+               //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;
-               $this->output                   = $output;
-               $this->filters                  = $filters;
 
-               if ($this->containerInterface->hasParameter('rapsys_pack')) {
-                       if ($parameters = $this->containerInterface->getParameter('rapsys_pack')) {
-                               if (isset($parameters['timeout'])) {
-                                       $timeout = $parameters['timeout'];
-                               } elseif (isset($parameters['user_agent'])) {
-                                       $userAgent = $parameters['user_agent'];
-                               } elseif (isset($parameters['redirect'])) {
-                                       $redirect = $parameters['redirect'];
-                               }
-                       }
-               }
+               //Set output
+               $this->output                   = $output;
 
-               //Set http default timeout
-               $this->timeout = ini_get('default_socket_timeout');
-               //Set http default user agent
-               $this->userAgent = ini_get('user_agent');
-               //Set http default redirect
-               $this->redirect = 20;
-
-               //Try to load service defaults
-               if ($this->containerInterface->hasParameter('rapsys_pack')) {
-                       if ($parameters = $this->containerInterface->getParameter('rapsys_pack')) {
-                               if (!empty($parameters['timeout'])) {
-                                       $this->timeout = $parameters['timeout'];
-                               }
-                               if (!empty($parameters['user_agent'])) {
-                                       $this->userAgent = $parameters['user_agent'];
-                               }
-                               if (!empty($parameters['redirect'])) {
-                                       $this->redirect = $parameters['redirect'];
-                               }
-                       }
-               }
+               //Set filters
+               $this->filters                  = $filters;
        }
 
        public function getTag() {
@@ -70,7 +59,7 @@ class PackTokenParser extends \Twig_TokenParser {
                $stream = $this->parser->getStream();
 
                $inputs = array();
-               $name = 'asset_url';
+               $name = $this->name;
                $output = $this->output;
                $filters = $this->filters;
 
@@ -80,7 +69,7 @@ class PackTokenParser extends \Twig_TokenParser {
                        if ($stream->test(\Twig_Token::STRING_TYPE)) {
                                // '@jquery', 'js/src/core/*', 'js/src/extra.js'
                                $inputs[] = $stream->next()->getValue();
-                       } elseif ($stream->test(\Twig_Token::NAME_TYPE, 'filter')) {
+                       } elseif ($stream->test(\Twig_Token::NAME_TYPE, 'filters')) {
                                // filter='yui_js'
                                $stream->next();
                                $stream->expect(\Twig_Token::OPERATOR_TYPE, '=');
@@ -117,18 +106,8 @@ class PackTokenParser extends \Twig_TokenParser {
                for($k = 0; $k < count($inputs); $k++) {
                        //Deal with generic url
                        if (strpos($inputs[$k], '//') === 0) {
-                               //Default scheme
-                               $scheme = 'https://';
-                               //Try to load service scheme
-                               if ($this->containerInterface->hasParameter('rapsys_pack')) {
-                                       if ($parameters = $this->containerInterface->getParameter('rapsys_pack')) {
-                                               if (isset($parameters['scheme'])) {
-                                                       $scheme = $parameters['scheme'];
-                                               }
-                                       }
-                               }
                                //Fix url
-                               $inputs[$k] = $scheme.substr($inputs[$k], 2);
+                               $inputs[$k] = $this->scheme.substr($inputs[$k], 2);
                        //Deal with non url path
                        } elseif (strpos($inputs[$k], '://') === false) {
                                //Check if we have a bundle path
@@ -172,7 +151,7 @@ class PackTokenParser extends \Twig_TokenParser {
                        array(
                                'http' => array(
                                        'timeout' => $this->timeout,
-                                       'user_agent' => $this->userAgent,
+                                       'user_agent' => $this->agent,
                                        'redirect' => $this->redirect,
                                )
                        )
@@ -197,14 +176,21 @@ class PackTokenParser extends \Twig_TokenParser {
                if (!empty($filters)) {
                        //Apply all filters
                        foreach($filters as $filter) {
-                               //Prefix with filter
-                               $filter = __NAMESPACE__.'\\Filter\\'.$filter;
-                               //Init tool object
-                               $tool = new $filter($this->containerInterface, $stream->getSourceContext(), $token->getLine());
+                               //Init args
+                               $args = array($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);
+                               unset($tool, $reflection);
                        }
                } else {
                        #TODO: trigger error about empty filters ?
@@ -212,8 +198,12 @@ class PackTokenParser extends \Twig_TokenParser {
 
                //Create output dir on demand
                if (!is_dir($parent = $dir = dirname($this->prefix.$output))) {
-                       //XXX: set as 0777, symfony umask (0022) will reduce rights (0755)
-                       mkdir($dir, 0777, true);
+                       try {
+                               //XXX: set as 0777, symfony umask (0022) will reduce rights (0755)
+                               mkdir($dir, 0777, true);
+                       } catch (\Exception $e) {
+                               throw new \Twig_Error_Syntax(sprintf('Unable to create directory: %s', $dir), $token->getLine(), $stream->getSourceContext());
+                       }
                }
 
                //Send file content
@@ -233,8 +223,11 @@ class PackTokenParser extends \Twig_TokenParser {
                }
 
                //Retrieve asset uri
-               if (($output = $this->containerInterface->get('assets.packages')->getUrl($output, 'rapsys_pack')) === false) {
-                       throw new \Twig_Error_Syntax(sprintf('Unable to get url for asset: %s with package %s', $output, 'rapsys_pack'), $token->getLine(), $stream->getSourceContext());
+               //XXX: was next line to support module specific asset configuration
+               #if (($output = $this->assetsPackages->getUrl($output, 'rapsys_pack')) === false) {
+               if (($output = $this->assetsPackages->getUrl($output)) === false) {
+                       #throw new \Twig_Error_Syntax(sprintf('Unable to get url for asset: %s with package %s', $output, 'rapsys_pack'), $token->getLine(), $stream->getSourceContext());
+                       throw new \Twig_Error_Syntax(sprintf('Unable to get url for asset: %s', $output), $token->getLine(), $stream->getSourceContext());
                }
 
                //Send pack node