Changed AssetTrait.php
[Pman.Core] / AssetTrait.php
1 <?php
2 // a little experimental... we are going to use the same name as the class. for these..
3
4 trait Pman_Core_AssetTrait {
5     
6     
7     
8      /**
9      * usage in template
10      * {outputJavascriptDir(#Hydra#,#Hydra.js",#.......#)}
11      *
12      * call_user
13      * 
14      */
15     
16     function outputJavascriptDir($path)
17     {
18         
19         $relpath = $this->rootURL . '/' . $path .'/';
20         $ff = HTML_FlexyFramework::get();
21         $dir =   $this->rootDir.'/' . $path;
22         
23         $args = func_get_args();
24         $ar = array();
25         if (count($args) < 2) {
26             $ar = glob($dir . '/*.js');
27         } else {
28             array_shift($args);
29             foreach($args as $f) {
30                 if (strpos($f,'*') > -1) {
31  
32                     $ar = array_merge($ar ,  glob($dir . '/'. $f));
33                     continue;
34                 }
35                 if (!preg_match('/\.js$/', $f)) {
36                     $f .= ".js";
37                 }
38                 $ar[] = $dir .'/'. $f;
39             }
40           
41         }
42          // cached version?? - how do we decide if it's expired?
43         // while scanning the directory is slow... - it's faster than serving every file...
44         if (empty($ar)) {
45             echo "<!-- skipping $path - no files found -->\n";
46             return;
47         }
48         
49         //$path = $this->rootURL ."/Pman/$mod/";
50         
51         
52         
53         $files = array();
54         $arfiles = array();
55         $maxtime = 0;
56         $mtime = 0;
57         foreach($ar as $fn) {
58             $f = basename($fn);
59             if (!preg_match('/\.js$/', $f) || $fn == '.js' || !file_exists($dir . '/' . $f)) { // only javascript files... (so XXX.Dialog.YYY*  works..)
60                 continue;
61             }
62             // got the 'module file..'
63             $mtime = filemtime($dir . '/'. $f);
64             $maxtime = max($mtime, $maxtime);
65             $arfiles[$fn] = $mtime;
66             $files[] = $relpath  . $f . '?ts='.$mtime;
67         }
68         
69         ksort($arfiles); // just sort by name so it's consistant for serialize..
70         
71         require_once 'Pman/Core/Asset.php';
72         $compiledir = Pman_Core_Asset::getCompileDir('js', '', true);
73         
74          
75         
76         $lsort = function($a,$b) { return strlen($a) > strlen($b) ? 1 : -1; };
77         usort($files, $lsort);
78         
79         $ff = HTML_FlexyFramework::get();
80         
81         if (!empty($ff->Pman['isDev']) && !empty($_REQUEST['isDev'])) {
82             echo "<!-- Javascript compile turned off (isDev on) -->\n";
83             $this->assetArrayToHtml($files,'js');
84             return;
85         }
86         
87         
88         $smod = str_replace('/','.',$path);
89         
90         $output = date('Y-m-d-H-i-s-', $maxtime). $smod .'-'.md5(serialize($arfiles)) .'.js';
91          
92         
93         
94         
95         // where are we going to write all of this..
96         // This has to be done via a 
97         if (!file_exists($compiledir.'/'.$output) || !filesize($compiledir.'/'.$output)) {
98             require_once 'Pman/Core/JsCompile.php';
99             $x = new Pman_Core_JsCompile();
100             
101             $x->pack($arfiles,$compiledir.'/'.$output, false);
102             clearstatcache();
103             if (!file_exists($compiledir.'/'.$output) ||
104                 !filesize($compiledir.'/'.$output)) {
105                 echo "<!-- compile did not generate files : ". basename($compiledir)  ."/{$output} -->\n";
106                 $this->assetArrayToHtml($files,'js');
107                 return;
108             } 
109             
110         } else {
111          //   echo "<!-- file already exists: {$basedir}/{$output} -->\n";
112         }
113         
114         $asset = $ff->project == 'Pman' ? '/Core/Asset/js/' : '/Asset/js/';
115         //$this->arrayToJsInclude(  $files);
116         $this->assetArrayToHtml(  array(
117             $this->baseURL.$asset. $output,
118           
119         ), 'js');
120         
121     }
122     
123     function assetArrayToHtml($ar, $type)
124     {
125         foreach($ar as $f) {
126             switch( $type) {
127                 case 'js':
128                     echo '<script type="text/javascript" src="'. $f. '"></script>'."\n";
129                     break;
130                 case 'css':
131                     echo '<link rel="stylesheet" href="'. $f. '"/>'."\n";
132                     break;
133        
134             }
135             
136         }
137     }
138     
139     
140     /**
141      * usage in template
142      * {outputCSSDir(#{Hydra/templates/images/css/#,#Hydra.js",#.......#)}
143      */
144     
145     function outputCSSDir($path)
146     {
147           
148         $relpath = $this->rootURL . '/' . $path .'/';
149         $ff = HTML_FlexyFramework::get();
150         $dir =   $this->rootDir.'/' . $path;
151         
152         $args = func_get_args();
153         $ar = array();
154         if (count($args) < 2) {
155             $ar = glob($dir . '/*.css');
156         } else {
157             array_shift($args);
158             foreach($args as $f) {
159                 if (strpos($f,'*') > -1) {
160  
161                     $ar = array_merge($ar ,  glob($dir . '/'. $f));
162                     continue;
163                 }
164                 // what if the fiel does not exist???
165                 $ar[] = $dir .'/'. $f;
166             }
167           
168         }
169         if (empty($ar)) {
170             echo "<!-- skipping $path - no files found -->\n";
171             return;
172         }
173         
174          // cached version?? - how do we decide if it's expired?
175         // while scanning the directory is slow... - it's faster than serving every file...
176         
177         
178         //$path = $this->rootURL ."/Pman/$mod/";
179         
180         //print_R($ar);exit;
181         $missing_files  = false;
182         $files = array();
183         $arfiles = array();
184         $relfiles = array(); // array of files without the path part...
185         $maxtime = 0;
186         $mtime = 0;
187         foreach($ar as $fn) {
188             $relfiles[] = substr($fn, strlen($dir)+1);
189             $f = basename($fn);
190             // got the 'module file..'
191             
192             if (!file_exists($dir . '/'. $f)) {
193                 echo "<!-- missing {$relpath}{$f} -->\n";
194                 $files[] = $relpath  . $f . '?ts=0';
195                 $missing_files = true;
196                 continue;
197             }
198             
199             $mtime = filemtime($dir . '/'. $f);
200             $maxtime = max($mtime, $maxtime);
201             $arfiles[$fn] = $mtime;
202             $files[] = $relpath  . $f . '?ts='.$mtime;
203             
204             
205             
206         }
207         if ($missing_files) {
208             $this->assetArrayToHtml($files, 'css');
209             return;
210             
211         }
212         
213          
214         //print_r($relfiles);
215       
216         require_once 'Pman/Core/Asset.php';
217         $compiledir = Pman_Core_Asset::getCompileDir('css', '', true);
218         
219          
220         if (!file_exists($compiledir)) {
221             mkdir($compiledir,0700,true);
222         }
223         
224          
225         
226         
227         // yes sort... if includes are used - they have to be in the first file...
228         $lsort = function($a,$b ) {
229                 return strlen($a) > strlen($b) ? 1 : -1;
230         };
231         usort($files, $lsort);
232         usort($relfiles,$lsort);
233        // print_R($relfiles);
234         
235         $ff = HTML_FlexyFramework::get();
236         
237         // isDev set
238         
239         if ((!empty($ff->Pman['isDev']) || $_SERVER['HTTP_HOST'] == 'localhost' )&& !empty($_REQUEST['isDev'])) {
240             echo "<!-- CSS compile turned off (isDev on) -->\n";
241             $this->assetArrayToHtml($files,'css');
242             return;
243         }
244         
245         
246         $smod = str_replace('/','.',$path);
247         
248         $output = date('Y-m-d-H-i-s-', $maxtime). $smod .'-'.md5(serialize(array($this->baseURL, $arfiles))) .'.css';
249          
250         $asset = $ff->project == 'Pman' ? '/Core/Asset/css/' : '/Asset/css/';
251         
252         // where are we going to write all of this..
253         // This has to be done via a 
254         if ( !file_exists($compiledir.'/'.$output) || !filesize($compiledir.'/'.$output)) {
255             
256             //print_r($relfiles);
257             
258             require_once 'HTML/CSS/Minify.php';
259             $x = new HTML_CSS_Minify(substr($relpath,0,-1), $dir, $relfiles);
260             
261             file_put_contents($compiledir.'/'.$output , $x->minify( $this->baseURL.$asset));
262             clearstatcache();
263             if (!file_exists($compiledir.'/'.$output) ||
264                 !filesize($compiledir.'/'.$routput)) {
265                 echo "<!-- compile did not generate files : " . basename($compiledir) . "/{$output} -->\n";
266                 $this->assetArrayToHtml($files,'css');
267                 return;
268             } 
269             
270         } else {
271          //   echo "<!-- file already exists: {$basedir}/{$output} -->\n";
272         }
273         
274          
275         //$this->arrayToJsInclude(  $files);
276         $this->assetArrayToHtml(  array(
277             $this->baseURL.$asset. $output,
278           
279         ),'css');
280         
281     }
282     
283     
284     
285     function outputSCSS($path)
286     {
287         // we cant output non-cached versions of this.... 
288         $ff = HTML_FlexyFramework::get();
289         $dir =   "{$this->rootDir}/$path/scss/";
290         
291         
292         if (empty($ar)) {
293             echo "<!-- skipping $path - no files found -->\n";
294             return;
295         }
296         
297          // cached version?? - how do we decide if it's expired?
298         // while scanning the directory is slow... - it's faster than serving every file...
299         
300         
301         //$path = $this->rootURL ."/Pman/$mod/";
302         
303         //print_R($ar);exit;
304         $missing_files  = false;
305         $files = array();
306         $arfiles = array();
307         $relfiles = array(); // array of files without the path part...
308         $maxtime = 0;
309         $mtime = 0;
310         foreach($ar as $fn) {
311             $relfiles[] = substr($fn, strlen($dir)+1);
312             $f = basename($fn);
313             // got the 'module file..'
314             
315             if (!file_exists($dir . '/'. $f)) {
316                 echo "<!-- missing {$relpath}{$f} -->\n";
317                 $files[] = $relpath  . $f . '?ts=0';
318                 $missing_files = true;
319                 continue;
320             }
321             
322             $mtime = filemtime($dir . '/'. $f);
323             $maxtime = max($mtime, $maxtime);
324             $arfiles[$fn] = $mtime;
325             $files[] = $relpath  . $f . '?ts='.$mtime;
326             
327             
328             
329         }
330         if ($missing_files) {
331             $this->assetArrayToHtml($files, 'css');
332             return;
333             
334         }
335         
336          
337         //print_r($relfiles);
338       
339         require_once 'Pman/Core/Asset.php';
340         $compiledir = Pman_Core_Asset::getCompileDir('css', '', true);
341         
342          
343         if (!file_exists($compiledir)) {
344             mkdir($compiledir,0700,true);
345         }
346         
347          
348         
349         
350         // yes sort... if includes are used - they have to be in the first file...
351         $lsort = function($a,$b ) {
352                 return strlen($a) > strlen($b) ? 1 : -1;
353         };
354         usort($files, $lsort);
355         usort($relfiles,$lsort);
356        // print_R($relfiles);
357         
358         $ff = HTML_FlexyFramework::get();
359         
360         // isDev set
361         
362         if ((!empty($ff->Pman['isDev']) || $_SERVER['HTTP_HOST'] == 'localhost' )&& !empty($_REQUEST['isDev'])) {
363             echo "<!-- CSS compile turned off (isDev on) -->\n";
364             $this->assetArrayToHtml($files,'css');
365             return;
366         }
367         
368         
369         $smod = str_replace('/','.',$path);
370         
371         $output = date('Y-m-d-H-i-s-', $maxtime). $smod .'-'.md5(serialize(array($this->baseURL, $arfiles))) .'.css';
372          
373         $asset = $ff->project == 'Pman' ? '/Core/Asset/css/' : '/Asset/css/';
374         
375         // where are we going to write all of this..
376         // This has to be done via a 
377         if ( !file_exists($compiledir.'/'.$output) || !filesize($compiledir.'/'.$output)) {
378             
379             //print_r($relfiles);
380             
381             require_once 'HTML/CSS/Minify.php';
382             $x = new HTML_CSS_Minify(substr($relpath,0,-1), $dir, $relfiles);
383             
384             file_put_contents($compiledir.'/'.$output , $x->minify( $this->baseURL.$asset));
385             clearstatcache();
386             if (!file_exists($compiledir.'/'.$output) ||
387                 !filesize($compiledir.'/'.$routput)) {
388                 echo "<!-- compile did not generate files : " . basename($compiledir) . "/{$output} -->\n";
389                 $this->assetArrayToHtml($files,'css');
390                 return;
391             } 
392             
393         } else {
394          //   echo "<!-- file already exists: {$basedir}/{$output} -->\n";
395         }
396         
397          
398         //$this->arrayToJsInclude(  $files);
399         $this->assetArrayToHtml(  array(
400             $this->baseURL.$asset. $output,
401           
402         ),'css');
403         
404     }
405     
406     
407 }