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         
82         if (!empty($this->bootLoader->isDev) && !empty($_REQUEST['isDev'])) {
83             echo "<!-- Javascript compile turned off (isDev on) -->\n";
84             $this->assetArrayToHtml($files,'js');
85             return;
86         }
87         
88         
89         $smod = str_replace('/','.',$path);
90         
91         $output = date('Y-m-d-H-i-s-', $maxtime). $smod .'-'.md5(serialize($arfiles)) .'.js';
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         //$this->arrayToJsInclude(  $files);
114         $this->assetArrayToHtml(  array(
115             $this->baseURL.'/Asset/js/'. $output,
116           
117         ), 'js');
118         
119     }
120     
121     function assetArrayToHtml($ar, $type)
122     {
123         foreach($ar as $f) {
124             switch( $type) {
125                 case 'js':
126                     echo '<script type="text/javascript" src="'. $f. '"></script>'."\n";
127                     break;
128                 case 'css':
129                     echo '<link rel="stylesheet" href="'. $f. '"/>'."\n";
130                     break;
131        
132             }
133             
134         }
135     }
136     
137     
138     /**
139      * usage in template
140      * {outputCssDir(#{Hydra/templates/images/css/#,#Hydra.js",#.......#)}
141      */
142     
143     function outputCssDir($path)
144     {
145           
146         $relpath = $this->rootURL . '/' . $path .'/';
147         $ff = HTML_FlexyFramework::get();
148         $dir =   $this->rootDir.'/' . $path;
149         
150         $args = func_get_args();
151         $ar = array();
152         if (count($args) < 2) {
153             $ar = glob($dir . '/*.css');
154         } else {
155             array_shift($args);
156             foreach($args as $f) {
157                 if (strpos($f,'*') > -1) {
158  
159                     $ar = array_merge($ar ,  glob($dir . '/'. $f));
160                     continue;
161                 }
162                 // what if the fiel does not exist???
163                 $ar[] = $dir .'/'. $f;
164             }
165           
166         }
167         
168         
169          // cached version?? - how do we decide if it's expired?
170         // while scanning the directory is slow... - it's faster than serving every file...
171         
172         
173         //$path = $this->rootURL ."/Pman/$mod/";
174         
175         //print_R($ar);exit;
176         $missing_files  = false;
177         $files = array();
178         $arfiles = array();
179         $relfiles = array(); // array of files without the path part...
180         $maxtime = 0;
181         $mtime = 0;
182         foreach($ar as $fn) {
183             $relfiles[] = substr($fn, strlen($dir)+1);
184             $f = basename($fn);
185             // got the 'module file..'
186             
187             if (!file_exists($dir . '/'. $f)) {
188                 echo "<!-- missing {$relpath}{$f} -->\n";
189                 $files[] = $relpath  . $f . '?ts=0';
190                 $missing_files = true;
191                 continue;
192             }
193             
194             $mtime = filemtime($dir . '/'. $f);
195             $maxtime = max($mtime, $maxtime);
196             $arfiles[$fn] = $mtime;
197             $files[] = $relpath  . $f . '?ts='.$mtime;
198             
199             
200             
201         }
202         if ($missing_files) {
203             $this->assetArrayToHtml($files, 'css');
204             return;
205             
206         }
207         
208          
209         //print_r($relfiles);
210         
211         $ui = posix_getpwuid(posix_geteuid());
212        
213         
214         $compiledir = session_save_path() . '/' .
215                 $ui['name'] . '-' . $ff->project . '-'. $ff->version . '-csscompile';
216         
217         if (!file_exists($compiledir)) {
218             mkdir($compiledir,0700,true);
219         }
220         
221          
222         // no sorting???
223         //$lsort = create_function('$a,$b','return strlen($a) > strlen($b) ? 1 : -1;');
224         //usort($files, $lsort);
225         
226         
227         if (!empty($this->bootLoader->isDev) && !empty($_REQUEST['isDev'])) {
228             echo "<!-- CSS compile turned off (isDev on) -->\n";
229             $this->assetArrayToHtml($files,'css');
230             return;
231         }
232         
233         
234         $smod = str_replace('/','.',$path);
235         
236         $output = date('Y-m-d-H-i-s-', $maxtime). $smod .'-'.md5(serialize($arfiles)) .'.css';
237          
238          
239         
240         // where are we going to write all of this..
241         // This has to be done via a 
242         if (true || !file_exists($compiledir.'/'.$output) || !filesize($compiledir.'/'.$output)) {
243             
244             
245             
246             require_once 'HTML/CSS/Minify.php';
247             $x = new HTML_CSS_Minify(substr($relpath,0,-1), $dir, $relfiles);
248             
249             file_put_contents($compiledir.'/'.$output , $x->minify( $this->baseURL.'/Asset/css'));
250             clearstatcache();
251             if (!file_exists($compiledir.'/'.$output) ||
252                 !filesize($compiledir.'/'.$routput)) {
253                 echo "<!-- compile did not generate files : " . basename($compiledir) . "/{$output} -->\n";
254                 $this->assetArrayToHtml($files,'css');
255                 return;
256             } 
257             
258         } else {
259          //   echo "<!-- file already exists: {$basedir}/{$output} -->\n";
260         }
261         //$this->arrayToJsInclude(  $files);
262         $this->assetArrayToHtml(  array(
263             $this->baseURL.'/Asset/css/'. $output,
264           
265         ),'css');
266         
267     }
268     
269     
270     
271 }