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