public function filterLoad(AssetInterface $asset)
 {
     $pb = $this->createProcessBuilder($this->nodeBin ? array($this->nodeBin, $this->emberBin) : array($this->emberBin));
     if ($sourcePath = $asset->getSourcePath()) {
         $templateName = basename($sourcePath);
     } else {
         throw new \LogicException('The embed-precompile filter requires that assets have a source path set');
     }
     $inputDirPath = FilesystemUtils::createThrowAwayDirectory('ember_in');
     $inputPath = $inputDirPath . DIRECTORY_SEPARATOR . $templateName;
     $outputPath = FilesystemUtils::createTemporaryFile('ember_out');
     file_put_contents($inputPath, $asset->getContent());
     $pb->add($inputPath)->add('-f')->add($outputPath);
     $process = $pb->getProcess();
     $returnCode = $process->run();
     unlink($inputPath);
     rmdir($inputDirPath);
     if (127 === $returnCode) {
         throw new \RuntimeException('Path to node executable could not be resolved.');
     }
     if (0 !== $returnCode) {
         if (file_exists($outputPath)) {
             unlink($outputPath);
         }
         throw FilterException::fromProcess($process)->setInput($asset->getContent());
     }
     if (!file_exists($outputPath)) {
         throw new \RuntimeException('Error creating output file.');
     }
     $compiledJs = file_get_contents($outputPath);
     unlink($outputPath);
     $asset->setContent($compiledJs);
 }
Example #2
0
 public function filterLoad(AssetInterface $asset)
 {
     $pb = $this->createProcessBuilder($this->nodeBin ? array($this->nodeBin, $this->tscBin) : array($this->tscBin));
     $templateName = basename($asset->getSourcePath());
     $inputDirPath = sys_get_temp_dir() . DIRECTORY_SEPARATOR . uniqid('input_dir');
     $inputPath = $inputDirPath . DIRECTORY_SEPARATOR . $templateName . '.ts';
     $outputPath = tempnam(sys_get_temp_dir(), 'output');
     mkdir($inputDirPath);
     file_put_contents($inputPath, $asset->getContent());
     $pb->add($inputPath)->add('--out')->add($outputPath);
     $proc = $pb->getProcess();
     $code = $proc->run();
     unlink($inputPath);
     rmdir($inputDirPath);
     if (0 !== $code) {
         if (file_exists($outputPath)) {
             unlink($outputPath);
         }
         throw FilterException::fromProcess($proc)->setInput($asset->getContent());
     }
     if (!file_exists($outputPath)) {
         throw new \RuntimeException('Error creating output file.');
     }
     $compiledJs = file_get_contents($outputPath);
     unlink($outputPath);
     $asset->setContent($compiledJs);
 }
 protected function compress(AssetInterface $asset, $type, $options = array())
 {
     $pb = $this->createProcessBuilder(array($this->javaPath));
     $pb->add('-jar')->add($this->jarPath);
     foreach ($options as $option) {
         $pb->add($option);
     }
     $this->charset and $pb->add('--charset')->add($this->charset);
     $this->preserveComments and $pb->add('--preserve-comments');
     $this->removeIntertagSpaces and $pb->add('--remove-intertag-spaces');
     // input and output files
     $tempDir = realpath(sys_get_temp_dir());
     $input = tempnam($tempDir, 'assetic_htmlcompressor');
     $output = tempnam($tempDir, 'assetic_htmlcompressor');
     file_put_contents($input, $asset->getContent());
     $pb->add('-o')->add($output)->add($input);
     $proc = $pb->getProcess();
     $code = $proc->run();
     unlink($input);
     if (0 !== $code || false !== strpos($proc->getOutput(), 'ERROR')) {
         if (file_exists($output)) {
             unlink($output);
         }
         throw FilterException::fromProcess($proc)->setInput($asset->getContent());
     }
     $asset->setContent(file_get_contents($output));
     unlink($output);
 }
 public function filterLoad(AssetInterface $asset)
 {
     $pb = $this->createProcessBuilder($this->nodeBin ? array($this->nodeBin, $this->tscBin) : array($this->tscBin));
     if ($sourcePath = $asset->getSourcePath()) {
         $templateName = basename($sourcePath);
     } else {
         $templateName = 'asset';
     }
     $inputDirPath = FilesystemUtils::createThrowAwayDirectory('typescript_in');
     $inputPath = $inputDirPath . DIRECTORY_SEPARATOR . $templateName . '.ts';
     $outputPath = FilesystemUtils::createTemporaryFile('typescript_out');
     file_put_contents($inputPath, $asset->getContent());
     $pb->add($inputPath)->add('--out')->add($outputPath);
     $proc = $pb->getProcess();
     $code = $proc->run();
     unlink($inputPath);
     rmdir($inputDirPath);
     if (0 !== $code) {
         if (file_exists($outputPath)) {
             unlink($outputPath);
         }
         throw FilterException::fromProcess($proc)->setInput($asset->getContent());
     }
     if (!file_exists($outputPath)) {
         throw new \RuntimeException('Error creating output file.');
     }
     $compiledJs = file_get_contents($outputPath);
     unlink($outputPath);
     $asset->setContent($compiledJs);
 }
 public function filterLoad(AssetInterface $asset)
 {
     $pb = $this->createProcessBuilder($this->nodeBin ? array($this->nodeBin, $this->emberBin) : array($this->emberBin));
     $templateName = basename($asset->getSourcePath());
     $inputDirPath = sys_get_temp_dir() . DIRECTORY_SEPARATOR . uniqid('input_dir');
     $inputPath = $inputDirPath . DIRECTORY_SEPARATOR . $templateName;
     $outputPath = tempnam(sys_get_temp_dir(), 'output');
     mkdir($inputDirPath);
     file_put_contents($inputPath, $asset->getContent());
     $pb->add($inputPath)->add('-f')->add($outputPath);
     if ($this->includeBaseDir) {
         $pb->add('-b')->add($inputDirPath . DIRECTORY_SEPARATOR);
     }
     $process = $pb->getProcess();
     $returnCode = $process->run();
     unlink($inputPath);
     rmdir($inputDirPath);
     if (127 === $returnCode) {
         throw new \RuntimeException('Path to node executable could not be resolved.');
     }
     if (0 !== $returnCode) {
         if (file_exists($outputPath)) {
             unlink($outputPath);
         }
         throw FilterException::fromProcess($process)->setInput($asset->getContent());
     }
     if (!file_exists($outputPath)) {
         throw new \RuntimeException('Error creating output file.');
     }
     $compiledJs = file_get_contents($outputPath);
     unlink($outputPath);
     $asset->setContent($compiledJs);
 }
Example #6
0
 /**
  * Filters an asset just before it's dumped.
  *
  * @param AssetInterface $asset An asset
  */
 public function filterDump(AssetInterface $asset)
 {
     $pb = $this->createProcessBuilder($this->nodeBin ? array($this->nodeBin, $this->cssoPath) : array($this->cssoPath));
     // input and output files
     $input = tempnam(sys_get_temp_dir(), 'input');
     $output = tempnam(sys_get_temp_dir(), 'output');
     file_put_contents($input, $asset->getContent());
     $pb->add('-i')->add($input);
     $pb->add('-o')->add($output);
     $process = $pb->getProcess();
     $code = $process->run();
     unlink($input);
     if (0 !== $code) {
         if (file_exists($output)) {
             unlink($output);
         }
         if (127 === $code) {
             throw new \RuntimeException('Path to node executable could not be resolved.');
         }
         throw FilterException::fromProcess($process)->setInput($asset->getContent());
     }
     if (!file_exists($output)) {
         throw new \RuntimeException('Error creating output file.');
     }
     $csso = file_get_contents($output);
     unlink($output);
     $asset->setContent($csso);
 }
 /**
  * Filters an asset just before it's dumped.
  *
  * @param AssetInterface $asset An asset
  */
 public function filterDump(AssetInterface $asset)
 {
     $pb = $this->createProcessBuilder($this->nodeBin ? array($this->nodeBin, $this->htmlMinifierBin) : array($this->htmlMinifierBin));
     // input and output files
     $input = tempnam(sys_get_temp_dir(), 'input');
     $output = tempnam(sys_get_temp_dir(), 'output');
     file_put_contents($input, $asset->getContent());
     foreach ($this->getOptions() as $option) {
         $pb->add('--' . $option);
     }
     $pb->add('--output');
     $pb->add($output);
     $pb->add($input);
     $proc = $pb->getProcess();
     $code = $proc->run();
     unlink($input);
     if (0 !== $code) {
         if (file_exists($output)) {
             unlink($output);
         }
         throw FilterException::fromProcess($proc)->setInput($asset->getContent());
     }
     if (!file_exists($output)) {
         throw new \RuntimeException('Error creating output file.');
     }
     $asset->setContent(file_get_contents($output));
     unlink($output);
 }
 public function filterDump(AssetInterface $asset)
 {
     $pb = $this->createProcessBuilder(array($this->jpegtranBin));
     if ($this->optimize) {
         $pb->add('-optimize');
     }
     if ($this->copy) {
         $pb->add('-copy')->add($this->copy);
     }
     if ($this->progressive) {
         $pb->add('-progressive');
     }
     if (null !== $this->restart) {
         $pb->add('-restart')->add($this->restart);
     }
     $pb->add($input = tempnam(sys_get_temp_dir(), 'assetic_jpegtran'));
     file_put_contents($input, $asset->getContent());
     $proc = $pb->getProcess();
     $code = $proc->run();
     unlink($input);
     if (0 < $code) {
         throw FilterException::fromProcess($proc)->setInput($asset->getContent());
     }
     $asset->setContent($proc->getOutput());
 }
 public function filterDump(AssetInterface $asset)
 {
     $pb = $this->createProcessBuilder($this->nodeBin ? array($this->nodeBin, $this->uglifyjsBin) : array($this->uglifyjsBin));
     if ($this->compress) {
         $pb->add('--compress');
     }
     if ($this->beautify) {
         $pb->add('--beautify');
     }
     if ($this->mangle) {
         $pb->add('--mangle');
     }
     // input and output files
     $input = tempnam(sys_get_temp_dir(), 'input');
     $output = tempnam(sys_get_temp_dir(), 'output');
     file_put_contents($input, $asset->getContent());
     $pb->add('-o')->add($output)->add($input);
     $proc = $pb->getProcess();
     $code = $proc->run();
     unlink($input);
     if (0 < $code) {
         if (file_exists($output)) {
             unlink($output);
         }
         if (127 === $code) {
             throw new \RuntimeException('Path to node executable could not be resolved.');
         }
         throw FilterException::fromProcess($proc)->setInput($asset->getContent());
     }
     if (!file_exists($output)) {
         throw new \RuntimeException('Error creating output file.');
     }
     $asset->setContent(file_get_contents($output));
     unlink($output);
 }
 public function filterLoad(AssetInterface $asset)
 {
     $sassProcessArgs = array();
     if (null !== $this->nodePath) {
         $sassProcessArgs[] = $this->nodePath;
     }
     $sassProcessArgs[] = $this->sassPath;
     $pb = $this->createProcessBuilder($sassProcessArgs);
     if ($dir = $asset->getSourceDirectory()) {
         $pb->add('--include-path')->add($dir);
     }
     if ($this->style) {
         $pb->add('--output-style')->add($this->style);
     }
     if ($this->sourceMap) {
         $pb->add('--source-map');
     }
     if ($this->debugInfo) {
         $pb->add('--source-comments');
     }
     foreach ($this->loadPaths as $loadPath) {
         $pb->add('--include-path')->add($loadPath);
     }
     // input
     $pb->add($input = tempnam(sys_get_temp_dir(), 'assetic_sass'));
     file_put_contents($input, $asset->getContent());
     $pb->add('--stdout');
     $proc = $pb->getProcess();
     $code = $proc->run();
     unlink($input);
     if (0 !== $code) {
         throw FilterException::fromProcess($proc)->setInput($asset->getContent());
     }
     $asset->setContent($proc->getOutput());
 }
 public function filterLoad(AssetInterface $asset)
 {
     $content = $asset->getContent();
     $root = $asset->getSourceRoot();
     $path = $asset->getSourcePath();
     if (!empty($content)) {
         $content = \CoffeeScript\Compiler::compile($asset->getContent(), array('filename' => $path));
     }
     $asset->setContent($content);
 }
Example #12
0
 public function filterDump(AssetInterface $asset)
 {
     switch ($this->type) {
         case 'css':
             $cssMin = new CSSMin();
             $content = $cssMin->run($asset->getContent());
             break;
         case 'js':
             $content = JSMin::minify($asset->getContent());
             break;
     }
     $asset->setContent($content);
 }
 public function filterDump(AssetInterface $asset)
 {
     $pb = $this->createProcessBuilder(array($this->ttf2eotBin));
     $input = tempnam(sys_get_temp_dir(), 'assetic_ttf2eot');
     file_put_contents($input, $asset->getContent());
     $pb->add($input);
     $proc = $pb->getProcess();
     $code = $proc->run();
     unlink($input);
     if ($code !== 0) {
         throw FilterException::fromProcess($proc)->setInput($asset->getContent());
     }
     $asset->setContent($proc->getOutput());
 }
 public function filterDump(AssetInterface $asset)
 {
     $is64bit = PHP_INT_SIZE === 8;
     $cleanup = array();
     $pb = new ProcessBuilder(array_merge(array($this->javaPath), $is64bit ? array('-server', '-XX:+TieredCompilation') : array('-client', '-d32'), array('-jar', $this->jarPath)));
     if (null !== $this->timeout) {
         $pb->setTimeout($this->timeout);
     }
     if (null !== $this->compilationLevel) {
         $pb->add('--compilation_level')->add($this->compilationLevel);
     }
     if (null !== $this->jsExterns) {
         $cleanup[] = $externs = tempnam(sys_get_temp_dir(), 'assetic_google_closure_compiler');
         file_put_contents($externs, $this->jsExterns);
         $pb->add('--externs')->add($externs);
     }
     if (null !== $this->externsUrl) {
         $cleanup[] = $externs = tempnam(sys_get_temp_dir(), 'assetic_google_closure_compiler');
         file_put_contents($externs, file_get_contents($this->externsUrl));
         $pb->add('--externs')->add($externs);
     }
     if (null !== $this->excludeDefaultExterns) {
         $pb->add('--use_only_custom_externs');
     }
     if (null !== $this->formatting) {
         $pb->add('--formatting')->add($this->formatting);
     }
     if (null !== $this->useClosureLibrary) {
         $pb->add('--manage_closure_dependencies');
     }
     if (null !== $this->warningLevel) {
         $pb->add('--warning_level')->add($this->warningLevel);
     }
     if (null !== $this->language) {
         $pb->add('--language_in')->add($this->language);
     }
     if (null !== $this->flagFile) {
         $pb->add('--flagfile')->add($this->flagFile);
     }
     $pb->add('--js')->add($cleanup[] = $input = tempnam(sys_get_temp_dir(), 'assetic_google_closure_compiler'));
     file_put_contents($input, $asset->getContent());
     $proc = $pb->getProcess();
     $code = $proc->run();
     array_map('unlink', $cleanup);
     if (0 !== $code) {
         throw FilterException::fromProcess($proc)->setInput($asset->getContent());
     }
     $asset->setContent($proc->getOutput());
 }
Example #15
0
 public function filterLoad(AssetInterface $asset)
 {
     $input = tempnam(sys_get_temp_dir(), 'assetic_roole');
     file_put_contents($input, $asset->getContent());
     $pb = $this->createProcessBuilder($this->nodeBin ? array($this->nodeBin, $this->rooleBin) : array($this->rooleBin));
     $pb->add('-p');
     $pb->add($input);
     $proc = $pb->getProcess();
     $code = $proc->run();
     unlink($input);
     if (0 !== $code) {
         throw FilterException::fromProcess($proc)->setInput($asset->getContent());
     }
     $asset->setContent($proc->getOutput());
 }
Example #16
0
 public function filterLoad(AssetInterface $asset)
 {
     $builder = $this->createProcessBuilder($this->nodeBin ? array($this->nodeBin, $this->jsxBin) : array($this->jsxBin));
     $inputDir = FilesystemUtils::createThrowAwayDirectory('jsx_in');
     $inputFile = $inputDir . DIRECTORY_SEPARATOR . 'asset.js';
     $outputDir = FilesystemUtils::createThrowAwayDirectory('jsx_out');
     $outputFile = $outputDir . DIRECTORY_SEPARATOR . 'asset.js';
     // create the asset file
     file_put_contents($inputFile, $asset->getContent());
     $builder->add($inputDir)->add($outputDir)->add('--no-cache-dir');
     $proc = $builder->getProcess();
     $code = $proc->run();
     // remove the input directory and asset file
     unlink($inputFile);
     rmdir($inputDir);
     if (0 !== $code) {
         if (file_exists($outputFile)) {
             unlink($outputFile);
         }
         if (file_exists($outputDir)) {
             rmdir($outputDir);
         }
         throw FilterException::fromProcess($proc);
     }
     $asset->setContent(file_get_contents($outputFile));
     // remove the output directory and processed asset file
     unlink($outputFile);
     rmdir($outputDir);
 }
 public function filterDump(AssetInterface $asset)
 {
     $content = $asset->getContent();
     var_dump($content);
     // Do something to $content
     $asset->setContent($content);
 }
 public function filterDump(AssetInterface $asset)
 {
     $script = $asset->getContent();
     $packer = new \JavaScriptPacker($script, $this->_encoding, $this->_fastDecode, $this->_specialChars);
     $script = $packer->pack();
     $asset->setContent(str_replace(";;", ";", trim($script) . ";"));
 }
    public function filterLoad(AssetInterface $asset)
    {
        $sourceUrl = $asset->getSourceUrl();
        if (!$sourceUrl || false !== strpos($sourceUrl, '://')) {
            return;
        }

        $options = array($this->sprocketizePath);

        foreach ($this->includeDirs as $directory) {
            $options[] = '-I';
            $options[] = $directory;
        }

        if (null !== $this->assetRoot) {
            $options[] = '-a';
            $options[] = $this->assetRoot;
        }

        // hack in a temporary file sibling
        $options[] = $input = dirname($this->baseDir.'/'.$sourceUrl).'/.'.rand(11111, 99999).'-'.basename($sourceUrl);
        $tmp = tempnam(sys_get_temp_dir(), 'assetic_sprockets');
        file_put_contents($tmp, $asset->getContent());
        rename($tmp, $input);

        $proc = new Process(implode(' ', array_map('escapeshellarg', $options)));
        $code = $proc->run();
        unlink($input);

        if (0 < $code) {
            throw new \RuntimeException($proc->getErrorOutput());
        }

        $asset->setContent($proc->getOutput());
    }
Example #20
0
 /**
  * Run the asset through UglifyJs
  *
  * @see Assetic\Filter\FilterInterface::filterDump()
  */
 public function filterDump(AssetInterface $asset)
 {
     $pb = $this->createProcessBuilder($this->nodeBin ? array($this->nodeBin, $this->uglifycssBin) : array($this->uglifycssBin));
     if ($this->expandVars) {
         $pb->add('--expand-vars');
     }
     if ($this->uglyComments) {
         $pb->add('--ugly-comments');
     }
     if ($this->cuteComments) {
         $pb->add('--cute-comments');
     }
     // input and output files
     $input = tempnam(sys_get_temp_dir(), 'input');
     file_put_contents($input, $asset->getContent());
     $pb->add($input);
     $proc = $pb->getProcess();
     $code = $proc->run();
     unlink($input);
     if (127 === $code) {
         throw new \RuntimeException('Path to node executable could not be resolved.');
     }
     if (0 !== $code) {
         throw FilterException::fromProcess($proc)->setInput($asset->getContent());
     }
     $asset->setContent($proc->getOutput());
 }
 public function filterDump(AssetInterface $asset)
 {
     $content = $asset->getContent();
     $config = array_merge(array('filename' => $asset->getSourcePath()), $this->config);
     $content = Compiler::compile($content, $config);
     $asset->setContent($content);
 }
 public function filterDump(AssetInterface $asset)
 {
     $optimizer = new Optimizer();
     $content = $asset->getContent();
     $content = $optimizer->optimizeCss($content);
     $asset->setContent($content);
 }
 public function filterDump(AssetInterface $asset)
 {
     $pb = new ProcessBuilder();
     $pb->inheritEnvironmentVariables()->add($this->jpegtranBin);
     if ($this->optimize) {
         $pb->add('-optimize');
     }
     if ($this->copy) {
         $pb->add('-copy')->add($this->copy);
     }
     if ($this->progressive) {
         $pb->add('-progressive');
     }
     if (null !== $this->restart) {
         $pb->add('-restart')->add($this->restart);
     }
     $pb->add($input = tempnam(sys_get_temp_dir(), 'assetic_jpegtran'));
     file_put_contents($input, $asset->getContent());
     $proc = $pb->getProcess();
     $code = $proc->run();
     unlink($input);
     if (0 < $code) {
         throw new \RuntimeException($proc->getErrorOutput());
     }
     $asset->setContent($proc->getOutput());
 }
    /**
     * {@inheritdoc}
     */
    public function filterDump(AssetInterface $asset)
    {
        if (preg_match('/\\.yfp\\.js$/', $asset->getSourcePath())) {
            $content = $asset->getContent();
            preg_match('/(\\w+)\\.yfp\\.js$/', $asset->getSourcePath(), $matches);
            if (isset($matches[1])) {
                $name = $matches[1];
                $pluginName = str_replace(' ', '', ucwords(strtr($matches[1], '_-', '  ')));
                $config = [];
                if ($this->parameterBag->has("ynlo.js_plugin.{$name}")) {
                    $config = $this->parameterBag->get("ynlo.js_plugin.{$name}");
                }
                $jsonConfig = json_encode($config, JSON_FORCE_OBJECT);
                $autoRegister = null;
                if (strpos($content, "YnloFramework.register('{$pluginName}')") === false && strpos($content, "YnloFramework.register(\"{$pluginName}\\')") === false) {
                    $autoRegister = "\nYnloFramework.register('{$pluginName}');";
                }
                $settings = <<<JAVASCRIPT
{$autoRegister}
YnloFramework.{$pluginName}.config = \$.extend({}, YnloFramework.{$pluginName}.config, {$jsonConfig});

JAVASCRIPT;
                $asset->setContent($content . $settings);
            }
        }
    }
Example #25
0
 /**
  * {@inheritdoc}
  */
 public function getContent()
 {
     if (!$this->innerAsset) {
         $this->createInnerAsset();
     }
     return $this->innerAsset->getContent();
 }
 /**
  * Apply a filter on file dump.
  *
  * @param  \Assetic\Asset\AssetInterface  $asset
  * @return void
  */
 public function filterDump(AssetInterface $asset)
 {
     // Using getSourceDirectory
     // instead of getSourceRoot
     // Since relative paths are based on the sourceDirectory...
     $this->assetDirectory = $this->realPath($asset->getSourceDirectory());
     $content = $asset->getContent();
     // Spin through the symlinks and normalize them. We'll first unset the original
     // symlink so that it doesn't clash with the new symlinks once they are added
     // back in.
     foreach ($this->symlinks as $link => $target) {
         unset($this->symlinks[$link]);
         if ($link == '//') {
             $link = $this->documentRoot;
         } else {
             $link = str_replace('//', $this->documentRoot . '/', $link);
         }
         $link = strtr($link, '/', DIRECTORY_SEPARATOR);
         $this->symlinks[$link] = $this->realPath($target);
     }
     $content = $this->trimUrls($content);
     $content = preg_replace_callback('/@import\\s+([\'"])(.*?)[\'"]/', array($this, 'processUriCallback'), $content);
     $content = preg_replace_callback('/url\\(\\s*([^\\)\\s]+)\\s*\\)/', array($this, 'processUriCallback'), $content);
     $asset->setContent($content);
 }
 public function dump(AssetInterface $asset)
 {
     $writer = new \Assetic\AssetWriter(sys_get_temp_dir(), $this->container->getParameter('assetic.variables'));
     $ref = new \ReflectionMethod($writer, 'getCombinations');
     $ref->setAccessible(true);
     $name = $asset->getSourcePath();
     $type = substr($name, strrpos($name, '.') + 1);
     switch ($type) {
         case 'coffee':
             $asset->ensureFilter($this->container->get('assetic.filter.coffee'));
             $type = 'js';
             break;
         case 'less':
             $asset->ensureFilter($this->container->get('assetic.filter.less'));
             $type = 'css';
             break;
     }
     $combinations = $ref->invoke($writer, $asset->getVars());
     $asset->setValues($combinations[0]);
     $asset->load();
     $content = $asset->getContent();
     // because the assetic cssrewrite makes bullshit here, we need to reimplement the filter
     if ($type === 'css') {
         $content = $this->cssFilter($content, '/' . dirname($asset->getSourcePath()));
     }
     return $content;
 }
 /**
  * @param AssetInterface $asset
  */
 public function filterDump(AssetInterface $asset)
 {
     $sourceBase = $asset->getSourceRoot();
     $sourcePath = $asset->getSourcePath();
     $assetRoot = $this->assetRoot;
     if (null === $sourcePath) {
         return;
     }
     $content = $this->filterReferences($asset->getContent(), function ($matches) use($sourceBase, $sourcePath, $assetRoot) {
         // its not a relative path
         if (false !== strpos($matches['url'], '://') || 0 === strpos($matches['url'], '//') || 0 === strpos($matches['url'], 'data:') || isset($matches['url'][0]) && '/' == $matches['url'][0]) {
             return $matches[0];
         }
         $url = $matches['url'];
         if (false !== ($pos = strpos($url, '?'))) {
             $url = substr($url, 0, $pos);
         }
         $sourceAsset = dirname($sourceBase . '/' . $sourcePath) . '/' . $url;
         if (!is_file($sourceAsset)) {
             return $matches[0];
         }
         $mimeType = MimeTypeGuesser::getInstance()->guess($sourceAsset);
         $destRelativePath = substr($mimeType, 0, strpos($mimeType, '/')) . '/' . basename($url);
         $destAsset = $assetRoot . '/' . $destRelativePath;
         if (!is_dir(dirname($destAsset))) {
             mkdir(dirname($destAsset), 0777, true);
         }
         copy($sourceAsset, $destAsset);
         return str_replace($matches['url'], '../' . $destRelativePath, $matches[0]);
     });
     $asset->setContent($content);
 }
 private function doFilter(AssetInterface $asset)
 {
     $content = $asset->getContent();
     $callback = function ($matches) {
         $fs = new Filesystem();
         $resource = $matches['resource'];
         preg_match("/(\\@{1,2})([A-Z][A-Za-z0-9\\_\\-]*)/", $resource, $matches);
         if ($resource[1] == "@") {
             $resource = substr($resource, 1);
         }
         try {
             $bundle = $this->container->get('kernel')->getBundle($matches[2]);
             $path = $this->container->get('kernel')->locateResource($resource);
             if ($fs->exists($path)) {
                 if (preg_match("/Resources\\/public\\/(.*)/", $path, $matches2)) {
                     $path = 'bundles/' . preg_replace('/bundle$/', '', strtolower($bundle->getName())) . '/' . $matches2[1];
                     if ($matches[1] == "@@") {
                         return $this->container->get('kernel')->getRootDir() . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'web' . DIRECTORY_SEPARATOR . $path;
                     }
                     try {
                         return $this->container->get('templating.helper.assets')->getUrl($path);
                     } catch (InactiveScopeException $e) {
                         return "../" . $path;
                     }
                 }
             }
         } catch (Exception $e) {
         }
         return $resource;
     };
     $pattern = "/(?P<resource>\\@{1,2}[A-Za-z\\_]+Bundle[A-Za-z0-9\\_\\.\\/\\-]*)/";
     $asset->setContent(preg_replace_callback($pattern, $callback, $content));
 }
Example #30
0
 public function filterDump(AssetInterface $asset)
 {
     $content = $asset->getContent();
     $matches = array();
     $patternSearchResult = preg_match_all(self::$stringCodePattern, $content, $matches);
     if (FALSE === $patternSearchResult) {
         // TODO log something
         return;
     }
     if (0 === $patternSearchResult) {
         // nothing to internationalize
         return;
     }
     $translator = $this->getTranslator();
     $globalMatches = $matches[0];
     $separators = $matches[1];
     $expressions = $matches[2];
     for ($i = 0; $i < count($globalMatches); $i++) {
         $globalMatch = $globalMatches[$i];
         $separator = $separators[$i];
         $stringCode = $expressions[$i];
         $primaryString = $this->getPrimaryString($stringCode);
         $translatedString = $this->getTranslator()->translate($primaryString, 'default', $this->targetLocale);
         $content = str_replace($globalMatch, $separator . $translatedString . $separator, $content);
     }
     $asset->setContent($content);
 }