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());
    }
    /**
     * Compresses a string.
     *
     * @param string $content The content to compress
     * @param string $type    The type of content, either "js" or "css"
     * @param array  $options An indexed array of additional options
     *
     * @return string The compressed content
     */
    protected function compress($content, $type, $options = array())
    {
        // prepend the start of the command
        $options = array_merge(array(
            $this->javaPath,
            '-jar',
            $this->jarPath,
            '--type',
            $type,
        ), $options);

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

        if (null !== $this->lineBreak) {
            $options[] = '--line-break';
            $options[] = $this->lineBreak;
        }

        $options[] = $input = tempnam(sys_get_temp_dir(), 'assetic');
        file_put_contents($input, $content);

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

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

        return $proc->getOutput();
    }
Example #3
0
 public function filterDump(AssetInterface $asset)
 {
     $options = array($this->jpegtranBin);
     if ($this->optimize) {
         $options[] = '-optimize';
     }
     if ($this->copy) {
         $options[] = '-copy';
         $options[] = $this->copy;
     }
     if ($this->progressive) {
         $options[] = '-progressive';
     }
     if (null !== $this->restart) {
         $options[] = '-restart';
         $options[] = $this->restart;
     }
     $options[] = $input = tempnam(sys_get_temp_dir(), 'assetic_jpegtran');
     file_put_contents($input, $asset->getContent());
     $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 #4
0
    public function filterLoad(AssetInterface $asset)
    {
        $options = array($this->nodePath, $this->coffeePath, '-sc');

        $proc = new Process(implode(' ', array_map('escapeshellarg', $options)), null, array(), $asset->getContent());
        $code = $proc->run();

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

        $asset->setContent($proc->getOutput());
    }
Example #5
0
    public function filterLoad(AssetInterface $asset)
    {
        static $format = <<<'EOF'
var less = require('less');
var sys  = require('sys');

new(less.Parser)(%s).parse(%s, function(e, tree) {
    if (e) {
        less.writeError(e);
        process.exit(2);
    }

    try {
        sys.print(tree.toCSS(%s));
        process.exit(0);
    } catch (e) {
        less.writeError(e);
        process.exit(3);
    }
});

EOF;
        $sourceUrl = $asset->getSourceUrl();
        // parser options
        $parserOptions = array();
        if ($sourceUrl && false === strpos($sourceUrl, '://')) {
            $baseDir = self::isAbsolutePath($sourceUrl) ? '' : $this->baseDir . '/';
            $parserOptions['paths'] = array($baseDir . dirname($sourceUrl));
            $parserOptions['filename'] = basename($sourceUrl);
        }
        // tree options
        $treeOptions = array();
        if (null !== $this->compress) {
            $treeOptions['compress'] = $this->compress;
        }
        // node.js configuration
        $env = array();
        if (0 < count($this->nodePaths)) {
            $env['NODE_PATH'] = implode(':', $this->nodePaths);
        }
        $options = array($this->nodeBin);
        $options[] = $input = tempnam(sys_get_temp_dir(), 'assetic_less');
        file_put_contents($input, sprintf($format, json_encode($parserOptions), json_encode($asset->getContent()), json_encode($treeOptions)));
        $proc = new Process(implode(' ', array_map('escapeshellarg', $options)), null, $env);
        $code = $proc->run();
        unlink($input);
        if (0 < $code) {
            throw new \RuntimeException($proc->getErrorOutput());
        }
        $asset->setContent($proc->getOutput());
    }
Example #6
0
    /**
     * {@inheritdoc}
     */
    public function filterLoad(AssetInterface $asset)
    {
        static $format = <<<'EOF'
var stylus = require('stylus');
var sys    = require('sys');

stylus(%s, %s).render(function(e, css){
    if (e) {
        throw e;
    }

    sys.print(css);
    process.exit(0);
});

EOF;
        // parser options
        $parserOptions = array();
        if ($sourceUrl = $asset->getSourceUrl()) {
            $parserOptions['filename'] = basename($sourceUrl);
            $parserOptions['paths'] = array($this->baseDir . DIRECTORY_SEPARATOR . dirname($sourceUrl));
        }
        if (null !== $this->compress) {
            $parserOptions['compress'] = $this->compress;
        }
        // node.js configuration
        $env = array();
        if (0 < count($this->nodePaths)) {
            $env['NODE_PATH'] = implode(':', $this->nodePaths);
        }
        $options = array($this->nodeBin);
        $options[] = $input = tempnam(sys_get_temp_dir(), 'assetic_stylus');
        file_put_contents($input, sprintf($format, json_encode($asset->getContent()), json_encode($parserOptions)));
        $proc = new Process(implode(' ', array_map('escapeshellarg', $options)), null, $env);
        $code = $proc->run();
        unlink($input);
        if (0 < $code) {
            throw new \RuntimeException($proc->getErrorOutput());
        }
        $asset->setContent($proc->getOutput());
    }
Example #7
0
 public function filterDump(AssetInterface $asset)
 {
     $options = array($this->optipngBin);
     if (null !== $this->level) {
         $options[] = '-o';
         $options[] = $this->level;
     }
     $options[] = '-out';
     $options[] = $output = tempnam(sys_get_temp_dir(), 'assetic_optipng');
     unlink($output);
     $options[] = $input = tempnam(sys_get_temp_dir(), 'assetic_optipng');
     file_put_contents($input, $asset->getContent());
     $proc = new Process(implode(' ', array_map('escapeshellarg', $options)));
     $code = $proc->run();
     if (0 < $code) {
         unlink($input);
         throw new \RuntimeException($proc->getOutput());
     }
     $asset->setContent(file_get_contents($output));
     unlink($input);
     unlink($output);
 }
    public function filterDump(AssetInterface $asset)
    {
        $cleanup = array();

        $options = array(
            $this->javaPath,
            '-jar',
            $this->jarPath,
        );

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

        if (null !== $this->jsExterns) {
            $cleanup[] = $externs = tempnam(sys_get_temp_dir(), 'assetic_google_closure_compiler');
            file_put_contents($externs, $this->jsExterns);
            $options[] = '--externs';
            $options[] = $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));
            $options[] = '--externs';
            $options[] = $externs;
        }

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

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

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

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

        $options[] = '--js';
        $options[] = $cleanup[] = $input = tempnam(sys_get_temp_dir(), 'assetic_google_closure_compiler');
        file_put_contents($input, $asset->getContent());

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

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

        $asset->setContent($proc->getOutput());
    }
Example #9
0
    public function filterLoad(AssetInterface $asset)
    {
        $options = array($this->sassPath);

        if ($this->unixNewlines) {
            $options[] = '--unix-newlines';
        }

        if ($this->scss) {
            $options[] = '--scss';
        }

        if ($this->style) {
            $options[] = '--style';
            $options[] = $this->style;
        }

        if ($this->quiet) {
            $options[] = '--quiet';
        }

        if ($this->debugInfo) {
            $options[] = '--debug-info';
        }

        if ($this->lineNumbers) {
            $options[] = '--line-numbers';
        }

        foreach ($this->loadPaths as $loadPath) {
            $options[] = '--load-path';
            $options[] = $loadPath;
        }

        if ($this->cacheLocation) {
            $options[] = '--cache-location';
            $options[] = $this->cacheLocation;
        }

        if ($this->noCache) {
            $options[] = '--no-cache';
        }

        // finally
        $options[] = $input = tempnam(sys_get_temp_dir(), 'assetic_sass');
        file_put_contents($input, $asset->getContent());

        $options[] = $output = tempnam(sys_get_temp_dir(), 'assetic_sass');

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

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

        $asset->setContent(file_get_contents($output));

        unlink($input);
        unlink($output);
    }
Example #10
0
    /**
     * Hack around a bit, get the job done.
     */
    public function filterLoad(AssetInterface $asset)
    {
        static $format = <<<'EOF'
#!/usr/bin/env ruby

require File.join(%s, 'sprockets')

module Sprockets
  class Secretary
    def reset!(options = @options)
      @options = DEFAULT_OPTIONS.merge(options)
      @environment  = Sprockets::Environment.new(@options[:root])
      @preprocessor = Sprockets::Preprocessor.new(@environment, :strip_comments => @options[:strip_comments])

      add_load_locations(@options[:load_path])
      add_source_files(@options[:source_files])
    end
  end

  class Preprocessor
    protected

    def pathname_for_relative_require_from(source_line)
      Sprockets::Pathname.new(@environment, File.join(%s, location_from(source_line)))
    end
  end
end

options = { :load_path    => [],
            :source_files => [%s],
            :expand_paths => false }

%ssecretary = Sprockets::Secretary.new(options)
secretary.install_assets if options[:asset_root]
print secretary.concatenation

EOF;
        $sourceUrl = $asset->getSourceUrl();
        if (!$sourceUrl || false !== strpos($sourceUrl, '://')) {
            return;
        }
        $more = '';
        foreach ($this->includeDirs as $directory) {
            $more .= 'options[:load_path] << ' . var_export($directory, true) . "\n";
        }
        if (null !== $this->assetRoot) {
            $more .= 'options[:asset_root] = ' . var_export($this->assetRoot, true) . "\n";
        }
        if ($more) {
            $more .= "\n";
        }
        $tmpAsset = tempnam(sys_get_temp_dir(), 'assetic_sprockets');
        file_put_contents($tmpAsset, $asset->getContent());
        $input = tempnam(sys_get_temp_dir(), 'assetic_sprockets');
        file_put_contents($input, sprintf($format, var_export($this->sprocketsLib, true), var_export($this->baseDir, true), var_export($tmpAsset, true), $more));
        $proc = new Process($cmd = implode(' ', array_map('escapeshellarg', array($this->rubyBin, $input))));
        $code = $proc->run();
        unlink($tmpAsset);
        unlink($input);
        if (0 < $code) {
            throw new \RuntimeException($proc->getErrorOutput());
        }
        $asset->setContent($proc->getOutput());
    }