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                 
36                 $ar[] = $dir .'/'. $f;
37             }
38           
39         }
40          // cached version?? - how do we decide if it's expired?
41         // while scanning the directory is slow... - it's faster than serving every file...
42         if (empty($ar)) {
43             echo "<!-- skipping $path - no files found -->\n";
44             return;
45         }
46         
47         //$path = $this->rootURL ."/Pman/$mod/";
48         
49         
50         
51         $files = array();
52         $arfiles = array();
53         $maxtime = 0;
54         $mtime = 0;
55         foreach($ar as $fn) {
56             $f = basename($fn);
57             // got the 'module file..'
58             $mtime = filemtime($dir . '/'. $f);
59             $maxtime = max($mtime, $maxtime);
60             $arfiles[$fn] = $mtime;
61             $files[] = $relpath  . $f . '?ts='.$mtime;
62         }
63         
64         ksort($arfiles); // just sort by name so it's consistant for serialize..
65         
66         $ui = posix_getpwuid(posix_geteuid());
67        
68         
69         $compiledir = session_save_path() . '/' .
70                 $ui['name'] . '-' . $ff->project . '-' . $ff->version . '-jscompile';
71         
72         if (!file_exists($compiledir)) {
73             mkdir($compiledir,0700,true);
74         }
75         
76          
77         
78         $lsort = create_function('$a,$b','return strlen($a) > strlen($b) ? 1 : -1;');
79         usort($files, $lsort);
80         
81         $ff = HTML_FlexyFramework::get();
82         
83         if (!empty($ff->Pman['isDev']) && !empty($_REQUEST['isDev'])) {
84             echo "<!-- Javascript compile turned off (isDev on) -->\n";
85             $this->assetArrayToHtml($files,'js');
86             return;
87         }
88         
89         
90         $smod = str_replace('/','.',$path);
91         
92         $output = date('Y-m-d-H-i-s-', $maxtime). $smod .'-'.md5(serialize($arfiles)) .'.js';
93          
94          
95         
96         // where are we going to write all of this..
97         // This has to be done via a 
98         if (!file_exists($compiledir.'/'.$output) || !filesize($compiledir.'/'.$output)) {
99             require_once 'Pman/Core/JsCompile.php';
100             $x = new Pman_Core_JsCompile();
101             
102             $x->pack($arfiles,$compiledir.'/'.$output, false);
103             clearstatcache();
104             if (!file_exists($compiledir.'/'.$output) ||
105                 !filesize($compiledir.'/'.$output)) {
106                 echo "<!-- compile did not generate files : ". basename($compiledir)  ."/{$output} -->\n";
107                 $this->assetArrayToHtml($files,'js');
108                 return;
109             } 
110             
111         } else {
112          //   echo "<!-- file already exists: {$basedir}/{$output} -->\n";
113         }
114         
115         $asset = $ff->project == 'Pman' ? '/Core/Asset/js/' : '/Asset/js/';
116         //$this->arrayToJsInclude(  $files);
117         $this->assetArrayToHtml(  array(
118             $this->baseURL.$asset. $output,
119           
120         ), 'js');
121         
122     }
123     
124     function assetArrayToHtml($ar, $type)
125     {
126         foreach($ar as $f) {
127             switch( $type) {
128                 case 'js':
129                     echo '<script type="text/javascript" src="'. $f. '"></script>'."\n";
130                     break;
131                 case 'css':
132                     echo '<link rel="stylesheet" href="'. $f. '"/>'."\n";
133                     break;
134        
135             }
136             
137         }
138     }
139     
140     
141     /**
142      * usage in template
143      * {outputCssDir(#{Hydra/templates/images/css/#,#Hydra.js",#.......#)}
144      */
145     
146     function outputCssDir($path)
147     {
148           
149         $relpath = $this->rootURL . '/' . $path .'/';
150         $ff = HTML_FlexyFramework::get();
151         $dir =   $this->rootDir.'/' . $path;
152         
153         $args = func_get_args();
154         $ar = array();
155         if (count($args) < 2) {
156             $ar = glob($dir . '/*.css');
157         } else {
158             array_shift($args);
159             foreach($args as $f) {
160                 if (strpos($f,'*') > -1) {
161  
162                     $ar = array_merge($ar ,  glob($dir . '/'. $f));
163                     continue;
164                 }
165                 // what if the fiel does not exist???
166                 $ar[] = $dir .'/'. $f;
167             }
168           
169         }
170         if (empty($ar)) {
171             echo "<!-- skipping $path - no files found -->\n";
172             return;
173         }
174         
175          // cached version?? - how do we decide if it's expired?
176         // while scanning the directory is slow... - it's faster than serving every file...
177         
178         
179         //$path = $this->rootURL ."/Pman/$mod/";
180         
181         //print_R($ar);exit;
182         $missing_files  = false;
183         $files = array();
184         $arfiles = array();
185         $relfiles = array(); // array of files without the path part...
186         $maxtime = 0;
187         $mtime = 0;
188         foreach($ar as $fn) {
189             $relfiles[] = substr($fn, strlen($dir)+1);
190             $f = basename($fn);
191             // got the 'module file..'
192             
193             if (!file_exists($dir . '/'. $f)) {
194                 echo "<!-- missing {$relpath}{$f} -->\n";
195                 $files[] = $relpath  . $f . '?ts=0';
196                 $missing_files = true;
197                 continue;
198             }
199             
200             $mtime = filemtime($dir . '/'. $f);
201             $maxtime = max($mtime, $maxtime);
202             $arfiles[$fn] = $mtime;
203             $files[] = $relpath  . $f . '?ts='.$mtime;
204             
205             
206             
207         }
208         if ($missing_files) {
209             $this->assetArrayToHtml($files, 'css');
210             return;
211             
212         }
213         
214          
215         //print_r($relfiles);
216         
217         $ui = posix_getpwuid(posix_geteuid());
218        
219         
220         $compiledir = session_save_path() . '/' .
221                 $ui['name'] . '-' . $ff->project . '-'. $ff->version . '-csscompile';
222         
223         if (!file_exists($compiledir)) {
224             mkdir($compiledir,0700,true);
225         }
226         
227          
228         
229         
230         // yes sort... if includes are used - they have to be in the first file...
231         $lsort = create_function('$a,$b','return strlen($a) > strlen($b) ? 1 : -1;');
232         usort($files, $lsort);
233         usort($relfiles,$lsort);
234        // print_R($relfiles);
235         
236         $ff = HTML_FlexyFramework::get();
237         
238         if (!empty($ff->Pman['isDev']) && !empty($_REQUEST['isDev'])) {
239             echo "<!-- CSS compile turned off (isDev on) -->\n";
240             $this->assetArrayToHtml($files,'css');
241             return;
242         }
243         
244         
245         $smod = str_replace('/','.',$path);
246         
247         $output = date('Y-m-d-H-i-s-', $maxtime). $smod .'-'.md5(serialize(array($this->baseURL, $arfiles))) .'.css';
248          
249         $asset = $ff->project == 'Pman' ? '/Core/Asset/css/' : '/Asset/css/';
250         
251         // where are we going to write all of this..
252         // This has to be done via a 
253         if ( !file_exists($compiledir.'/'.$output) || !filesize($compiledir.'/'.$output)) {
254             
255             //print_r($relfiles);
256             
257             require_once 'HTML/CSS/Minify.php';
258             $x = new HTML_CSS_Minify(substr($relpath,0,-1), $dir, $relfiles);
259             
260             file_put_contents($compiledir.'/'.$output , $x->minify( $this->baseURL.$asset));
261             clearstatcache();
262             if (!file_exists($compiledir.'/'.$output) ||
263                 !filesize($compiledir.'/'.$routput)) {
264                 echo "<!-- compile did not generate files : " . basename($compiledir) . "/{$output} -->\n";
265                 $this->assetArrayToHtml($files,'css');
266                 return;
267             } 
268             
269         } else {
270          //   echo "<!-- file already exists: {$basedir}/{$output} -->\n";
271         }
272         
273          
274         //$this->arrayToJsInclude(  $files);
275         $this->assetArrayToHtml(  array(
276             $this->baseURL.$asset. $output,
277           
278         ),'css');
279         
280     }
281     
282     
283     
284 }