ILIAS  release_5-2 Revision v5.2.25-18-g3f80b82851
EmberPrecompileFilter.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the Assetic package, an OpenSky project.
5  *
6  * (c) 2010-2014 OpenSky Project Inc
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11 
12 namespace Assetic\Filter;
13 
17 
27 {
28  private $emberBin;
29  private $nodeBin;
30 
31  public function __construct($handlebarsBin = '/usr/bin/ember-precompile', $nodeBin = null)
32  {
33  $this->emberBin = $handlebarsBin;
34  $this->nodeBin = $nodeBin;
35  }
36 
37  public function filterLoad(AssetInterface $asset)
38  {
39  $pb = $this->createProcessBuilder($this->nodeBin
40  ? array($this->nodeBin, $this->emberBin)
41  : array($this->emberBin));
42 
43  if ($sourcePath = $asset->getSourcePath()) {
44  $templateName = basename($sourcePath);
45  } else {
46  throw new \LogicException('The embed-precompile filter requires that assets have a source path set');
47  }
48 
49  $inputDirPath = FilesystemUtils::createThrowAwayDirectory('ember_in');
50  $inputPath = $inputDirPath.DIRECTORY_SEPARATOR.$templateName;
51  $outputPath = FilesystemUtils::createTemporaryFile('ember_out');
52 
53  file_put_contents($inputPath, $asset->getContent());
54 
55  $pb->add($inputPath)->add('-f')->add($outputPath);
56 
57  $process = $pb->getProcess();
58  $returnCode = $process->run();
59 
60  unlink($inputPath);
61  rmdir($inputDirPath);
62 
63  if (127 === $returnCode) {
64  throw new \RuntimeException('Path to node executable could not be resolved.');
65  }
66 
67  if (0 !== $returnCode) {
68  if (file_exists($outputPath)) {
69  unlink($outputPath);
70  }
71  throw FilterException::fromProcess($process)->setInput($asset->getContent());
72  }
73 
74  if (!file_exists($outputPath)) {
75  throw new \RuntimeException('Error creating output file.');
76  }
77 
78  $compiledJs = file_get_contents($outputPath);
79  unlink($outputPath);
80 
81  $asset->setContent($compiledJs);
82  }
83 
84  public function filterDump(AssetInterface $asset)
85  {
86  }
87 }
static createTemporaryFile($prefix)
Creates a temporary file.
static fromProcess(Process $proc)
Precompiles Handlebars templates for use in the Ember.js framework.
filterLoad(AssetInterface $asset)
Filters an asset after it has been loaded.
__construct($handlebarsBin='/usr/bin/ember-precompile', $nodeBin=null)
filterDump(AssetInterface $asset)
Filters an asset just before it&#39;s dumped.
getSourcePath()
Returns the relative path for the source asset.
createProcessBuilder(array $arguments=array())
getContent()
Returns the loaded content of the current asset.
An asset has a mutable URL and content and can be loaded and dumped.
Create styles array
The data for the language used.
static createThrowAwayDirectory($prefix)
Creates a throw-away directory.
setContent($content)
Sets the content of the current asset.