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         
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         
170         
171          // cached version?? - how do we decide if it's expired?
172         // while scanning the directory is slow... - it's faster than serving every file...
173         
174         
175         //$path = $this->rootURL ."/Pman/$mod/";
176         
177         //print_R($ar);exit;
178         $missing_files  = false;
179         $files = array();
180         $arfiles = array();
181         $relfiles = array(); // array of files without the path part...
182         $maxtime = 0;
183         $mtime = 0;
184         foreach($ar as $fn) {
185             $relfiles[] = substr($fn, strlen($dir)+1);
186             $f = basename($fn);
187             // got the 'module file..'
188             
189             if (!file_exists($dir . '/'. $f)) {
190                 echo "<!-- missing {$relpath}{$f} -->\n";
191                 $files[] = $relpath  . $f . '?ts=0';
192                 $missing_files = true;
193                 continue;
194             }
195             
196             $mtime = filemtime($dir . '/'. $f);
197             $maxtime = max($mtime, $maxtime);
198             $arfiles[$fn] = $mtime;
199             $files[] = $relpath  . $f . '?ts='.$mtime;
200             
201             
202             
203         }
204         if ($missing_files) {
205             $this->assetArrayToHtml($files, 'css');
206             return;
207             
208         }
209         
210          
211         //print_r($relfiles);
212         
213         $ui = posix_getpwuid(posix_geteuid());
214        
215         
216         $compiledir = session_save_path() . '/' .
217                 $ui['name'] . '-' . $ff->project . '-'. $ff->version . '-csscompile';
218         
219         if (!file_exists($compiledir)) {
220             mkdir($compiledir,0700,true);
221         }
222         
223          
224         // no sorting???
225         //$lsort = create_function('$a,$b','return strlen($a) > strlen($b) ? 1 : -1;');
226         //usort($files, $lsort);
227         
228         
229         if (!empty($this->bootLoader->isDev) && !empty($_REQUEST['isDev'])) {
230             echo "<!-- CSS compile turned off (isDev on) -->\n";
231             $this->assetArrayToHtml($files,'css');
232             return;
233         }
234         
235         
236         $smod = str_replace('/','.',$path);
237         
238         $output = date('Y-m-d-H-i-s-', $maxtime). $smod .'-'.md5(serialize($arfiles)) .'.css';
239          
240          
241         
242         // where are we going to write all of this..
243         // This has to be done via a 
244         if (true || !file_exists($compiledir.'/'.$output) || !filesize($compiledir.'/'.$output)) {
245             
246             
247             
248             require_once 'HTML/CSS/Minify.php';
249             $x = new HTML_CSS_Minify(substr($relpath,0,-1), $dir, $relfiles);
250             
251             file_put_contents($compiledir.'/'.$output , $x->minify( $this->baseURL.'/Asset/css'));
252             clearstatcache();
253             if (!file_exists($compiledir.'/'.$output) ||
254                 !filesize($compiledir.'/'.$routput)) {
255                 echo "<!-- compile did not generate files : " . basename($compiledir) . "/{$output} -->\n";
256                 $this->assetArrayToHtml($files,'css');
257                 return;
258             } 
259             
260         } else {
261          //   echo "<!-- file already exists: {$basedir}/{$output} -->\n";
262         }
263         
264          $asset = $ff->project == 'Pman' ? '/Core/Asset/css/' : '/Asset/css/';
265         //$this->arrayToJsInclude(  $files);
266         $this->assetArrayToHtml(  array(
267             $this->baseURL.$asset. $output,
268           
269         ),'css');
270         
271     }
272     
273     
274     
275 }