Advertisement
Guest User

Untitled

a guest
May 27th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.70 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4. * assetic Resoure Management loader
  5. *
  6. * @author Gary Constable <garyconstable80@gmail.com>
  7. * @package core/classes
  8. */
  9.  
  10.  
  11. use Assetic\Factory\AssetFactory;
  12. use Assetic\AssetWriter;
  13. //use Assetic\AssetManager;
  14. use Assetic\Factory\Worker\CacheBustingWorker;
  15. use Assetic\FilterManager;
  16. //use Assetic\Filter\UglifyJs2Filter;
  17. use Assetic\Factory\LazyAssetManager;
  18.  
  19.  
  20.  
  21.  
  22. class resource_loader {
  23.  
  24.  
  25. private $type;
  26. public $resource;
  27. public $name;
  28.  
  29. public $files = array();
  30. public $filters = array();
  31. public $options = array();
  32. public $prefix = '';
  33.  
  34. private $base_path = '/assets/';
  35.  
  36. private $static_name = "/app/config/static_files.inc.php";
  37.  
  38.  
  39.  
  40. function __construct( $files = array(), $filter = array(), $options = array(), $type = "", $static_name = '', $base_path = '', $prefix = '' ){
  41.  
  42. $this->prefix = $prefix;
  43.  
  44. if($static_name != ''){ $this->static_name = $static_name; }
  45.  
  46. if($base_path != ''){ $this->base_path = $base_path; }
  47.  
  48. $this->files = $files;
  49.  
  50. $this->type = $type;
  51.  
  52. $this->filters = $filter;
  53.  
  54. $this->options = $options;
  55.  
  56.  
  57. if( $_ENV['config']['debug'] === false || $_ENV['config']['debug'] === 'false' ){
  58.  
  59. //d( SERVER_PATH . $this->static_name);
  60.  
  61. require SERVER_PATH . $this->static_name;
  62.  
  63. switch($type){
  64.  
  65. case 'css':
  66. $this->resource = $this->prefix . $this->base_path . 'css/' . $arr['css'];
  67. break;
  68.  
  69. case 'js':
  70. $this->resource = $this->prefix . $this->base_path . 'js/' . $arr['js'];
  71. break;
  72. }
  73.  
  74.  
  75. }else{
  76.  
  77. //d(WEB_ROOT . $this->static_name);
  78.  
  79. switch($type){
  80.  
  81. case 'css':
  82. $this->writeFile( 'css', $this->createCss() );
  83. break;
  84.  
  85. case 'js':
  86. $this->writeFile( 'js', $this->createJs() );
  87. break;
  88. }
  89.  
  90. }
  91.  
  92. }
  93.  
  94.  
  95. public function createCss(){
  96.  
  97.  
  98. $filePath = WEB_ROOT . $this->base_path . 'css';
  99.  
  100. $factory = new AssetFactory( $filePath );
  101. $am = new LazyAssetManager($factory);
  102. $fm = new FilterManager();
  103.  
  104. if(in_array( 'cssmin', $this->filters )){
  105. $fm->set('cssmin', new \Assetic\Filter\CssMinFilter() );
  106. }
  107.  
  108. if(in_array( 'yui', $this->filters )){
  109. $fm->set('yui', new \Assetic\Filter\Yui\CssCompressorFilter( BASE_PATH . '/vendor/bin/yuicompressor.jar') );
  110. }
  111.  
  112. $factory->setAssetManager($am);
  113. $factory->setFilterManager($fm);
  114. $factory->setDebug(false);
  115.  
  116. $factory->addWorker(new CacheBustingWorker($am));
  117.  
  118. $css = $factory->createAsset( $this->files, $this->filters, $this->options);
  119. $am->set( 'css', $css);
  120.  
  121. $writer = new AssetWriter( WEB_ROOT . $this->base_path . 'css' );
  122. $writer->writeManagerAssets( $am );
  123.  
  124. $this->resource = $this->prefix . $this->base_path . 'css/' . $css->getTargetPath();
  125.  
  126. $this->name = $css->getTargetPath();
  127.  
  128. return $this->name;
  129.  
  130. }
  131.  
  132.  
  133.  
  134. public function createJs(){
  135.  
  136. $filePath = WEB_ROOT . $this->base_path . 'js';
  137.  
  138. $factory = new AssetFactory( $filePath );
  139. $am = new LazyAssetManager($factory);
  140. $fm = new FilterManager();
  141.  
  142. if(in_array( 'jsmin', $this->filters )){
  143. $fm->set('jsmin', new \Assetic\Filter\JSMinFilter() );
  144. }
  145.  
  146. if(in_array( 'jsyui', $this->filters )){
  147. $fm->set('jsyui', new \Assetic\Filter\Yui\JsCompressorFilter( BASE_PATH . '/vendor/bin/yuicompressor.jar') );
  148. }
  149.  
  150. //need to install node.js for this to work!
  151. if(in_array( 'uglifyjs2', $this->filters )){
  152. $fm->set('uglifyjs2', new \Assetic\Filter\UglifyJs2Filter( BASE_PATH . '/vendor/joacub/uglify-js2/bin/uglifyjs') );
  153. }
  154.  
  155. $factory->setAssetManager($am);
  156. $factory->setFilterManager($fm);
  157. $factory->setDebug(false);
  158.  
  159. $factory->addWorker(new CacheBustingWorker($am));
  160.  
  161. $js = $factory->createAsset( $this->files, $this->filters, $this->options);
  162. $am->set( 'js', $js);
  163.  
  164. $writer = new AssetWriter( WEB_ROOT . $this->base_path . 'js' );
  165. $writer->writeManagerAssets( $am );
  166.  
  167. $this->resource = $this->prefix . $this->base_path . 'js/' . $js->getTargetPath();
  168.  
  169. $this->name = $js->getTargetPath();
  170.  
  171. return $this->name;
  172. }
  173.  
  174.  
  175.  
  176. public function writeFile( $type, $name){
  177.  
  178. //d(SERVER_PATH . $this->static_name);
  179.  
  180. if(!file_exists( SERVER_PATH . $this->static_name )){
  181. file_put_contents( SERVER_PATH . $this->static_name, '' );
  182. }
  183.  
  184. require SERVER_PATH . $this->static_name;
  185.  
  186. $tmp = isset($arr) ? $arr : array() ;
  187. $tmp = array_merge( $tmp, array($type => $name, 'now' => date("D M j G:i:s T Y") ));
  188.  
  189. $str = "<?php ";
  190. $str .= '$arr = array(';
  191.  
  192. foreach($tmp as $index => $value){
  193.  
  194. $str .= '"'.$index.'" => "'. $value .'",' ;
  195.  
  196. }
  197.  
  198. $str .= ')';
  199. $str .= ";?>";
  200.  
  201.  
  202. $fp = fopen( SERVER_PATH . $this->static_name, 'w+');
  203. fwrite($fp, $str);
  204. fclose($fp);
  205.  
  206. if($this->prefix === ''){
  207. $this->cleanUp($type, $name);
  208. }
  209. }
  210.  
  211.  
  212.  
  213. /**
  214. * Remove old files compliled by assetic
  215. *
  216. * @param string $type
  217. * @param string $name - name of the file we have just genearted and do not want to remove
  218. */
  219. public function cleanUp( $type = '', $name = '' ){
  220.  
  221. $path = pathinfo($name);
  222.  
  223. $dir = WEB_ROOT . '/assets/' . $type . '/' . $path['dirname'] . '/';
  224.  
  225. $files = scandir($dir);
  226.  
  227. if(!empty($files)){
  228.  
  229. $ignore = array('..', '.');
  230. $ignore[] = $path['basename'];
  231.  
  232. foreach($files as $var){
  233.  
  234. if(!in_array($var, $ignore)){
  235.  
  236. if( file_exists($dir . $var) && !is_dir( $dir . $var) ){
  237. unlink($dir . $var);
  238. }
  239. }
  240. }
  241. }
  242. }
  243. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement