Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * assetic Resoure Management loader
- *
- * @author Gary Constable <garyconstable80@gmail.com>
- * @package core/classes
- */
- use Assetic\Factory\AssetFactory;
- use Assetic\AssetWriter;
- //use Assetic\AssetManager;
- use Assetic\Factory\Worker\CacheBustingWorker;
- use Assetic\FilterManager;
- //use Assetic\Filter\UglifyJs2Filter;
- use Assetic\Factory\LazyAssetManager;
- class resource_loader {
- private $type;
- public $resource;
- public $name;
- public $files = array();
- public $filters = array();
- public $options = array();
- public $prefix = '';
- private $base_path = '/assets/';
- private $static_name = "/app/config/static_files.inc.php";
- function __construct( $files = array(), $filter = array(), $options = array(), $type = "", $static_name = '', $base_path = '', $prefix = '' ){
- $this->prefix = $prefix;
- if($static_name != ''){ $this->static_name = $static_name; }
- if($base_path != ''){ $this->base_path = $base_path; }
- $this->files = $files;
- $this->type = $type;
- $this->filters = $filter;
- $this->options = $options;
- if( $_ENV['config']['debug'] === false || $_ENV['config']['debug'] === 'false' ){
- //d( SERVER_PATH . $this->static_name);
- require SERVER_PATH . $this->static_name;
- switch($type){
- case 'css':
- $this->resource = $this->prefix . $this->base_path . 'css/' . $arr['css'];
- break;
- case 'js':
- $this->resource = $this->prefix . $this->base_path . 'js/' . $arr['js'];
- break;
- }
- }else{
- //d(WEB_ROOT . $this->static_name);
- switch($type){
- case 'css':
- $this->writeFile( 'css', $this->createCss() );
- break;
- case 'js':
- $this->writeFile( 'js', $this->createJs() );
- break;
- }
- }
- }
- public function createCss(){
- $filePath = WEB_ROOT . $this->base_path . 'css';
- $factory = new AssetFactory( $filePath );
- $am = new LazyAssetManager($factory);
- $fm = new FilterManager();
- if(in_array( 'cssmin', $this->filters )){
- $fm->set('cssmin', new \Assetic\Filter\CssMinFilter() );
- }
- if(in_array( 'yui', $this->filters )){
- $fm->set('yui', new \Assetic\Filter\Yui\CssCompressorFilter( BASE_PATH . '/vendor/bin/yuicompressor.jar') );
- }
- $factory->setAssetManager($am);
- $factory->setFilterManager($fm);
- $factory->setDebug(false);
- $factory->addWorker(new CacheBustingWorker($am));
- $css = $factory->createAsset( $this->files, $this->filters, $this->options);
- $am->set( 'css', $css);
- $writer = new AssetWriter( WEB_ROOT . $this->base_path . 'css' );
- $writer->writeManagerAssets( $am );
- $this->resource = $this->prefix . $this->base_path . 'css/' . $css->getTargetPath();
- $this->name = $css->getTargetPath();
- return $this->name;
- }
- public function createJs(){
- $filePath = WEB_ROOT . $this->base_path . 'js';
- $factory = new AssetFactory( $filePath );
- $am = new LazyAssetManager($factory);
- $fm = new FilterManager();
- if(in_array( 'jsmin', $this->filters )){
- $fm->set('jsmin', new \Assetic\Filter\JSMinFilter() );
- }
- if(in_array( 'jsyui', $this->filters )){
- $fm->set('jsyui', new \Assetic\Filter\Yui\JsCompressorFilter( BASE_PATH . '/vendor/bin/yuicompressor.jar') );
- }
- //need to install node.js for this to work!
- if(in_array( 'uglifyjs2', $this->filters )){
- $fm->set('uglifyjs2', new \Assetic\Filter\UglifyJs2Filter( BASE_PATH . '/vendor/joacub/uglify-js2/bin/uglifyjs') );
- }
- $factory->setAssetManager($am);
- $factory->setFilterManager($fm);
- $factory->setDebug(false);
- $factory->addWorker(new CacheBustingWorker($am));
- $js = $factory->createAsset( $this->files, $this->filters, $this->options);
- $am->set( 'js', $js);
- $writer = new AssetWriter( WEB_ROOT . $this->base_path . 'js' );
- $writer->writeManagerAssets( $am );
- $this->resource = $this->prefix . $this->base_path . 'js/' . $js->getTargetPath();
- $this->name = $js->getTargetPath();
- return $this->name;
- }
- public function writeFile( $type, $name){
- //d(SERVER_PATH . $this->static_name);
- if(!file_exists( SERVER_PATH . $this->static_name )){
- file_put_contents( SERVER_PATH . $this->static_name, '' );
- }
- require SERVER_PATH . $this->static_name;
- $tmp = isset($arr) ? $arr : array() ;
- $tmp = array_merge( $tmp, array($type => $name, 'now' => date("D M j G:i:s T Y") ));
- $str = "<?php ";
- $str .= '$arr = array(';
- foreach($tmp as $index => $value){
- $str .= '"'.$index.'" => "'. $value .'",' ;
- }
- $str .= ')';
- $str .= ";?>";
- $fp = fopen( SERVER_PATH . $this->static_name, 'w+');
- fwrite($fp, $str);
- fclose($fp);
- if($this->prefix === ''){
- $this->cleanUp($type, $name);
- }
- }
- /**
- * Remove old files compliled by assetic
- *
- * @param string $type
- * @param string $name - name of the file we have just genearted and do not want to remove
- */
- public function cleanUp( $type = '', $name = '' ){
- $path = pathinfo($name);
- $dir = WEB_ROOT . '/assets/' . $type . '/' . $path['dirname'] . '/';
- $files = scandir($dir);
- if(!empty($files)){
- $ignore = array('..', '.');
- $ignore[] = $path['basename'];
- foreach($files as $var){
- if(!in_array($var, $ignore)){
- if( file_exists($dir . $var) && !is_dir( $dir . $var) ){
- unlink($dir . $var);
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement