ILIAS  release_5-2 Revision v5.2.25-18-g3f80b82851
StylusFilter.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 
18 
26 {
27  private $nodeBin;
28  private $compress;
29  private $useNib;
30 
37  public function __construct($nodeBin = '/usr/bin/node', array $nodePaths = array())
38  {
39  $this->nodeBin = $nodeBin;
40  $this->setNodePaths($nodePaths);
41  }
42 
48  public function setCompress($compress)
49  {
50  $this->compress = $compress;
51  }
52 
58  public function setUseNib($useNib)
59  {
60  $this->useNib = $useNib;
61  }
62 
66  public function filterLoad(AssetInterface $asset)
67  {
68  static $format = <<<'EOF'
69 var stylus = require('stylus');
70 var sys = require(process.binding('natives').util ? 'util' : 'sys');
71 
72 stylus(%s, %s)%s.render(function(e, css){
73  if (e) {
74  throw e;
75  }
76 
77  sys.print(css);
78  process.exit(0);
79 });
80 
81 EOF;
82 
83  // parser options
84  $parserOptions = array();
85  if ($dir = $asset->getSourceDirectory()) {
86  $parserOptions['paths'] = array($dir);
87  $parserOptions['filename'] = basename($asset->getSourcePath());
88  }
89 
90  if (null !== $this->compress) {
91  $parserOptions['compress'] = $this->compress;
92  }
93 
94  $pb = $this->createProcessBuilder();
95 
96  $pb->add($this->nodeBin)->add($input = FilesystemUtils::createTemporaryFile('stylus'));
97  file_put_contents($input, sprintf($format,
98  json_encode($asset->getContent()),
99  json_encode($parserOptions),
100  $this->useNib ? '.use(require(\'nib\')())' : ''
101  ));
102 
103  $proc = $pb->getProcess();
104  $code = $proc->run();
105  unlink($input);
106 
107  if (0 !== $code) {
108  throw FilterException::fromProcess($proc)->setInput($asset->getContent());
109  }
110 
111  $asset->setContent($proc->getOutput());
112  }
113 
117  public function filterDump(AssetInterface $asset)
118  {
119  }
120 
121  public function getChildren(AssetFactory $factory, $content, $loadPath = null)
122  {
123  // todo
124  return array();
125  }
126 }
static createTemporaryFile($prefix)
Creates a temporary file.
static fromProcess(Process $proc)
$code
Definition: example_050.php:99
setCompress($compress)
Enable output compression.
getSourcePath()
Returns the relative path for the source asset.
filterLoad(AssetInterface $asset)
{Filters an asset after it has been loaded.An asset}
setNodePaths(array $nodePaths)
filterDump(AssetInterface $asset)
{Filters an asset just before it&#39;s dumped.An asset}
createProcessBuilder(array $arguments=array())
setUseNib($useNib)
Enable the use of Nib.
getContent()
Returns the loaded content of the current asset.
getChildren(AssetFactory $factory, $content, $loadPath=null)
Returns child assets.
e($cmd)
Definition: flush.php:14
getSourceDirectory()
Returns the asset&#39;s source directory.
An asset has a mutable URL and content and can be loaded and dumped.
Create styles array
The data for the language used.
__construct($nodeBin='/usr/bin/node', array $nodePaths=array())
Constructs filter.
The asset factory creates asset objects.
A filter that knows how to extract dependencies.
setContent($content)
Sets the content of the current asset.
const EOF
How fgetc() reports an End Of File.
Definition: JSMin_lib.php:92