~ubuntu-branches/debian/jessie/assetic/jessie

« back to all changes in this revision

Viewing changes to src/Assetic/Filter/LessFilter.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
use Assetic\Factory\AssetFactory;
 
17
use Assetic\Util\LessUtils;
 
18
 
 
19
/**
 
20
 * Loads LESS files.
 
21
 *
 
22
 * @link http://lesscss.org/
 
23
 * @author Kris Wallsmith <kris.wallsmith@gmail.com>
 
24
 */
 
25
class LessFilter extends BaseNodeFilter implements DependencyExtractorInterface
 
26
{
 
27
    private $nodeBin;
 
28
 
 
29
    /**
 
30
     * @var array
 
31
     */
 
32
    private $treeOptions;
 
33
 
 
34
    /**
 
35
     * @var array
 
36
     */
 
37
    private $parserOptions;
 
38
 
 
39
    /**
 
40
     * Load Paths
 
41
     *
 
42
     * A list of paths which less will search for includes.
 
43
     *
 
44
     * @var array
 
45
     */
 
46
    protected $loadPaths = array();
 
47
 
 
48
    /**
 
49
     * Constructor.
 
50
     *
 
51
     * @param string $nodeBin   The path to the node binary
 
52
     * @param array  $nodePaths An array of node paths
 
53
     */
 
54
    public function __construct($nodeBin = '/usr/bin/node', array $nodePaths = array())
 
55
    {
 
56
        $this->nodeBin = $nodeBin;
 
57
        $this->setNodePaths($nodePaths);
 
58
        $this->treeOptions = array();
 
59
        $this->parserOptions = array();
 
60
    }
 
61
 
 
62
    /**
 
63
     * @param bool $compress
 
64
     */
 
65
    public function setCompress($compress)
 
66
    {
 
67
        $this->addTreeOption('compress', $compress);
 
68
    }
 
69
 
 
70
    public function setLoadPaths(array $loadPaths)
 
71
    {
 
72
        $this->loadPaths = $loadPaths;
 
73
    }
 
74
 
 
75
    /**
 
76
     * Adds a path where less will search for includes
 
77
     *
 
78
     * @param string $path Load path (absolute)
 
79
     */
 
80
    public function addLoadPath($path)
 
81
    {
 
82
        $this->loadPaths[] = $path;
 
83
    }
 
84
 
 
85
    /**
 
86
     * @param string $code
 
87
     * @param string $value
 
88
     */
 
89
    public function addTreeOption($code, $value)
 
90
    {
 
91
        $this->treeOptions[$code] = $value;
 
92
    }
 
93
 
 
94
    /**
 
95
     * @param string $code
 
96
     * @param string $value
 
97
     */
 
98
    public function addParserOption($code, $value)
 
99
    {
 
100
        $this->parserOptions[$code] = $value;
 
101
    }
 
102
 
 
103
    public function filterLoad(AssetInterface $asset)
 
104
    {
 
105
        static $format = <<<'EOF'
 
106
var less = require('less');
 
107
var sys  = require(process.binding('natives').util ? 'util' : 'sys');
 
108
 
 
109
new(less.Parser)(%s).parse(%s, function(e, tree) {
 
110
    if (e) {
 
111
        less.writeError(e);
 
112
        process.exit(2);
 
113
    }
 
114
 
 
115
    try {
 
116
        sys.print(tree.toCSS(%s));
 
117
    } catch (e) {
 
118
        less.writeError(e);
 
119
        process.exit(3);
 
120
    }
 
121
});
 
122
 
 
123
EOF;
 
124
 
 
125
        $root = $asset->getSourceRoot();
 
126
        $path = $asset->getSourcePath();
 
127
 
 
128
        // parser options
 
129
        $parserOptions = $this->parserOptions;
 
130
        if ($root && $path) {
 
131
            $parserOptions['paths'] = array(dirname($root.'/'.$path));
 
132
            $parserOptions['filename'] = basename($path);
 
133
        }
 
134
 
 
135
        foreach ($this->loadPaths as $loadPath) {
 
136
            $parserOptions['paths'][] = $loadPath;
 
137
        }
 
138
 
 
139
        $pb = $this->createProcessBuilder();
 
140
 
 
141
        $pb->add($this->nodeBin)->add($input = tempnam(sys_get_temp_dir(), 'assetic_less'));
 
142
        file_put_contents($input, sprintf($format,
 
143
            json_encode($parserOptions),
 
144
            json_encode($asset->getContent()),
 
145
            json_encode($this->treeOptions)
 
146
        ));
 
147
 
 
148
        $proc = $pb->getProcess();
 
149
        $code = $proc->run();
 
150
        unlink($input);
 
151
 
 
152
        if (0 !== $code) {
 
153
            throw FilterException::fromProcess($proc)->setInput($asset->getContent());
 
154
        }
 
155
 
 
156
        $asset->setContent($proc->getOutput());
 
157
    }
 
158
 
 
159
    public function filterDump(AssetInterface $asset)
 
160
    {
 
161
    }
 
162
 
 
163
    /**
 
164
     * @todo support for import-once
 
165
     * @todo support for import (less) "lib.css"
 
166
     */
 
167
    public function getChildren(AssetFactory $factory, $content, $loadPath = null)
 
168
    {
 
169
        $loadPaths = $this->loadPaths;
 
170
        if (null !== $loadPath) {
 
171
            $loadPaths[] = $loadPath;
 
172
        }
 
173
 
 
174
        if (empty($loadPaths)) {
 
175
            return array();
 
176
        }
 
177
 
 
178
        $children = array();
 
179
        foreach (LessUtils::extractImports($content) as $reference) {
 
180
            if ('.css' === substr($reference, -4)) {
 
181
                // skip normal css imports
 
182
                // todo: skip imports with media queries
 
183
                continue;
 
184
            }
 
185
 
 
186
            if ('.less' !== substr($reference, -5)) {
 
187
                $reference .= '.less';
 
188
            }
 
189
 
 
190
            foreach ($loadPaths as $loadPath) {
 
191
                if (file_exists($file = $loadPath.'/'.$reference)) {
 
192
                    $coll = $factory->createAsset($file, array(), array('root' => $loadPath));
 
193
                    foreach ($coll as $leaf) {
 
194
                        $leaf->ensureFilter($this);
 
195
                        $children[] = $leaf;
 
196
                        goto next_reference;
 
197
                    }
 
198
                }
 
199
            }
 
200
 
 
201
            next_reference:
 
202
        }
 
203
 
 
204
        return $children;
 
205
    }
 
206
}