1 <?php
2
3 include(__DIR__."/libs/phpclosure.php");
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 23 24
25 class MagratheaCompressor {
26
27 const COMPRESS_JS = 1;
28 const COMPRESS_CSS = 2;
29
30 private $files = array();
31 private $content = "";
32 private $type = COMPRESS_JS;
33 private $compressionMode = "advanced";
34
35 function MagratheaCompressor($compress_type=null) {
36 if(!is_null($compress_type)){
37 $this->type = $compress_type;
38 }
39 }
40
41 function setCompressionMode($mode){
42 $this->compressionMode = strtolower($mode);
43 }
44
45 46 47 48
49 function add($file) {
50 $this->files[] = $file;
51 return $this;
52 }
53
54 function compress(){
55 if( $this->type == self::COMPRESS_CSS ){
56 $this->compressCSS();
57 } else if ( $this->type == self::COMPRESS_JS ) {
58 $this->compressJs();
59 }
60 }
61
62 function compressCSS(){
63 $allContent = "";
64 foreach ($this->files as $f) {
65 $allContent .= file_get_contents($f);
66 }
67 Debug("compressing CSS...");
68 $this->content =
69 str_replace('; ',';',
70 str_replace(' }','}',
71 str_replace('{ ','{',
72 str_replace(
73 array("\r\n","\r","\n","\t",' ',' ',' '),"",
74 preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!','',
75 $allContent)
76 )
77 )
78 )
79 );
80 }
81
82 function compressJs($advancedMode=false){
83 $phpclosure = new PhpClosure();
84 foreach($this->files as $f){
85 $phpclosure->add($f);
86 }
87 if($this->compressionMode == "advanced"){
88 $phpclosure->advancedMode()->useClosureLibrary();
89 }
90 if( MagratheaDebugger::Instance()->GetType() == MagratheaDebugger::NONE ){
91 $phpclosure->hideDebugInfo();
92 }
93 Debug("compressing Javascript...");
94 $this->content = $phpclosure->_compile();
95
96 $consoleRegex = "/({|}|\)|;|\s)console.(log|debug|info|warn|error|assert|dir|dirxml|trace|group|groupEnd|time|timeEnd|profile|profileEnd|count)\\(.*?\\);/i";
97 $this->content = preg_replace($consoleRegex, "$1", $this->content);
98 }
99
100 function GetCompressedContent(){
101 return $this->content;
102 }
103 }
104
105 ?>