public function HandlebarsFilter::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/HandlebarsFilter.php, line 47

Class

HandlebarsFilter
Compiles Handlebars templates into Javascript.

Namespace

Assetic\Filter

Code

public function filterLoad(AssetInterface $asset) {
  $pb = $this
    ->createProcessBuilder($this->nodeBin ? array(
    $this->nodeBin,
    $this->handlebarsBin,
  ) : array(
    $this->handlebarsBin,
  ));
  $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->minimize) {
    $pb
      ->add('--min');
  }
  if ($this->simple) {
    $pb
      ->add('--simple');
  }
  $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);
}