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

Revision 7265, 2.7 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 STYL files.
20 *
21 * @link http://learnboost.github.com/stylus/
22 * @author Konstantin Kudryashov <ever.zet@gmail.com>
23 */
24class StylusFilter implements FilterInterface
25{
26    private $nodeBin;
27    private $nodePaths;
28    private $compress;
29
30    /**
31     * Constructs filter.
32     *
33     * @param string $nodeBin   The path to the node binary
34     * @param array  $nodePaths An array of node paths
35     */
36    public function __construct($nodeBin = '/usr/bin/node', array $nodePaths = array())
37    {
38        $this->nodeBin = $nodeBin;
39        $this->nodePaths = $nodePaths;
40    }
41
42    /**
43     * Enable output compression.
44     *
45     * @param boolean $compress
46     */
47    public function setCompress($compress)
48    {
49        $this->compress = $compress;
50    }
51
52    /**
53     * {@inheritdoc}
54     */
55    public function filterLoad(AssetInterface $asset)
56    {
57        static $format = <<<'EOF'
58var stylus = require('stylus');
59var sys    = require(process.binding('natives').util ? 'util' : 'sys');
60
61stylus(%s, %s).render(function(e, css){
62    if (e) {
63        throw e;
64    }
65
66    sys.print(css);
67    process.exit(0);
68});
69
70EOF;
71
72        $root = $asset->getSourceRoot();
73        $path = $asset->getSourcePath();
74
75        // parser options
76        $parserOptions = array();
77        if ($root && $path) {
78            $parserOptions['paths'] = array(dirname($root.'/'.$path));
79            $parserOptions['filename'] = basename($path);
80        }
81
82        if (null !== $this->compress) {
83            $parserOptions['compress'] = $this->compress;
84        }
85
86        $pb = new ProcessBuilder();
87        $pb->inheritEnvironmentVariables();
88
89        // node.js configuration
90        if (0 < count($this->nodePaths)) {
91            $pb->setEnv('NODE_PATH', implode(':', $this->nodePaths));
92        }
93
94        $pb->add($this->nodeBin)->add($input = tempnam(sys_get_temp_dir(), 'assetic_stylus'));
95        file_put_contents($input, sprintf($format,
96            json_encode($asset->getContent()),
97            json_encode($parserOptions)
98        ));
99
100        $proc = $pb->getProcess();
101        $code = $proc->run();
102        unlink($input);
103
104        if (0 < $code) {
105            throw FilterException::fromProcess($proc)->setInput($asset->getContent());
106        }
107
108        $asset->setContent($proc->getOutput());
109    }
110
111    /**
112     * {@inheritdoc}
113     */
114    public function filterDump(AssetInterface $asset)
115    {
116    }
117}
Note: See TracBrowser for help on using the repository browser.