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