source: sandbox/newExpressoMail/newExpressoMail/Assetic/Filter/LessFilter.php @ 7265

Revision 7265, 3.2 KB checked in by gustavo, 12 years ago (diff)

Ticket #0000 - Criado novo modulo para o desenvolvimento do novo ExpressoMail?

  • Property svn:executable set to *
Line 
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
12namespace Assetic\Filter;
13
14use Assetic\Asset\AssetInterface;
15use Assetic\Exception\FilterException;
16use Symfony\Component\Process\ProcessBuilder;
17
18/**
19 * Loads LESS files.
20 *
21 * @link http://lesscss.org/
22 * @author Kris Wallsmith <kris.wallsmith@gmail.com>
23 */
24class LessFilter implements FilterInterface
25{
26    private $nodeBin;
27    private $nodePaths;
28    private $compress;
29
30    /**
31     * Load Paths
32     *
33     * A list of paths which less will search for includes.
34     *
35     * @var array
36     */
37    protected $loadPaths = array();
38
39    /**
40     * Constructor.
41     *
42     * @param string $nodeBin   The path to the node binary
43     * @param array  $nodePaths An array of node paths
44     */
45    public function __construct($nodeBin = '/usr/bin/node', array $nodePaths = array())
46    {
47        $this->nodeBin = $nodeBin;
48        $this->nodePaths = $nodePaths;
49    }
50
51    public function setCompress($compress)
52    {
53        $this->compress = $compress;
54    }
55
56    /**
57     * Adds a path where less will search for includes
58     *
59     * @param string $path Load path (absolute)
60     */
61    public function addLoadPath($path)
62    {
63        $this->loadPaths[] = $path;
64    }
65
66    public function filterLoad(AssetInterface $asset)
67    {
68        static $format = <<<'EOF'
69var less = require('less');
70var sys  = require(process.binding('natives').util ? 'util' : 'sys');
71
72new(less.Parser)(%s).parse(%s, function(e, tree) {
73    if (e) {
74        less.writeError(e);
75        process.exit(2);
76    }
77
78    try {
79        sys.print(tree.toCSS(%s));
80    } catch (e) {
81        less.writeError(e);
82        process.exit(3);
83    }
84});
85
86EOF;
87
88        $root = $asset->getSourceRoot();
89        $path = $asset->getSourcePath();
90
91        // parser options
92        $parserOptions = array();
93        if ($root && $path) {
94            $parserOptions['paths'] = array(dirname($root.'/'.$path));
95            $parserOptions['filename'] = basename($path);
96        }
97        foreach ($this->loadPaths as $loadPath) {
98            $parserOptions['paths'][] = $loadPath;
99        }
100
101        // tree options
102        $treeOptions = array();
103        if (null !== $this->compress) {
104            $treeOptions['compress'] = $this->compress;
105        }
106
107        $pb = new ProcessBuilder();
108        $pb->inheritEnvironmentVariables();
109
110        // node.js configuration
111        if (0 < count($this->nodePaths)) {
112            $pb->setEnv('NODE_PATH', implode(':', $this->nodePaths));
113        }
114
115        $pb->add($this->nodeBin)->add($input = tempnam(sys_get_temp_dir(), 'assetic_less'));
116        file_put_contents($input, sprintf($format,
117            json_encode($parserOptions),
118            json_encode($asset->getContent()),
119            json_encode($treeOptions)
120        ));
121
122        $proc = $pb->getProcess();
123        $code = $proc->run();
124        unlink($input);
125
126        if (0 < $code) {
127            throw FilterException::fromProcess($proc)->setInput($asset->getContent());
128        }
129
130        $asset->setContent($proc->getOutput());
131    }
132
133    public function filterDump(AssetInterface $asset)
134    {
135    }
136}
Note: See TracBrowser for help on using the repository browser.