-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCssjscombine.php
1379 lines (1240 loc) · 36 KB
/
Cssjscombine.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* CSS JS Minifier & Combiner
* Simple & Fast CSS & Javascript minifier, built only one files and less than 50 kb (with this comment also :D)
* + URL & Path helper
* just call the call and do it!
*
* tested on:
* - Code Igniter 2 & 3
* - Phalcon
* - Slim2 & 3
* - fat free
* - YII2
* - and many more by our project need
*
* tested javascript file :
* - jquery.min.js
* - https://platform.twitter.com/widgets.js
* - jquery-migrate.min.js
* - jquery-ui.min.js
*
* inspire and use some code from :
* JShrink by - Robert Hafner <tedivm@tedivm.com>
* @see {https://github.com/tedious/JShrink}
*
* The script is for combine and getting aso minifying assets Javascript and CSS
* The script handle and minify css and javascript and combining multiple files
*
* + Successfully about re-minifying of jquery minified version without issues
* + High compression minify
* + Fix for url() css rule ( with custom path or custom url)
* + support allowing conditional comment that start /*! on javascript or you want it remove
* + allow to show first comment to know what the css start for information
*
* Donation via @paypal are wellcome -> nawa@yahoo.com
*
* @package CssJsCombine
* @version 1.0
* @author awan <nawa@yahoo.com>
* @license GPL3/+
* & minifier code has license by author itself @link {https://github.com/tedious/JShrink}
* @revision :
* January 10 2016 GMT+7 :
* - Change internal functions , for conflist handle comment multiple line below
* - Remove Method processOneLineComments()
* - Remove Method processMultiLineComments()
*
* March 19 2016 GMT+7 :
* - fix use comment parameter that invalid called on combine()
* - fix camelcase call combine onmethod combine() array
* - make comment more neat
* - fix invalid comment @package & some invalid comments
* - add magic method __destruct()
*
*/
/**
* CssJsCombine
*
* NOTE :
* If you use Code Igniter you could ->
* Put on your application/([models / library] folder)/Cssjscombine.php
* and call before you run the class
* eg : on your controller index() or any where that you could load it
*
* if you use as library just call
* $this->load->model('cssjscombine');
*
* or you put on library directory
* $this->load->library('cssjscombine');
*
* and to doing minify :
*
* $this->cssjscombine->minifyCSS('csstexthere'); // for css
* $this->cssjscombine->minifyJS('jstexthere'); // for javascript
*
* Or if you want to cobine JS or CSS files from your sub directory of web
* just set with:
*
* $this->cssjscombine->combine(
* array(
* '/full/path/to/your/script/filecss1.css',
* '/full/path/to/your/script/css/filecss2.css',
* '/full/path/to/your/script/css/filecss2.css',
* // etc etc ....
* ),
* false, // (boolean true|false) if you want print first comment if possible to your combined
* // files js / css per files request
* false, // (boolean true|false) if you want print conditional comment if possible to your combined
* // files js only (show conditional comment start with /*! on javascript will be ignored to replaced)
* );
*
* please make sure you have only set if it css list with css files not js
* or you want combine js you must list with file of js.
*
* If you want know what this class functions works, please read the comment on each method / functions.
*
* This script not returning error if failed parsing , but will be record as error_logs
* call :
* (classof) CssJsCombine->getErrorLog();
* to get an array of error
*/
class CssJsCombine
{
/* -------------------------------------------------------------+
* PROPERTY |
* -------------------------------------------------------------+
*/
/**
* Ignored Javascript to minified
* this contains regex , please dont use modifier here!
* eg: if want to add regex ignore all with minified
* just set => (.+).min.js
*
* @var array
*/
protected $ignored_js_minify = [
// '(.+).min.js' => true, # no for min for example
];
/**
* The index_input javascript to be minified.
*
* @var string
*/
protected $index_input;
/**
* Cached Proccess
*
* @var null
*/
protected $all_cached = null;
/**
* The location of the character (in the index_input string) that is next to be
* processed.
*
* @var int
*/
protected $index_position = 0;
/**
* The first of the characters currently being looked at.
*
* @var string
*/
protected $index_a = '';
/**
* The next character being looked at (after a);
*
* @var string
*/
protected $index_b = '';
/**
* This character is only active when certain look ahead actions take place.
*
* @var string
*/
protected $index_c;
/**
* Contains lock ids which are used to replace certain code patterns and
* prevent them from being minified
*
* @var array
*/
protected $index_locks = [];
/**
* Just additional resource record to prevent
* Throws error
*
* @var boolean
*/
protected $is_failed = false;
/**
* Base directory (eg root path of script executed for the right uses)
*
* @var string
*/
protected $base_directory = null;
/**
* Base URL of site
*
* @var string
*/
protected $base_url = null;
/**
* Error Logs
*
* @var array
*/
protected $error_log = [];
/**
* CssJsCombine constructor.
*/
public function __construct()
{
// base directory
// on CI use FCPATH for Root path but we have fix with our method (:
$this->base_directory = $this->currentRootPath();
$this->base_url = $this->baseUrl();
}
/**
* Set base URL
*
* @param string $base_url root url / base url or web root URI
*/
public function setBaseUrl($base_url)
{
$this->base_url = $base_url;
}
/**
* Set Base directory
*
* @param string $base_directory root directory of script execute / web root
*/
public function setBaseDirectory($base_directory)
{
$this->base_directory = $base_directory;
}
/**
* Add regex filename to ignored
*
* @param string $value regex
*/
public function addJsFileNameIgnoredRegex($value)
{
if (is_array($value)) {
foreach ($value as $key => $val) {
if (is_string($val) && trim($val)) {
$this->ignored_js_minify[$val] = true;
}
}
return;
}
if (is_string($value) && trim($value)) {
$this->ignored_js_minify[$value] = true;
}
}
/**
* Reset Regex Ignored Files to empty array
*/
public function clearJsFileIgnoredRegex()
{
$this->ignored_js_minify = [];
}
/**
* Remove regex files to ignored
*
* @param string $value regex as key
*/
public function removeJsFileNameIgnoredRegex($value)
{
if (is_string($value) && trim($value)) {
unset($this->ignored_js_minify[$value]);
}
}
/* -------------------------------------------------------------+
* PROCESSOR |
* -------------------------------------------------------------+
*/
/**
* Takes a string containing javascript / css and removes unneeded characters in
* order to shrink the code without altering it's functionality.
* By combining & minify of files certain as arrat
*
* @param array|string $files_to_read ful path or url to geti minified and combine it
* @param bool $use_first_comment show first comment to print into output
* @param bool $allowed_conditional_comment show conditional comment start with /*! on javascript will be ignored to replaced
* @return string
*/
public function combine(
$files_to_read = null,
$use_first_comment = false,
$allowed_conditional_comment = false
) {
if (is_array($files_to_read)) {
foreach ($files_to_read as $key => $value) {
unset($files_to_read[$key]);
if (is_string($value) && trim($value) && $tasked = $this->combine(
$value,
$use_first_comment,
$allowed_conditional_comment
)
) {
$files_to_read[$key] = $tasked;
// clear
unset($tasked);
}
}
if (!empty($files_to_read)) {
return implode("\n", $files_to_read);
}
}
if (is_string($files_to_read) && trim($files_to_read)) {
if (!($use_http = preg_match('/(ht|f)tps?:\/\//i', $files_to_read)) // if not contain url ftp|http(s)
&& (is_file($files_to_read) && is_readable($files_to_read))
|| true
) {
$ext = strtolower(pathinfo($files_to_read, PATHINFO_EXTENSION));
if (!in_array($ext, ['js', 'css'])) {
return null;
}
/**
* Check if use local
*/
if (!$use_http && function_exists('fopen')) {
$retVal = null;
if ($fp = @fopen($files_to_read, 'r+')) {
while (($buffer = fgets($fp, 4096)) !== false) {
$retVal .= $buffer;
}
fclose($fp);
}
} else {
if ($use_http) {
$ctx = stream_context_create(
[
'http'=> [
'timeout' => 10, // 10 seconds has very very long time!!
]
]
);
$retVal = @file_get_contents($files_to_read, false, $ctx);
} else {
$retVal = @file_get_contents($files_to_read, false);
}
}
if (is_string($retVal)) {
if ($ext == 'js') {
foreach ($this->ignored_js_minify as $key => $v) {
if (preg_match('#'.$key.'$#', $files_to_read)) {
return $retVal;
}
}
}
return $ext == 'js'
? $this->internalMinifyJS($retVal, $use_first_comment, $allowed_conditional_comment)
: $this->internalFixCSS($this->minifyCss($retVal, $use_first_comment, $allowed_conditional_comment), $files_to_read);
}
}
return '';
}
return null;
}
/* -------------------------------------------------------------+
* CSS MINIFIER |
* -------------------------------------------------------------+
*/
/**
* Minify css
*
* @param string $cssText css to minify
* @param boolean $use_first_comment put first comment of css for informational only
* @param string $url_replacer url assets directory placed
* @return string minified css
*/
public function minifyCss(
$cssText,
$use_first_comment = false,
$url_replacer = null
) {
if (!is_string($cssText)) {
return false;
}
$first_comment = '';
if ($use_first_comment) {
// get first comment
preg_match('%^\s*/\*(?:[^*]|[\r\n]|(?:\*+(?:[^*/]|[\r\n])))*\*+/%', $cssText, $match);
if (!empty($match[0])) {
$first_comment = "{$match[0]}\n";
}
}
// remove comments
$cssText = preg_replace('/(^\s*|\s*$|\/\*(?:(?!\*\/)[\s\S])*\*\/|[\r\n\t]+)/', '', $cssText);
// remove multi twice characters into 3 characters sequence eg : #ffddee to #fde
$cssText = preg_replace(
'/(#(?:([a-f]|[A-F]|[0-9]){1}(?:\\2)([a-f]|[A-F]|[0-9]){1}(?:\\3))([a-f]|[A-F]|[0-9]){1}(?:\\4))\b/',
'#$2$3$4',
$cssText
);
$regex = '(?six)
\s*+;\s*(})\s*
| \s*([*$~^|]?=|[{};,>~+-]|\s+!important\b)\s*
| ([[(:])\s+
| \s+([\]\)])
| \s+(:)\s+
(?!
(?>
[^{}"\']++
| \"(?:[^"\\\\]++|\\\\.)*\"
| \'(?:[^\'\\\\]++|\\\\.)*\'
)*
{
)
| ^\s+|\s+ \z
| (\s)\s+
| (\#((?:[a-f]|[A-F]|[0-9]){3}))(?:\\2)?\b # replace same value hex digit to 3 character eg : #ffffff to #fff
';
/**
* Fix css url('statmenturi/');
*/
if (is_string($url_replacer) && trim($url_replacer) && preg_match('/^(https?:)?\/\//', $url_replacer) === 0) {
$cssText = $this->internalFixCSS($cssText, $url_replacer);
}
return $first_comment.preg_replace("%{$regex}%", '$1$2$3$4$5$6$7', $cssText);
}
/**
* Fix css url path that enclosed with ../ to better uses
*
* @param string $text css text
* @param string $path url assets directory placed
* @return string sanitized css
*/
public function fixCSSUri($text, $path)
{
if (!$text || !is_string($text) || !trim($text) || !is_string($path)) {
return $text;
}
if (preg_match('/url\(.+?\)/i', $text)) {
$ci = $this;
$text = preg_replace_callback('/url\((.+?)\)/ixm', function ($c) use ($path, $ci) {
$detach = trim(trim($c[1]), '\'"');
if (!preg_match('/^(https?:)?\/\//i', $detach)) {
$detach = preg_replace('/(\/|\\\)+/', '/', $detach);
$isAll = strpos($path, '//') === 0;
$urlArray = parse_url($path);
if (strpos($detach, '../') !== false) {
$path = isset( $urlArray['path'] ) && trim( $urlArray['path'], '/' ) != ''
? trim( $urlArray['path'], '/' )
: null;
if ( $path ) {
$path = preg_replace( '/(\\\|\/)+/', '/', $path );
$pathArray = explode('/', $path );
$detachArray = explode('../', $detach);
foreach ($detachArray as $value) {
if (!$value) {
array_shift($detachArray);
array_pop($pathArray);
break;
}
break;
}
$detach = implode('/', $detachArray);
$urlArray['path'] = ltrim(implode('/', $pathArray), '/');
$path = isset($urlArray['scheme'])
? $urlArray['scheme'] .'://'
: ($isAll ? '//' : '');
$path .= isset($urlArray['host']) ? $urlArray['host'] : '';
$path .= isset($urlArray['port']) ? ':'.$urlArray['post'] : '';
$path .= $urlArray['path'] ? '/'.$urlArray['path'] : '';
$query = isset($urlArray['query']) ? "?{$urlArray['query']}" : '';
}
}
$c[0] = rtrim($path, '/').'/'.ltrim($detach, '/');
$c[0] = strpos($c[0], '\'') !== false ? json_encode("{$c[0]}") : "'{$c[0]}'";
if (!empty($query)) {
if (strpos($c[0], '?') !== false) {
$ex = explode('?', $query);
$y = array_shift($ex);
$im = implode('?', $ex);
$query = strpos($im, '#') === 0
? $query . $im
: $query .'&'.$im;
$c[0] = $y. $query;
} else {
$c[0] .= $query;
}
}
$c[0] = "url({$c[0]})";
}
return $c[0];
}, $text);
}
return $text;
}
/**
* Fix css url path that enclosed with ../ to better uses
* This method for internakl Use Only
*
* @access private
* @param string $text css text
* @param string $path path to css file / full path
* @return string sanitized css
*/
private function internalFixCSS($text, $path)
{
if (!$text || !is_string($text) || !trim($text)) {
return $text;
}
if (preg_match('/url\(.+?\)/i', $text)) {
$ci = $this;
$text = preg_replace_callback('/url\((.+?)\)/ixm', function ($c) use ($path, $ci) {
$detach = trim(trim($c[1]), '\'"');
if (!preg_match('/^(https?:)?\/\//i', $detach)) {
$base_url = $ci->base_url;
$detach_dir = realpath(dirname($path).'/'.dirname($detach));
$fcPath = preg_replace('/(\/|\\\)+/', '/', $this->base_directory);
$detach_dir = preg_replace('/(\/|\\\)+/', '/', $detach_dir);
$detach_dir = (substr($detach_dir, strlen($fcPath)));
$c[0] = preg_replace(
'/^https?:\/\//i',
'',
// base URL
// take from base URL of CI
trim(rtrim($base_url, '/').'/'.(trim($detach_dir, '/').'/'.basename($detach)))
);
$c[0] = strpos($c[0], '\'') !== false ? json_encode("//{$c[0]}") : "'//{$c[0]}'";
$c[0] = "url({$c[0]})";
}
return $c[0];
}, $text);
}
return $text;
}
/* -------------------------------------------------------------+
* JAVASCRIPT MINIFIER |
* -------------------------------------------------------------+
*/
/**
* Takes a string containing javascript and removes unneeded characters in
* order to shrink the code without altering it's functionality.
*
* @param string $js The raw javascript to be minified
* @param bool $use_first_comment show first comment to print into output
* @param bool $allowed_conditional_comment show conditional comment start with /*! on javascript will be ignored to replaced
* @return bool|string
*/
public function minifyJS(
$js,
$use_first_comment = false,
$allowed_conditional_comment = false
) {
if (!is_string($js) || !trim($js)) {
return false;
}
/**
* Clean the cached string
* this method use clean all
* @var null
*/
$this->cleanAll();
/**
* Getting first comment if available and allow to print out
* @var string
*/
$first_comment = '';
if ($use_first_comment && ! $allowed_conditional_comment
&& preg_match('%^\s*/\*(?:[^*]|[\r\n]|(?:\*+(?:[^*/]|[\r\n])))*\*+/%', $js, $match)
&& !empty($match[0])
) {
$first_comment = trim($match[0])."\n";
unset($match);
}
/**
* Getting Real comment from input JS
*/
if ($allowed_conditional_comment) {
// remove comments non conditional
$js = preg_replace('%\s*/\*(?![\!])(?:[^*]|[\r\n]|(?:\*+(?:[^*/]|[\r\n])))*\*+/%', '', $js);
preg_match_all('%\s*/\*\!(?:[^*]|[\r\n]|(?:\*+(?:[^*/]|[\r\n])))*\*/%', $js, $match_all);
} else {
// remove comments
$js = preg_replace('%\s*/\*(?:[^*]|[\r\n]|(?:\*+(?:[^*/]|[\r\n])))*\*+/%', '', $js);
}
$match_all = isset($match_all) ? $match_all : [];
/**
* Doing progress
*/
$js = $this->lock($js);
$this->minifyDirectToOutput($js);
$this->unlock($this->all_cached);
// clean again just use clean reset
$this->cleanCached();
if ($this->is_failed) {
return false;
}
// freed the memory
unset($js);
// remove comments if not allowed conditional comment
if (! $allowed_conditional_comment) {
$this->all_cached = preg_replace('/(?:(?:\/\*(?:[^*]|(?:\*+[^*\/]))*\*+\/)|(?:(?<!\:|\\\|\'|\")\/\/.*))/', '$1', $this->all_cached);
} else {
/**
* Replace with real comments from @match_all from !Getting Real comment from input JS!
*/
$this->all_cached = preg_replace_callback('%(\s*/\*\!(?:[^*]|[\r\n]|(?:\*+(?:[^*/]|[\r\n])))*\*/)%', function () use ($match_all) {
static $d = 0;
// by default use empty
$text = '';
if (!empty($match_all) && isset($match_all[0][$d])) {
$match_all[0][$d] = ltrim($match_all[0][$d]);
$ex = explode("\n", $match_all[0][$d]);
foreach ($ex as $key => $value) {
$val = ltrim($value);
if (substr($val, 0, 2) == '/*') {
$ex[$key] = "{$val}";
} elseif (substr($val, 0, 1) == '*') {
$ex[$key] = " {$val}";
} else {
$ex[$key] = " *{$val}";
}
}
$match_all[0][$d] = implode("\n", $ex);
$text = "\n{$match_all[0][$d]}";
$d++;
if (count($match_all[0]) <= $d) {
$d = 0;
}
}
return $text;
}, $this->all_cached);
// unset
unset($match_all);
}
/**
* Sanitized and and minify it!
*/
// remove newline on brackets
$this->all_cached = preg_replace(
"/\n?(\{|\})(?:(?:\n(?![ ]+))?|(\n\s*)?)(?!\/\*\!)/", # fix regex
'$1',
// remove multiple new line
str_replace(
"\n\n",
"\n",
$this->all_cached
)
);
// remove new line after semicolon
$this->all_cached = preg_replace(
'/;\n(?!\s*\/\*)/',
';',
// remove semicolon inside before brackets not in regex or quote(s)
preg_replace(
'/(?>(?!\'|\"|\/));\s*\}|\s*}\s*/',
'}',
$this->all_cached
)
);
// if not allowed conditional comments
// use str replace for fast method
if (! $allowed_conditional_comment) {
$this->all_cached = str_replace(
["-\n", "+\n", ":\n", "\n["],
['-','+',':','['],
$this->all_cached
);
} else {
// remove new line in -+;[ not in comment
$this->all_cached = preg_replace('/(?>(?!\/\*))([\-\+\:])\n/', '$1', $this->all_cached);
$this->all_cached = preg_replace('/(?>(?!\/\*))\n\[/', '[', $this->all_cached);
}
/**
* If position of ouput on first statements is it will be exec fix clean
* comments
*/
if (strpos(trim($this->all_cached), '/*!') === 0) {
$this->all_cached = preg_replace('/(\/\*\!(?:[^*]|(?:\*+[^*\/]))*\*+\/)\s*/', "$1\n", $this->all_cached);
}
/**
* Trim output result
*/
$this->all_cached = trim($this->all_cached);
return $first_comment.$this->all_cached;
}
/**
* Takes a string containing javascript and removes unneeded characters in
* order to shrink the code without altering it's functionality.
* this for internal Use Only
*
* @access private
* @param string $text The raw javascript to be minified
* @param bool $use_first_comment show first comment to print into output
* @param bool $allowed_conditional_comment show conditional comment start with /*! on javascript will be ignored to replaced
* @return bool|string
*/
private function internalMinifyJS(
$text,
$use_first_comment = false,
$allowed_conditional_comment = false
) {
if (!is_string($text)) {
return false;
}
// doing clean
$this->cleanAll();
$this->all_cached = $this->minifyJS(
$text,
$use_first_comment,
$allowed_conditional_comment
);
return $this->all_cached;
}
/**
* Processes a javascript string and outputs only the required characters,
* stripping out all unneeded characters.
*
* @param string $js The raw javascript to be minified
*/
protected function minifyDirectToOutput($js)
{
if ($this->is_failed) {
return;
}
// $this->options = array_merge($this->$defaultOptions, $options);
$this->index_input = str_replace("\r\n", "\n", $js);
// freed memory
unset($js);
// match it first for double slash
if (strpos($this->index_input, '//')) {
// remove comments double slash
// fixed for error literal
$this->index_input = preg_replace('/(?:(?>(?!\/\*))(?<!\:|\\\|\'|\")\/\/.+\n)/', '', $this->index_input);
}
$this->index_input = str_replace("\r", "\n", $this->index_input);
// We add a newline to the end of the script to make it easier to deal
// with comments at the bottom of the script- this prevents the unclosed
// comment error that can otherwise occur.
$this->index_input .= PHP_EOL;
// Populate "a" with a new line, "b" with the first character, before
// entering the loop
$this->index_a = "\n";
$this->index_b = $this->getReal();
$this->loop();
$this->cleanCached();
}
/**
* The primary action occurs here. This function loops through the index_input string,
* outputting anything that's relevant and discarding anything that is not.
*/
protected function loop()
{
// exec this
if ($this->is_failed) {
return null;
}
while ($this->index_a !== false && !is_null($this->index_a) && $this->index_a !== '') {
// exec this
if ($this->is_failed) {
return null;
}
switch ($this->index_a) {
// new lines
case "\n":
// if the next line is something that can't stand alone preserve the newline
if (strpos('(-+{[@', $this->index_b) !== false) {
$this->all_cached .= $this->index_a;
$this->saveString();
break;
}
// if B is a space we skip the rest of the switch block and go down to the
// string/regex check below, resetting $this->index_b with getReal
if ($this->index_b === ' ') {
break;
}
// otherwise we treat the newline like a space
case ' ':
if ($this->isAlphaNumeric($this->index_b)) {
$this->all_cached .= $this->index_a;
}
$this->saveString();
break;
default:
switch ($this->index_b) {
case "\n":
if (strpos('}])+-"\'', $this->index_a) !== false) {
$this->all_cached .= $this->index_a;
$this->saveString();
break;
} else {
if ($this->isAlphaNumeric($this->index_a)) {
$this->all_cached .= $this->index_a;
$this->saveString();
}
}
break;
case ' ':
if (!$this->isAlphaNumeric($this->index_a)) {
break;
}
default:
// check for some regex that breaks stuff
if ($this->index_a == '/' && ($this->index_b == '\'' || $this->index_b == '"')) {
$this->saveRegex();
continue;
}
$this->all_cached .= $this->index_a;
$this->saveString();
break;
}
}
// do reg check of doom
$this->index_b = $this->getReal();
if (($this->index_b == '/' && strpos('(,=:[!&|?', $this->index_a) !== false)) {
$this->saveRegex();
}
}
}
/**
* Resets attributes that do not need to be stored between requests so that
* the next request is ready to go. Another reason for this is to make sure
* the variables are cleared and are not taking up memory.
*/
protected function cleanCached()
{
$this->index_input = null;
$this->index_position = 0;
$this->index_a = $this->index_b = '';
$this->index_c = null;
$this->is_failed = false;
}
/**
* Clean all record,
* best to call on end of call result
* and you should call this to freed your memory ::[recommended]
*/
public function cleanAll()
{
$this->cleanCached();
$this->index_locks = [];
$this->all_cached = null;
$this->error_log = [];
}
/**
* This function gets the next "real" character. It is essentially a wrapper
* around the getChar function that skips comments. This has significant
* performance benefits as the skipping is done using native functions (ie,
* c code) rather than in script php.
*
*
* @return string Next 'real' character to be processed.
*/
protected function getReal()
{
return $this->getChar();
}
/**
* Returns the next string for processing based off of the current index_position.
*
* @return string
*/
protected function getChar()
{
// Check to see if we had anything in the look ahead buffer and use that.
if (isset($this->index_c)) {
$char = $this->index_c;
unset($this->index_c);
// Otherwise we start pulling from the index_input.
} else {
$char = substr($this->index_input, $this->index_position, 1);
// If the next character doesn't exist return false.
if (isset($char) && $char === false) {
return false;
}
// Otherwise increment the pointer and use this char.
$this->index_position++;
}
// Normalize all whitespace except for the newline character into a
// standard space.
if ($char !== "\n" && ord($char) < 32) {
return ' ';
}
return $char;
}
/**
* Pushes the index_position ahead to the next instance of the supplied string. If it
* is found the first character of the string is returned and the index_position is set
* to it's position.
*
* @param string $string
* @return string|false Returns the first character of the string or false.
*/
protected function getNext($string)
{
// prevent execute
if ($this->is_failed) {
return null;
}
// Find the next occurrence of "string" after the current position.
$pos = strpos($this->index_input, $string, $this->index_position);
// If it's not there return false.
if ($pos === false) {
return false;
}
// Adjust position of index_position to jump ahead to the asked for string
$this->index_position = $pos;
// Return the first character of that string.
return substr($this->index_input, $this->index_position, 1);
}
/**
* When a javascript string is detected this function crawls for the end of
* it and saves the whole string.
*/
protected function saveString()
{
if ($this->is_failed) {
return null;
}
$startPosition = $this->index_position;
// saveString is always called after a gets cleared, so we push b into
// that spot.
$this->index_a = $this->index_b;
// If this isn't a string we don't need to do anything.
if ($this->index_a != "'" && $this->index_a != '"') {
return;
}
// String type is the quote used, " or '
$stringType = $this->index_a;
// Echo out that starting quote
$this->all_cached .= $this->index_a;
// Loop until the string is done