1
+ <?php
2
+
3
+ /**
4
+ * Copyright tureki.org [tureki11@gmail.com]
5
+ *
6
+ * Licensed under the Apache License, Version 2.0 (the "License");
7
+ * you may not use this file except in compliance with the License.
8
+ * You may obtain a copy of the License at
9
+ *
10
+ * http://www.apache.org/licenses/LICENSE-2.0
11
+ *
12
+ * Unless required by applicable law or agreed to in writing, software
13
+ * distributed under the License is distributed on an "AS IS" BASIS,
14
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ * See the License for the specific language governing permissions and
16
+ * limitations under the License.
17
+ */
18
+
19
+ /**
20
+ * PHP Closure Compiler
21
+ * A PHP Library to use Google Closure Compiler compress Javascript
22
+ *
23
+ * @copyright tureki.org
24
+ * @author tureki
25
+ *
26
+ **/
27
+ class phpcc {
28
+
29
+ public function __construct ($ options )
30
+ {
31
+
32
+ $ this ->options = array (
33
+ 'java_file ' => 'java ' ,
34
+ 'jar_file ' => 'compiler.jar ' ,
35
+ 'output_path ' => '/ ' ,
36
+ 'optimization ' => 'SIMPLE_OPTIMIZATIONS ' ,
37
+ 'charset ' => 'utf-8 '
38
+ );
39
+
40
+ $ this ->options = array_merge ($ this ->options ,$ options );
41
+
42
+ $ this ->reset ();
43
+
44
+ }
45
+
46
+ /**
47
+ * Add javascript file to compiler list
48
+ *
49
+ * @param string $file
50
+ * @return class phpcc
51
+ */
52
+ public function add ($ file )
53
+ {
54
+
55
+ if (is_array ($ file )){
56
+
57
+ $ this ->js_files = array_merge ($ this ->js_files ,$ file );
58
+
59
+ }else {
60
+
61
+ $ this ->js_files [] = $ file ;
62
+
63
+ }
64
+
65
+ return $ this ;
66
+
67
+ }
68
+
69
+ /**
70
+ * Execute compiler.
71
+ *
72
+ * @param string $filename
73
+ * @return class phpcc
74
+ */
75
+ public function exec ($ filename = 'all.min.js ' )
76
+ {
77
+
78
+ $ str_file = '' ;
79
+
80
+ if ($ this ->bol_single ){
81
+
82
+ $ ary_result = array ();
83
+
84
+ $ num_js = count ($ this ->js_files );
85
+
86
+ for ($ i =0 ; $ i < $ num_js ; $ i ++) {
87
+
88
+ $ str_file = ' --js ' . $ this ->js_files [$ i ];
89
+
90
+ $ filename = basename ($ this ->js_files [$ i ]);
91
+
92
+ $ ary_result [] = $ this ->_get_argv ($ str_file ,$ filename );
93
+
94
+ }
95
+
96
+ $ num_js = count ($ this ->js_files_dir );
97
+
98
+ for ($ i =0 ; $ i < $ num_js ; $ i ++) {
99
+
100
+ $ str_file = ' --js ' . $ this ->js_files_dir [$ i ];
101
+ echo $ this ->js_files_dir [$ i ] ."| " . $ this ->js_dir . "\n" ;
102
+ $ filename = str_replace ($ this ->js_dir , '' , $ this ->js_files_dir [$ i ]);
103
+ echo $ filename ."\n" ;
104
+ $ ary_result [] = $ this ->_get_argv ($ str_file ,$ filename );
105
+
106
+ }
107
+
108
+
109
+ return $ ary_result ;
110
+
111
+ }else {
112
+
113
+ if (count ($ this ->js_files_dir )>0 ){
114
+
115
+ $ this ->js_files = array_merge ($ this ->js_files ,$ this ->js_files_dir );
116
+
117
+ }
118
+
119
+ $ this ->js_files = array_unique ($ this ->js_files );
120
+
121
+ sort ($ this ->js_files );
122
+
123
+ $ num_js = count ($ this ->js_files );
124
+
125
+ for ($ i =0 ; $ i < $ num_js ; $ i ++) {
126
+
127
+ $ str_file .= ' --js ' . $ this ->js_files [$ i ];
128
+
129
+ }
130
+
131
+ return $ this ->_get_argv ($ str_file ,$ filename );
132
+
133
+ }
134
+
135
+ }
136
+
137
+ /**
138
+ * Help method will return "Closure Compiler --help" when setting success
139
+ *
140
+ * @return array
141
+ */
142
+ public function help ()
143
+ {
144
+
145
+ $ str_cmd = $ this ->_get_cmd () . ' --help ' ;
146
+
147
+ return $ this ->_exec ($ str_cmd );
148
+
149
+ }
150
+
151
+ /**
152
+ * Compress all js to one file.
153
+ *
154
+ * @return class phpcc
155
+ */
156
+ public function merge ()
157
+ {
158
+
159
+ $ this ->bol_single = false ;
160
+
161
+ return $ this ;
162
+
163
+ }
164
+
165
+ /**
166
+ * Add command param. exp:--angular_pass
167
+ *
168
+ * @param string $param
169
+ * @param string $value
170
+ * @return class phpcc
171
+ */
172
+ public function param ($ param ,$ value =null )
173
+ {
174
+
175
+ if ($ value ){
176
+
177
+ $ str_param = $ param ." " . $ value ;
178
+
179
+ }else {
180
+
181
+ $ str_param = $ param ;
182
+
183
+ }
184
+
185
+ $ this ->shell_params [] = $ str_param ;
186
+
187
+ return $ this ;
188
+
189
+ }
190
+
191
+ /**
192
+ * Reset all setting.
193
+ *
194
+ * @return class phpcc
195
+ */
196
+ public function reset ()
197
+ {
198
+
199
+ $ this ->bol_single = false ;
200
+
201
+ $ this ->js_dir = '' ;
202
+
203
+ $ this ->js_files = array ();
204
+
205
+ $ this ->js_files_dir = array ();
206
+
207
+ $ this ->shell_params = array ();
208
+
209
+ return $ this ;
210
+
211
+ }
212
+
213
+ /**
214
+ * Set directory you want to compiler
215
+ *
216
+ * @param string $path
217
+ * @param array $ext
218
+ * @return class phpcc
219
+ */
220
+ public function setDir ($ path ,$ ext =array ('js ' ))
221
+ {
222
+
223
+ $ this ->js_dir = $ path ;
224
+
225
+ $ cls_objects = new RecursiveIteratorIterator (
226
+ new RecursiveDirectoryIterator ($ path )
227
+ );
228
+
229
+ foreach ($ cls_objects as $ str_name ){
230
+
231
+ $ str_filetype = pathinfo ($ str_name , PATHINFO_EXTENSION );
232
+
233
+ if (in_array (strtolower ($ str_filetype ), $ ext )) {
234
+
235
+ $ this ->js_files_dir [] = $ str_name ;
236
+
237
+ }
238
+
239
+ }
240
+
241
+ return $ this ;
242
+
243
+ }
244
+
245
+ /**
246
+ * Do not merge javascript files
247
+ *
248
+ * @return class phpcc
249
+ */
250
+ public function single ()
251
+ {
252
+
253
+ $ this ->bol_single = true ;
254
+
255
+ return $ this ;
256
+
257
+ }
258
+
259
+ private function _create ($ file_path )
260
+ {
261
+
262
+ if (!file_exists ($ file_path )){
263
+
264
+ $ pathinfo = pathinfo ($ file_path );
265
+
266
+ $ path = $ pathinfo ["dirname " ];
267
+
268
+ try {
269
+ if (!file_exists ($ path ) || !is_writeable ($ path )){
270
+ mkdir ($ path , 0777 ,true );
271
+ touch ($ file_path );
272
+ }
273
+ return true ;
274
+ }catch (Exception $ e ){
275
+ return false ;
276
+ }
277
+
278
+ }
279
+
280
+ }
281
+
282
+ private function _exec ($ str_cmd )
283
+ {
284
+
285
+ exec ($ str_cmd . ' 2>&1 ' , $ out , $ status );
286
+
287
+ return array (
288
+ 'shell ' => $ str_cmd ,
289
+ 'out ' => $ out ,
290
+ 'status ' => $ status
291
+ );
292
+
293
+ }
294
+
295
+ private function _get_argv ($ str_file ,$ filename )
296
+ {
297
+
298
+ $ opt = $ this ->_get_options ();
299
+
300
+ $ str_output = $ opt ["output_path " ].$ filename ;
301
+
302
+ $ str_param = implode (" " , $ this ->shell_params );
303
+
304
+ $ str_cmd = $ this ->_get_cmd ();
305
+
306
+ $ str_cmd .= ' ' . $ str_param .' ' . $ str_file ;
307
+
308
+ $ this ->_create ($ str_output );
309
+
310
+ if (!empty ($ str_file )){
311
+ $ str_cmd .=
312
+ ' --compilation_level ' .$ opt ['optimization ' ].
313
+ ' --charset ' .$ opt ['charset ' ].
314
+ ' --js_output_file ' .$ str_output ;
315
+ }
316
+
317
+ return $ this ->_exec ($ str_cmd );
318
+
319
+ }
320
+
321
+ private function _get_cmd ()
322
+ {
323
+
324
+ $ opt = $ this ->_get_options ();
325
+
326
+ $ str_cmd = $ opt ['java_file ' ] . ' -jar ' . $ opt ['jar_file ' ];
327
+
328
+ return $ str_cmd ;
329
+
330
+ }
331
+
332
+ private function _get_options ()
333
+ {
334
+
335
+ return $ this ->options ;
336
+
337
+ }
338
+
339
+ }
340
+ ?>
0 commit comments