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; |
---|
13 | |
---|
14 | use Assetic\Asset\AssetInterface; |
---|
15 | use Assetic\Exception\FilterException; |
---|
16 | use Symfony\Component\Process\ProcessBuilder; |
---|
17 | |
---|
18 | /** |
---|
19 | * UglifyCss filter. |
---|
20 | * |
---|
21 | * @link https://github.com/fmarcia/UglifyCSS |
---|
22 | * @author Franck Marcia <franck.marcia@gmail.com> |
---|
23 | */ |
---|
24 | class UglifyCssFilter implements FilterInterface |
---|
25 | { |
---|
26 | private $uglifyCssPath; |
---|
27 | private $nodeJsPath; |
---|
28 | |
---|
29 | private $expandVars; |
---|
30 | private $uglyComments; |
---|
31 | private $cuteComments; |
---|
32 | |
---|
33 | /** |
---|
34 | * @param string $uglifyCssPath Absolute path to the uglifycss executable |
---|
35 | * @param string $nodeJsPath Absolute path to the folder containg node.js executable |
---|
36 | */ |
---|
37 | public function __construct($uglifyCssPath, $nodeJsPath = null) |
---|
38 | { |
---|
39 | $this->uglifyCssPath = $uglifyCssPath; |
---|
40 | $this->nodeJsPath = $nodeJsPath; |
---|
41 | } |
---|
42 | |
---|
43 | /** |
---|
44 | * Expand variables |
---|
45 | * @param bool $expandVars True to enable |
---|
46 | */ |
---|
47 | public function setExpandVars($expandVars) |
---|
48 | { |
---|
49 | $this->expandVars = $expandVars; |
---|
50 | } |
---|
51 | |
---|
52 | /** |
---|
53 | * Remove newlines within preserved comments |
---|
54 | * @param bool $uglyComments True to enable |
---|
55 | */ |
---|
56 | public function setUglyComments($uglyComments) |
---|
57 | { |
---|
58 | $this->uglyComments = $uglyComments; |
---|
59 | } |
---|
60 | |
---|
61 | /** |
---|
62 | * Preserve newlines within and around preserved comments |
---|
63 | * @param bool $cuteComments True to enable |
---|
64 | */ |
---|
65 | public function setCuteComments($cuteComments) |
---|
66 | { |
---|
67 | $this->cuteComments = $cuteComments; |
---|
68 | } |
---|
69 | |
---|
70 | /** |
---|
71 | * @see Assetic\Filter\FilterInterface::filterLoad() |
---|
72 | */ |
---|
73 | public function filterLoad(AssetInterface $asset) |
---|
74 | { |
---|
75 | } |
---|
76 | |
---|
77 | /** |
---|
78 | * Run the asset through UglifyJs |
---|
79 | * |
---|
80 | * @see Assetic\Filter\FilterInterface::filterDump() |
---|
81 | */ |
---|
82 | public function filterDump(AssetInterface $asset) |
---|
83 | { |
---|
84 | $executables = array(); |
---|
85 | |
---|
86 | if ($this->nodeJsPath !== null) { |
---|
87 | $executables[] = $this->nodeJsPath; |
---|
88 | } |
---|
89 | |
---|
90 | $executables[] = $this->uglifyCssPath; |
---|
91 | |
---|
92 | $pb = new ProcessBuilder($executables); |
---|
93 | |
---|
94 | if ($this->expandVars) { |
---|
95 | $pb->add('--expand-vars'); |
---|
96 | } |
---|
97 | |
---|
98 | if ($this->uglyComments) { |
---|
99 | $pb->add('--ugly-comments'); |
---|
100 | } |
---|
101 | |
---|
102 | if ($this->cuteComments) { |
---|
103 | $pb->add('--cute-comments'); |
---|
104 | } |
---|
105 | |
---|
106 | // input and output files |
---|
107 | $input = tempnam(sys_get_temp_dir(), 'input'); |
---|
108 | |
---|
109 | file_put_contents($input, $asset->getContent()); |
---|
110 | $pb->add($input); |
---|
111 | |
---|
112 | $proc = $pb->getProcess(); |
---|
113 | $code = $proc->run(); |
---|
114 | unlink($input); |
---|
115 | |
---|
116 | if (127 === $code) { |
---|
117 | throw new \RuntimeException('Path to node executable could not be resolved.'); |
---|
118 | } |
---|
119 | |
---|
120 | if (0 < $code) { |
---|
121 | throw FilterException::fromProcess($proc)->setInput($asset->getContent()); |
---|
122 | } |
---|
123 | |
---|
124 | $asset->setContent($proc->getOutput()); |
---|
125 | } |
---|
126 | } |
---|