Skip to content

Commit a1fa961

Browse files
committed
phpcc update
1 parent 13b53bc commit a1fa961

File tree

7 files changed

+351
-11
lines changed

7 files changed

+351
-11
lines changed

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ You can view information about Google Closure Compiler on this [link](https://de
77

88
## How to use
99

10-
Download **phpcc** Library. and then include <code>phpcc.inc</code> in your <code>.php</code> file. you can test <code>sample.php</code>.
10+
Download **phpcc** Library. and then include <code>phpcc.php</code> in your <code>.php</code> file. You can test <code>sample.php</code> in <code>samples</code> folder.
1111

1212
<pre>
13-
include 'src/phpcc.inc';
13+
include 'src/phpcc.php';
1414

1515
$phpcc = new phpcc(array(
1616
'java_file' => 'YOUR JAVA FILE PATH',
17-
'jar_file' => 'src/compiler/compiler.jar',
17+
'jar_file' => '../src/compiler/compiler.jar',
1818
'output_path' => './output/',
1919
'optimization' => 'SIMPLE_OPTIMIZATIONS'
2020
));
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

sample.php renamed to samples/sample.php

+8-8
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
/**
2020
*include library
2121
*/
22-
include_once 'src/phpcc.inc';
22+
include_once '../src/phpcc.php';
2323

2424
/**
2525
* java_file
@@ -45,8 +45,8 @@
4545
* @var phpcc
4646
*/
4747
$phpcc = new phpcc(array(
48-
'java_file' => 'YOUR JAVA FILE PATH',
49-
'jar_file' => 'src/compiler/compiler.jar',
48+
'java_file' => 'YOUR_JAVA_FILE_PATH',
49+
'jar_file' => '../src/compiler/compiler.jar',
5050
'output_path' => './output/',
5151
'optimization' => 'SIMPLE_OPTIMIZATIONS',
5252
'charset' => 'utf-8'
@@ -60,11 +60,11 @@
6060
/**
6161
* Add jQuery file and combined compression
6262
*/
63-
// $ary_result = $phpcc
64-
// ->add("js/jquery-1.10.2.js")
65-
// ->add("js/1.9/jquery-1.9.1.js")
66-
// ->exec("all.js");
67-
// print_r($ary_result);
63+
$ary_result = $phpcc
64+
->add("js/jquery-1.10.2.js")
65+
->add("js/1.9/jquery-1.9.1.js")
66+
->exec("all.js");
67+
print_r($ary_result);
6868

6969
/**
7070
* Add jQuery file and individual compression

src/phpcc.php

+340
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,340 @@
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

Comments
 (0)