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