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\GoogleClosure; |
---|
13 | |
---|
14 | use Assetic\Asset\AssetInterface; |
---|
15 | use Assetic\Exception\FilterException; |
---|
16 | use Symfony\Component\Process\ProcessBuilder; |
---|
17 | |
---|
18 | /** |
---|
19 | * Filter for the Google Closure Compiler JAR. |
---|
20 | * |
---|
21 | * @link https://developers.google.com/closure/compiler/ |
---|
22 | * @author Kris Wallsmith <kris.wallsmith@gmail.com> |
---|
23 | */ |
---|
24 | class CompilerJarFilter extends BaseCompilerFilter |
---|
25 | { |
---|
26 | private $jarPath; |
---|
27 | private $javaPath; |
---|
28 | |
---|
29 | public function __construct($jarPath, $javaPath = '/usr/bin/java') |
---|
30 | { |
---|
31 | $this->jarPath = $jarPath; |
---|
32 | $this->javaPath = $javaPath; |
---|
33 | } |
---|
34 | |
---|
35 | public function filterDump(AssetInterface $asset) |
---|
36 | { |
---|
37 | $cleanup = array(); |
---|
38 | |
---|
39 | $pb = new ProcessBuilder(array( |
---|
40 | $this->javaPath, |
---|
41 | '-jar', |
---|
42 | $this->jarPath, |
---|
43 | )); |
---|
44 | |
---|
45 | if (null !== $this->compilationLevel) { |
---|
46 | $pb->add('--compilation_level')->add($this->compilationLevel); |
---|
47 | } |
---|
48 | |
---|
49 | if (null !== $this->jsExterns) { |
---|
50 | $cleanup[] = $externs = tempnam(sys_get_temp_dir(), 'assetic_google_closure_compiler'); |
---|
51 | file_put_contents($externs, $this->jsExterns); |
---|
52 | $pb->add('--externs')->add($externs); |
---|
53 | } |
---|
54 | |
---|
55 | if (null !== $this->externsUrl) { |
---|
56 | $cleanup[] = $externs = tempnam(sys_get_temp_dir(), 'assetic_google_closure_compiler'); |
---|
57 | file_put_contents($externs, file_get_contents($this->externsUrl)); |
---|
58 | $pb->add('--externs')->add($externs); |
---|
59 | } |
---|
60 | |
---|
61 | if (null !== $this->excludeDefaultExterns) { |
---|
62 | $pb->add('--use_only_custom_externs'); |
---|
63 | } |
---|
64 | |
---|
65 | if (null !== $this->formatting) { |
---|
66 | $pb->add('--formatting')->add($this->formatting); |
---|
67 | } |
---|
68 | |
---|
69 | if (null !== $this->useClosureLibrary) { |
---|
70 | $pb->add('--manage_closure_dependencies'); |
---|
71 | } |
---|
72 | |
---|
73 | if (null !== $this->warningLevel) { |
---|
74 | $pb->add('--warning_level')->add($this->warningLevel); |
---|
75 | } |
---|
76 | |
---|
77 | if (null !== $this->language) { |
---|
78 | $pb->add('--language_in')->add($this->language); |
---|
79 | } |
---|
80 | |
---|
81 | $pb->add('--js')->add($cleanup[] = $input = tempnam(sys_get_temp_dir(), 'assetic_google_closure_compiler')); |
---|
82 | file_put_contents($input, $asset->getContent()); |
---|
83 | |
---|
84 | $proc = $pb->getProcess(); |
---|
85 | $code = $proc->run(); |
---|
86 | array_map('unlink', $cleanup); |
---|
87 | |
---|
88 | if (0 < $code) { |
---|
89 | throw FilterException::fromProcess($proc)->setInput($asset->getContent()); |
---|
90 | } |
---|
91 | |
---|
92 | $asset->setContent($proc->getOutput()); |
---|
93 | } |
---|
94 | } |
---|