1 | <?php |
---|
2 | |
---|
3 | /* |
---|
4 | * This file is part of the Assetic package, an OpenSky project. |
---|
5 | * |
---|
6 | * (c) 2010-2012 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 | |
---|
14 | use Assetic\Asset\AssetInterface; |
---|
15 | use Assetic\Exception\FilterException; |
---|
16 | use Symfony\Component\Process\ProcessBuilder; |
---|
17 | |
---|
18 | /** |
---|
19 | * Compiles CoffeeScript into Javascript. |
---|
20 | * |
---|
21 | * @link http://coffeescript.org/ |
---|
22 | * @author Kris Wallsmith <kris.wallsmith@gmail.com> |
---|
23 | */ |
---|
24 | class CoffeeScriptFilter implements FilterInterface |
---|
25 | { |
---|
26 | private $coffeePath; |
---|
27 | private $nodePath; |
---|
28 | |
---|
29 | // coffee options |
---|
30 | private $bare; |
---|
31 | |
---|
32 | public function __construct($coffeePath = '/usr/bin/coffee', $nodePath = '/usr/bin/node') |
---|
33 | { |
---|
34 | $this->coffeePath = $coffeePath; |
---|
35 | $this->nodePath = $nodePath; |
---|
36 | } |
---|
37 | |
---|
38 | public function setBare($bare) |
---|
39 | { |
---|
40 | $this->bare = $bare; |
---|
41 | } |
---|
42 | |
---|
43 | public function filterLoad(AssetInterface $asset) |
---|
44 | { |
---|
45 | $input = tempnam(sys_get_temp_dir(), 'assetic_coffeescript'); |
---|
46 | file_put_contents($input, $asset->getContent()); |
---|
47 | |
---|
48 | $pb = new ProcessBuilder(array( |
---|
49 | $this->nodePath, |
---|
50 | $this->coffeePath, |
---|
51 | '-cp', |
---|
52 | )); |
---|
53 | |
---|
54 | if ($this->bare) { |
---|
55 | $pb->add('--bare'); |
---|
56 | } |
---|
57 | |
---|
58 | $pb->add($input); |
---|
59 | $proc = $pb->getProcess(); |
---|
60 | $code = $proc->run(); |
---|
61 | unlink($input); |
---|
62 | |
---|
63 | if (0 < $code) { |
---|
64 | throw FilterException::fromProcess($proc)->setInput($asset->getContent()); |
---|
65 | } |
---|
66 | |
---|
67 | $asset->setContent($proc->getOutput()); |
---|
68 | } |
---|
69 | |
---|
70 | public function filterDump(AssetInterface $asset) |
---|
71 | { |
---|
72 | } |
---|
73 | } |
---|