/
CoffeeScriptFilter.php
83 lines (66 loc) · 1.85 KB
/
CoffeeScriptFilter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?php
/*
* This file is part of the Assetic package, an OpenSky project.
*
* (c) 2010-2014 OpenSky Project Inc
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Assetic\Filter;
use Assetic\Asset\AssetInterface;
use Assetic\Exception\FilterException;
use Assetic\Util\FilesystemUtils;
/**
* Compiles CoffeeScript into Javascript.
*
* @link http://coffeescript.org/
* @author Kris Wallsmith <kris.wallsmith@gmail.com>
*/
class CoffeeScriptFilter extends BaseNodeFilter
{
private $coffeeBin;
private $nodeBin;
// coffee options
private $bare;
private $noHeader;
public function __construct($coffeeBin = '/usr/bin/coffee', $nodeBin = null)
{
$this->coffeeBin = $coffeeBin;
$this->nodeBin = $nodeBin;
}
public function setBare($bare)
{
$this->bare = $bare;
}
public function setNoHeader($noHeader)
{
$this->noHeader = $noHeader;
}
public function filterLoad(AssetInterface $asset)
{
$input = FilesystemUtils::createTemporaryFile('coffee');
file_put_contents($input, $asset->getContent());
$pb = $this->createProcessBuilder($this->nodeBin
? array($this->nodeBin, $this->coffeeBin)
: array($this->coffeeBin));
$pb->add('-cp');
if ($this->bare) {
$pb->add('--bare');
}
if ($this->noHeader) {
$pb->add('--no-header');
}
$pb->add($input);
$proc = $pb->getProcess();
$code = $proc->run();
unlink($input);
if (0 !== $code) {
throw FilterException::fromProcess($proc)->setInput($asset->getContent());
}
$asset->setContent($proc->getOutput());
}
public function filterDump(AssetInterface $asset)
{
}
}