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