public function StylusFilter::filterLoad

Filters an asset after it has been loaded.

Parameters

AssetInterface $asset An asset:

Overrides FilterInterface::filterLoad

File

drupal/core/vendor/kriswallsmith/assetic/src/Assetic/Filter/StylusFilter.php, line 55

Class

StylusFilter
Loads STYL files.

Namespace

Assetic\Filter

Code

public function filterLoad(AssetInterface $asset) {
  static $format = <<<'EOF'
var stylus = require('stylus');
var sys    = require(process.binding('natives').util ? 'util' : 'sys');

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

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

EOF;
  $root = $asset
    ->getSourceRoot();
  $path = $asset
    ->getSourcePath();

  // parser options
  $parserOptions = array();
  if ($root && $path) {
    $parserOptions['paths'] = array(
      dirname($root . '/' . $path),
    );
    $parserOptions['filename'] = basename($path);
  }
  if (null !== $this->compress) {
    $parserOptions['compress'] = $this->compress;
  }
  $pb = new ProcessBuilder();
  $pb
    ->inheritEnvironmentVariables();

  // node.js configuration
  if (0 < count($this->nodePaths)) {
    $pb
      ->setEnv('NODE_PATH', implode(':', $this->nodePaths));
  }
  $pb
    ->add($this->nodeBin)
    ->add($input = tempnam(sys_get_temp_dir(), 'assetic_stylus'));
  file_put_contents($input, sprintf($format, json_encode($asset
    ->getContent()), json_encode($parserOptions)));
  $proc = $pb
    ->getProcess();
  $code = $proc
    ->run();
  unlink($input);
  if (0 < $code) {
    throw FilterException::fromProcess($proc)
      ->setInput($asset
      ->getContent());
  }
  $asset
    ->setContent($proc
    ->getOutput());
}