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