1 <?php
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 21 22
23 class MagratheaView{
24
25 private $javascript_files = array();
26 private $javascript_lastmodified_arr = array();
27 private $css_files = array();
28 private $css_lastmodified_arr = array();
29 private $site_path = "";
30 private $compressionMode = "default";
31 private $relativePath = false;
32 private $urlForAssets = "";
33
34 protected $compressed_path_js = "javascript/_compressed";
35 protected $compressed_path_css = "css/_compressed";
36
37 protected static $inst = null;
38
39 public static function Instance(){
40 if(!isset(self::$inst)){
41 self::$inst = new MagratheaView();
42 }
43 return self::$inst;
44 }
45 private function __construct(){
46 $this->site_path = MagratheaConfig::Instance()->GetConfigFromDefault("site_path");
47 }
48
49 50 51 52 53
54 public function SetCompressionMode($mode){
55 $this->compressionMode = strtolower($mode);
56 return $this;
57 }
58 59 60 61 62
63 public function IsRelativePath($it_is){
64 $this->relativePath = $it_is;
65 return $this;
66 }
67 68 69 70
71 public function ShouldCompressJavascript(){
72 return MagratheaConfig::Instance()->GetConfigFromDefault("compress_js");
73 }
74 75 76 77
78 public function ShouldCompressCss(){
79 return MagratheaConfig::Instance()->GetConfigFromDefault("compress_css");
80 }
81 82 83 84 85 86
87 public function SetUrlForAssets($url){
88 $this->urlForAssets = $url;
89 return $this;
90 }
91
92 93 94 95 96
97 public function SetCompressedPathJs($path){
98 $this->compressed_path_js = $path;
99 return $this;
100 }
101 102 103 104 105
106 public function SetCompressedPathCss($path){
107 $this->compressed_path_css = $path;
108 return $this;
109 }
110 111 112 113
114 public function GetCompressedPathJs(){
115 return $this->compressed_path_js;
116 }
117 118 119 120
121 public function GetCompressedPathCss(){
122 return $this->compressed_path_css;
123 }
124
125 126 127 128 129
130 public function IncludeJavascript($js_file){
131 array_push($this->javascript_files, $js_file);
132 $file_full = $this->site_path."/".$js_file;
133 if(file_exists($file_full)){
134 $lastm = filemtime($file_full);
135 } else {
136 throw new MagratheaViewException("File ".$file_full." could not be reached!");
137 }
138 array_push($this->javascript_lastmodified_arr, $lastm);
139 return $this;
140 }
141 142 143 144 145
146 public function IncludeCSS($css_file){
147 array_push($this->css_files, $css_file);
148 $file_full = $this->site_path."/".$css_file;
149 if(file_exists($file_full)){
150 $lastm = filemtime($file_full);
151 } else {
152 throw new MagratheaViewException("File ".$file_full." could not be reached!");
153 }
154 array_push($this->css_lastmodified_arr, $lastm);
155 return $this;
156 }
157
158 159 160 161
162 public function Javascripts(){
163 $this->Javascript(false);
164 }
165 166 167 168 169
170 public function Javascript($print=false, $compression=null){
171 $compression = ($compression==null ? $this->ShouldCompressJavascript() : $compression );
172 $array_files = array_unique($this->javascript_files);
173 $jsContent = "<!--JS FILES MANAGED BY MAGRATHEA [compression: ".$compression."] -->\n";
174 if($compression == "true"){
175 sort($this->javascript_lastmodified_arr);
176 $js_lmod = implode("_", $this->javascript_lastmodified_arr);
177 $js_lmod_hash = md5($js_lmod);
178 $compressedFileName = $this->compressed_path_js."/".$js_lmod_hash."_compressed.js";
179 if(!file_exists($compressedFileName)){
180 if (!$handle = @fopen($compressedFileName, 'w')) {
181 $jsContent .= "<!--error compressing javascript! could not create file-->";
182 $jsContent .= $this->Javascript(false, "false");
183 return $jsContent;
184 }
185 $jsCompressor = new MagratheaCompressor(MagratheaCompressor::COMPRESS_JS);
186 $jsCompressor->setCompressionMode($this->compressionMode);
187 foreach($array_files as $file){
188 $jsCompressor->add($file);
189 }
190 $jsCompressor->compress();
191 $compressed_js = $jsCompressor->GetCompressedContent();
192 if (!fwrite($handle, $compressed_js)) {
193 $jsContent .= "<!--error compressing javascript! could not write file-->";
194 $jsContent .= $this->Javascript(false, "false");
195 return $jsContent;
196 }
197 fclose($handle);
198 }
199 $jsContent .= "<script type='text/javascript' src='".$this->urlForAssets.($this->relativePath ? "" : "/").$compressedFileName."'></script>\n";
200 } else {
201 foreach($array_files as $file){
202 $jsContent .= "<script type='text/javascript' src='".$this->urlForAssets.($this->relativePath ? "" : "/").$file."'></script>\n";
203 }
204 }
205 if($print) echo $jsContent;
206 return $jsContent;
207 }
208
209 210 211 212 213 214
215 public function InlineJavascript(){
216 $html = "<script type=\"text/javascript\">";
217 $html .= "/* GENERATED by MAGRATHEA at ".now()." */";
218 $array_files = array_unique($this->javascript_files);
219 foreach ($array_files as $file) {
220 $html .= file_get_contents($file);
221 }
222 $html .= "</script>";
223 return $html;
224 }
225
226 227 228 229
230 public function CSSs(){
231 echo "<!--CSS FILES MANAGED BY MAGRATHEA-->\n";
232 echo $this->CSS();
233 }
234 235 236 237 238
239 public function CSS($compression=null){
240 $compression = ($compression==null ? $this->ShouldCompressCss() : $compression );
241 $array_files = array_unique($this->css_files);
242 $cssContent = "";
243 if($compression == "true"){
244 sort($this->css_lastmodified_arr);
245 $css_lmod = implode("_", $this->css_lastmodified_arr);
246 $css_lmod_hash = md5($css_lmod);
247 $compressedFileName = $this->compressed_path_css."/".$css_lmod_hash."_compressed.css";
248 if(!file_exists($compressedFileName)){
249 if (!$handle = @fopen($compressedFileName, 'w')) {
250 $cssContent .= "<!--error compressing css! could not create file-->\n";
251 $cssContent .= $this->CSS("false");
252 return $cssContent;
253 }
254 $cssCompressor = new MagratheaCompressor(@MagratheaCompressor::COMPRESS_CSS);
255 foreach($array_files as $file){
256 $cssCompressor->add($file);
257 }
258 $cssCompressor->compress();
259 $compressed_css = $cssCompressor->GetCompressedContent();
260 if (!fwrite($handle, $compressed_css)) {
261 $cssContent .= "<!--error compressing css! could not write file-->\n";
262 $cssContent .= $this->CSS("false");
263 return $cssContent;
264 }
265 fclose($handle);
266 }
267 $cssContent .= "<link href='".$this->urlForAssets.($this->relativePath ? "" : "/").$compressedFileName."' rel='stylesheet'>\n";
268 } else {
269 foreach($array_files as $file){
270 $cssContent .= "<link href='".$this->urlForAssets.($this->relativePath ? "" : "/").$file."' rel='stylesheet'>\n";
271 }
272 }
273 return $cssContent;
274 }
275
276 277 278 279 280 281
282 public function InlineCSS($compression=false){
283 $html = "<style>";
284 $html .= "/* GENERATED by MAGRATHEA at ".now()." */";
285 $array_files = array_unique($this->css_files);
286 foreach ($array_files as $file) {
287 $html .= file_get_contents($this->site_path."/".$file);
288 }
289 $html .= "</style>";
290 return $html;
291 }
292
293
294 }
295
296
297
298