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