fix #8131 - chinese translations
[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                 if (!preg_match('/\.js$/', $f)) {
36                     $f .= ".js";
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) || $fn == '.js' || !file_exists($dir . '/' . $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         require_once 'Pman/Core/Asset.php';
72         $compiledir = Pman_Core_Asset::getCompileDir('js', '', true);
73         
74          
75         
76         $lsort = function($a,$b) { return strlen($a) > strlen($b) ? 1 : -1; };
77         usort($files, $lsort);
78         
79         $ff = HTML_FlexyFramework::get();
80         
81         if (!empty($ff->Pman['isDev']) && !empty($_REQUEST['isDev'])) {
82             echo "<!-- Javascript compile turned off (isDev on) -->\n";
83             $this->assetArrayToHtml($files,'js');
84             return;
85         }
86         
87         
88         $smod = str_replace('/','.',$path);
89         
90         $output = date('Y-m-d-H-i-s-', $maxtime). $smod .'-'.md5(serialize($arfiles)) .'.js';
91          
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      * 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         require_once 'Pman/Core/Asset.php';
216         $compiledir = Pman_Core_Asset::getCompileDir('css', '', true);
217         
218          
219         if (!file_exists($compiledir)) {
220             mkdir($compiledir,0700,true);
221         }
222         
223          
224         
225         
226         // yes sort... if includes are used - they have to be in the first file...
227         $lsort = function($a,$b ) {
228                 return strlen($a) > strlen($b) ? 1 : -1;
229         };
230         usort($files, $lsort);
231         usort($relfiles,$lsort);
232        // print_R($relfiles);
233         
234         $ff = HTML_FlexyFramework::get();
235         
236         // isDev set
237         
238         if ((!empty($ff->Pman['isDev']) || $_SERVER['HTTP_HOST'] == 'localhost' )&& !empty($_REQUEST['isDev'])) {
239             echo "<!-- CSS compile turned off (isDev on) -->\n";
240             $this->assetArrayToHtml($files,'css');
241             return;
242         }
243         
244         
245         $smod = str_replace('/','.',$path);
246         
247         $output = date('Y-m-d-H-i-s-', $maxtime). $smod .'-'.md5(serialize(array($this->baseURL, $arfiles))) .'.css';
248          
249         $asset = $ff->project == 'Pman' ? '/Core/Asset/css/' : '/Asset/css/';
250         
251         // where are we going to write all of this..
252         // This has to be done via a 
253         if ( !file_exists($compiledir.'/'.$output) || !filesize($compiledir.'/'.$output)) {
254             
255             //print_r($relfiles);
256             
257             require_once 'HTML/CSS/Minify.php';
258             $x = new HTML_CSS_Minify(substr($relpath,0,-1), $dir, $relfiles);
259             
260             file_put_contents($compiledir.'/'.$output , $x->minify( $this->baseURL.$asset));
261             clearstatcache();
262             if (!file_exists($compiledir.'/'.$output) ||
263                 !filesize($compiledir.'/'.$output)) {
264                 echo "<!-- compile did not generate files : " . basename($compiledir) . "/{$output} -->\n";
265                 $this->assetArrayToHtml($files,'css');
266                 return;
267             } 
268             
269         } else {
270          //   echo "<!-- file already exists: {$basedir}/{$output} -->\n";
271         }
272         
273          
274         //$this->arrayToJsInclude(  $files);
275         $this->assetArrayToHtml(  array(
276             $this->baseURL.$asset. $output,
277           
278         ),'css');
279         
280     }
281     
282     
283     
284     function outputSCSS($smod)
285     {
286         // we cant output non-cached versions of this....
287         
288         // this doesnt really look like it would work!
289         $this->outputSCSSDir("{$this->rootDir}/Pman/{$smod}/scss/{$smod}.scss", $smod);
290         
291     }
292     /*
293      * Pman projects - expect
294      * /Pman/MyProject/scss/MyProject.less  <<
295      *           this should contain includes for the others?
296      *              @import "fonts.less";
297      *              ....
298      * Then all the files go here.
299      * /Pman/MyProject/scss/*.less
300      *
301      * For a Non Pman project
302      *  send:
303      *  /MyProject/scss/base.less << could be anything really...
304      *
305      * 
306      */
307      
308     
309     function outputSCSSDir($file, $smod= '')
310     {
311         
312          
313         $ff = HTML_FlexyFramework::get();
314         $asset = $ff->project == 'Pman' ? '/Core/Asset/css/' : '/Asset/css/';
315         
316         
317         if (!file_exists($file)) {
318             return;
319         }
320         
321         $ar = glob(dirname($file). '/*.scss');
322         $maxtime = filemtime($file);
323         foreach($ar as $fn) {
324             $maxtime= max($maxtime, filemtime($fn));
325         }
326         
327         
328         
329         //print_r($relfiles);
330       
331         require_once 'Pman/Core/Asset.php';
332         $compiledir = Pman_Core_Asset::getCompileDir('css',  '', true);
333         
334          
335         if (!file_exists($compiledir)) {
336             mkdir($compiledir,0700,true);
337         }
338         
339          
340         $output = date('Y-m-d-H-i-s-', $maxtime). $smod .'-less-'.md5(serialize(array($this->baseURL, $ar))) .'.css';
341          
342         
343         
344         // where are we going to write all of this..
345         // This has to be done via a
346          
347         
348         if ( !file_exists($compiledir.'/'.$output) || !filesize($compiledir.'/'.$output)) {
349             
350             
351             
352             require_once 'System.php';
353             static $sassc = false;
354             if ($sassc === false) {
355                 $sassc = System::which("sassc");
356             }
357             if (empty($sassc)) {
358                 die("INSTALL sassc");
359             }
360                  
361              
362             $ver = `$sassc --version`;
363             $bits = explode("\n", trim($ver));
364             foreach($bits as $b) {
365                  
366                 $lr = explode(":", $b);
367                 $vers[trim($lr[0])] = trim($lr[1]);
368             } 
369             
370             $sm = $vers['sass'] > 3.4 ? ' --sourcemap=auto ' : '--sourcemap';
371             $cmd = "{$sassc} --style=compressed  {$sm} -I ". dirname($file) . " -I {$this->rootDir}/roojs1/scss/bootstrap ". basename($file) . " {$compiledir}/{$output}";
372             //echo "$cmd\n";            echo `$cmd`;
373             `$cmd`;
374             
375              
376             clearstatcache();
377             if (!file_exists($compiledir.'/'.$output) ||
378                 !filesize($compiledir.'/'.$output)) {
379                 echo "<!-- compile did not generate files : $cmd -->\n";
380                 echo "<script type=\"text/javascript\">alert('Failed to compile Less Dir: ". basename($file). "');</script>\n";
381                 return;
382             } 
383             
384         } else {
385          //   echo "<!-- file already exists: {$basedir}/{$output} -->\n";
386         }
387         
388          
389         //$this->arrayToJsInclude(  $files);
390         $this->assetArrayToHtml(  array(
391             $this->baseURL.$asset. $output,
392           
393         ),'css');
394         
395     }
396     
397     
398     
399      
400     
401     
402 }