~ubuntu-branches/debian/stretch/assetic/stretch

« back to all changes in this revision

Viewing changes to src/Assetic/Filter/CoffeeScriptFilter.php

  • Committer: Package Import Robot
  • Author(s): David Prévot
  • Date: 2014-04-21 12:48:05 UTC
  • Revision ID: package-import@ubuntu.com-20140421124805-y9ri97838g33fo9z
Tags: upstream-1.1.2
Import upstream version 1.1.2

Show diffs side-by-side

added added

removed removed

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