Example #1
0
 public function process(AssetInterface $asset)
 {
     if (0 < preg_match($this->pattern, $asset->getTargetUrl())) {
         $asset->ensureFilter($this->filter);
     }
     return $asset;
 }
Example #2
0
 public function filterDump(AssetInterface $asset)
 {
     $sourceUrl = $asset->getSourceUrl();
     $targetUrl = $asset->getTargetUrl();
     if (null === $sourceUrl || null === $targetUrl || $sourceUrl == $targetUrl) {
         return;
     }
     // learn how to get from the target back to the source
     if (false !== strpos($sourceUrl, '://')) {
         // the source is absolute, this should be easy
         $parts = parse_url($sourceUrl);
         $host = $parts['scheme'] . '://' . $parts['host'];
         $path = dirname($parts['path']) . '/';
     } else {
         // assume source and target are on the same host
         $host = '';
         // pop entries off the target until it fits in the source
         if ('.' == dirname($sourceUrl)) {
             $path = str_repeat('../', substr_count($targetUrl, '/'));
         } elseif ('.' == ($targetDir = dirname($targetUrl))) {
             $path = dirname($sourceUrl) . '/';
         } else {
             $path = '';
             while (0 !== strpos($sourceUrl, $targetDir)) {
                 if (false !== ($pos = strrpos($targetDir, '/'))) {
                     $targetDir = substr($targetDir, 0, $pos);
                     $path .= '../';
                 } else {
                     $targetDir = '';
                     $path .= '../';
                     break;
                 }
             }
             $path .= substr(dirname($sourceUrl) . '/', strlen($targetDir) + 1);
         }
     }
     $callback = function ($matches) use($host, $path) {
         if (false !== strpos($matches['url'], '://')) {
             // absolute
             return $matches[0];
         }
         if ('/' == $matches['url'][0]) {
             // root relative
             return str_replace($matches['url'], $host . $matches['url'], $matches[0]);
         }
         // document relative
         $url = $matches['url'];
         while (0 === strpos($url, '../') && 2 <= substr_count($path, '/')) {
             $path = substr($path, 0, strrpos(rtrim($path, '/'), '/') + 1);
             $url = substr($url, 3);
         }
         return str_replace($matches['url'], $host . $path . $url, $matches[0]);
     };
     $content = $asset->getContent();
     $content = preg_replace_callback('/url\\((["\']?)(?<url>.*)(\\1)\\)/', $callback, $content);
     $content = preg_replace_callback('/import (["\'])(?<url>.*)(\\1)/', $callback, $content);
     $asset->setContent($content);
 }
Example #3
0
    public function process(AssetInterface $asset)
    {
        $targetUrl = $asset->getTargetUrl();
        if ($targetUrl && '/' != $targetUrl[0] && 0 !== strpos($targetUrl, '_controller/')) {
            $asset->setTargetUrl('_controller/'.$targetUrl);
        }

        return $asset;
    }
Example #4
0
 protected function compileAssetUrl(\Twig_Compiler $compiler, AssetInterface $asset, $name)
 {
     $compiler
         ->raw('isset($context[\'assetic\'][\'use_controller\']) && $context[\'assetic\'][\'use_controller\'] ? ')
         ->subcompile($this->getPathFunction($name))
         ->raw(' : ')
         ->subcompile($this->getAssetFunction($asset->getTargetUrl()))
     ;
 }
Example #5
0
 public function writeAsset(AssetInterface $asset)
 {
     static::write($this->dir . '/' . $asset->getTargetUrl(), $asset->dump());
 }
    public function filterDump(AssetInterface $asset)
    {
        $sourceUrl = $asset->getSourceUrl();
        $targetUrl = $asset->getTargetUrl();

        if (null === $sourceUrl || null === $targetUrl || $sourceUrl == $targetUrl) {
            return;
        }

        // learn how to get from the target back to the source
        if (false !== strpos($sourceUrl, '://')) {
            // the source is absolute, this should be easy
            $parts = parse_url($sourceUrl);

            $host = $parts['scheme'].'://'.$parts['host'];
            $path = dirname($parts['path']).'/';
        } else {
            // assume source and target are on the same host
            $host = '';

            // pop entries off the target until it fits in the source
            $path = '';
            $targetDir = dirname($targetUrl);
            while (0 !== strpos($sourceUrl, $targetDir)) {
                if (false !== $pos = strrpos($targetDir, '/')) {
                    $targetDir = substr($targetDir, 0, $pos);
                    $path .= '../';
                } else {
                    throw new \RuntimeException(sprintf('Unable to calculate relative path from "%s" to "%s"', $targetUrl, $sourceUrl));
                }
            }
            $path .= substr(dirname($sourceUrl).'/', strlen($targetDir) + 1);
        }

        $filter = function($url) use($host, $path)
        {
            if (false !== strpos($url, '://')) {
                // absolute
                return $url;
            } elseif ('/' == $url[0]) {
                // root relative
                return $host.$url;
            } else {
                // document relative
                while (0 === strpos($url, '../') && 2 <= substr_count($path, '/')) {
                    $path = substr($path, 0, strrpos(rtrim($path, '/'), '/') + 1);
                    $url = substr($url, 3);
                }
                return $host.$path.$url;
            }
        };

        // tokenize and filter the asset content
        $tokens = $this->tokenizer->tokenizeString($asset->getContent());

        // cleanup the php tags codesniffer adds
        $tokens = array_slice($tokens, 1, -1);
        $token = array_pop($tokens);
        if (' ' != $token['content']) {
            $token['content'] = substr($token['content'], 0, -1);
            $tokens[] = $token;
        }

        $content = '';
        $inUrl = $inImport = 0;
        for ($i = 0; $i < count($tokens); $i++) {
            $token = $tokens[$i];

            if (T_URL == $token['code']) {
                $token['content'] = $filter($token['content']);
            } elseif (T_STRING == $token['code'] && 'url' == $token['content']) {
                $inUrl = 1;
            } elseif (T_STRING == $token['code'] && 'import' == $token['content'] && isset($tokens[$i - 1]) && T_ASPERAND == $tokens[$i - 1]['code']) {
                $inImport = 1;
            } elseif (T_OPEN_PARENTHESIS == $token['code'] && 1 == $inUrl) {
                $inUrl = 2;
            } elseif (T_CONSTANT_ENCAPSED_STRING == $token['code'] && (2 == $inUrl || 1 == $inImport)) {
                $quote = $token['content'][0];
                $url = $filter(substr($token['content'], 1, -1));
                $token['content'] = $quote.$url.$quote;
            } elseif (T_WHITESPACE != $token['code']) {
                $inUrl = $inImport = 0;
            }

            $content .= $token['content'];
        }

        $asset->setContent($content);
    }
Example #7
0
    /**
     * Writes an asset.
     *
     * @param AssetInterface  $asset   An asset
     * @param string          $basePath The base directory to write to
     * @param OutputInterface $output  The command output
     *
     * @throws RuntimeException If there is a problem writing the asset
     */
    protected function dumpAsset(AssetInterface $asset, $basePath, OutputInterface $output)
    {
        $target = rtrim($basePath, '/') . '/' . $asset->getTargetUrl();
        if (!is_dir($dir = dirname($target))) {
            $output->writeln('<info>[dir+]</info> '.$dir);
            if (false === @mkdir($dir, 0777, true)) {
                throw new \RuntimeException('Unable to create directory '.$dir);
            }
        }

        $output->writeln('<info>[file+]</info> '.$target);
        if (false === @file_put_contents($target, $asset->dump())) {
            throw new \RuntimeException('Unable to write file '.$target);
        }
    }
 protected function getAssetUrl(AssetInterface $asset, $options = array())
 {
     return $asset->getTargetUrl();
 }
 protected function getAssetUrl(AssetInterface $asset, $options = array())
 {
     return $this->assetsHelper->getUrl($asset->getTargetUrl(), isset($options['package']) ? $options['package'] : null);
 }
 /**
  * Processes an asset.
  *
  * @param AssetInterface $asset An asset
  * @param Boolean        $debug Debug mode
  */
 public function process(AssetInterface $asset, $debug = false)
 {
     if ((null === $this->debug || $this->debug === $debug) && preg_match($this->pattern, $asset->getTargetUrl())) {
         $asset->ensureFilter($this->filter);
     }
 }
Example #11
0
    /**
     * Loads a route to serve an supplied asset.
     *
     * The fake front controller that {@link UseControllerWorker} adds to the
     * target URL will be removed before set as a route pattern.
     *
     * @param RouteCollection $routes The route collection
     * @param AssetInterface  $asset  The asset
     * @param string          $name   The name to use
     * @param integer         $pos    The leaf index
     */
    private function loadRouteForAsset(RouteCollection $routes, AssetInterface $asset, $name, $pos = null)
    {
        $defaults = array(
            '_controller' => 'assetic.controller:render',
            'name'        => $name,
            'pos'         => $pos,
        );

        // remove the fake front controller
        $pattern = str_replace('_controller/', '', $asset->getTargetUrl());

        if ($format = pathinfo($pattern, PATHINFO_EXTENSION)) {
            $defaults['_format'] = $format;
        }

        $route = '_assetic_'.$name;
        if (null !== $pos) {
            $route .= '_'.$pos;
        }

        $routes->add($route, new Route($pattern, $defaults));
    }
Example #12
0
 protected function compileAssetUrl(\Twig_Compiler $compiler, AssetInterface $asset, $name)
 {
     $compiler->repr($asset->getTargetUrl());
 }