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\Sass; |
---|
13 | |
---|
14 | use Assetic\Asset\AssetInterface; |
---|
15 | use Assetic\Filter\FilterInterface; |
---|
16 | use Assetic\Exception\FilterException; |
---|
17 | use Symfony\Component\Process\ProcessBuilder; |
---|
18 | |
---|
19 | /** |
---|
20 | * Loads SASS files. |
---|
21 | * |
---|
22 | * @link http://sass-lang.com/ |
---|
23 | * @author Kris Wallsmith <kris.wallsmith@gmail.com> |
---|
24 | */ |
---|
25 | class SassFilter implements FilterInterface |
---|
26 | { |
---|
27 | const STYLE_NESTED = 'nested'; |
---|
28 | const STYLE_EXPANDED = 'expanded'; |
---|
29 | const STYLE_COMPACT = 'compact'; |
---|
30 | const STYLE_COMPRESSED = 'compressed'; |
---|
31 | |
---|
32 | private $sassPath; |
---|
33 | private $rubyPath; |
---|
34 | private $unixNewlines; |
---|
35 | private $scss; |
---|
36 | private $style; |
---|
37 | private $quiet; |
---|
38 | private $debugInfo; |
---|
39 | private $lineNumbers; |
---|
40 | private $loadPaths = array(); |
---|
41 | private $cacheLocation; |
---|
42 | private $noCache; |
---|
43 | private $compass; |
---|
44 | |
---|
45 | public function __construct($sassPath = '/usr/bin/sass', $rubyPath = null) |
---|
46 | { |
---|
47 | $this->sassPath = $sassPath; |
---|
48 | $this->rubyPath = $rubyPath; |
---|
49 | $this->cacheLocation = realpath(sys_get_temp_dir()); |
---|
50 | } |
---|
51 | |
---|
52 | public function setUnixNewlines($unixNewlines) |
---|
53 | { |
---|
54 | $this->unixNewlines = $unixNewlines; |
---|
55 | } |
---|
56 | |
---|
57 | public function setScss($scss) |
---|
58 | { |
---|
59 | $this->scss = $scss; |
---|
60 | } |
---|
61 | |
---|
62 | public function setStyle($style) |
---|
63 | { |
---|
64 | $this->style = $style; |
---|
65 | } |
---|
66 | |
---|
67 | public function setQuiet($quiet) |
---|
68 | { |
---|
69 | $this->quiet = $quiet; |
---|
70 | } |
---|
71 | |
---|
72 | public function setDebugInfo($debugInfo) |
---|
73 | { |
---|
74 | $this->debugInfo = $debugInfo; |
---|
75 | } |
---|
76 | |
---|
77 | public function setLineNumbers($lineNumbers) |
---|
78 | { |
---|
79 | $this->lineNumbers = $lineNumbers; |
---|
80 | } |
---|
81 | |
---|
82 | public function addLoadPath($loadPath) |
---|
83 | { |
---|
84 | $this->loadPaths[] = $loadPath; |
---|
85 | } |
---|
86 | |
---|
87 | public function setCacheLocation($cacheLocation) |
---|
88 | { |
---|
89 | $this->cacheLocation = $cacheLocation; |
---|
90 | } |
---|
91 | |
---|
92 | public function setNoCache($noCache) |
---|
93 | { |
---|
94 | $this->noCache = $noCache; |
---|
95 | } |
---|
96 | |
---|
97 | public function setCompass($compass) |
---|
98 | { |
---|
99 | $this->compass = $compass; |
---|
100 | } |
---|
101 | |
---|
102 | public function filterLoad(AssetInterface $asset) |
---|
103 | { |
---|
104 | $sassProcessArgs = array($this->sassPath); |
---|
105 | if (null !== $this->rubyPath) { |
---|
106 | $sassProcessArgs = array_merge(explode(' ', $this->rubyPath), $sassProcessArgs); |
---|
107 | } |
---|
108 | |
---|
109 | $pb = new ProcessBuilder($sassProcessArgs); |
---|
110 | |
---|
111 | $root = $asset->getSourceRoot(); |
---|
112 | $path = $asset->getSourcePath(); |
---|
113 | |
---|
114 | if ($root && $path) { |
---|
115 | $pb->add('--load-path')->add(dirname($root.'/'.$path)); |
---|
116 | } |
---|
117 | |
---|
118 | if ($this->unixNewlines) { |
---|
119 | $pb->add('--unix-newlines'); |
---|
120 | } |
---|
121 | |
---|
122 | if (true === $this->scss || (null === $this->scss && 'scss' == pathinfo($path, PATHINFO_EXTENSION))) { |
---|
123 | $pb->add('--scss'); |
---|
124 | } |
---|
125 | |
---|
126 | if ($this->style) { |
---|
127 | $pb->add('--style')->add($this->style); |
---|
128 | } |
---|
129 | |
---|
130 | if ($this->quiet) { |
---|
131 | $pb->add('--quiet'); |
---|
132 | } |
---|
133 | |
---|
134 | if ($this->debugInfo) { |
---|
135 | $pb->add('--debug-info'); |
---|
136 | } |
---|
137 | |
---|
138 | if ($this->lineNumbers) { |
---|
139 | $pb->add('--line-numbers'); |
---|
140 | } |
---|
141 | |
---|
142 | foreach ($this->loadPaths as $loadPath) { |
---|
143 | $pb->add('--load-path')->add($loadPath); |
---|
144 | } |
---|
145 | |
---|
146 | if ($this->cacheLocation) { |
---|
147 | $pb->add('--cache-location')->add($this->cacheLocation); |
---|
148 | } |
---|
149 | |
---|
150 | if ($this->noCache) { |
---|
151 | $pb->add('--no-cache'); |
---|
152 | } |
---|
153 | |
---|
154 | if ($this->compass) { |
---|
155 | $pb->add('--compass'); |
---|
156 | } |
---|
157 | |
---|
158 | // input |
---|
159 | $pb->add($input = tempnam(sys_get_temp_dir(), 'assetic_sass')); |
---|
160 | file_put_contents($input, $asset->getContent()); |
---|
161 | |
---|
162 | $proc = $pb->getProcess(); |
---|
163 | $code = $proc->run(); |
---|
164 | unlink($input); |
---|
165 | |
---|
166 | if (0 < $code) { |
---|
167 | throw FilterException::fromProcess($proc)->setInput($asset->getContent()); |
---|
168 | } |
---|
169 | |
---|
170 | $asset->setContent($proc->getOutput()); |
---|
171 | } |
---|
172 | |
---|
173 | public function filterDump(AssetInterface $asset) |
---|
174 | { |
---|
175 | } |
---|
176 | } |
---|