Created
December 23, 2011 00:54
-
-
Save SET001/1512576 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Resource{ | |
private $_files = array(); | |
public $type; // either css or js | |
public $composed_file; // name of composed file | |
private static $_instances = array(); | |
public function get(){ | |
return $this->_files; | |
} | |
protected function __construct($type = 'js'){ | |
$this->type = in_array($type, array('css', 'js')) ? $type : 'js'; | |
} | |
public static function instance($type = 'js'){ | |
if (!isset(self::$_instances[$type])) | |
self::$_instances[$type] = new Resource($type); | |
return self::$_instances[$type]; | |
} | |
public function add($file){ | |
if (is_array($file)){ | |
$this->_files = array_merge(array_diff($file, $this->_files), $this->_files); | |
} | |
elseif (!in_array($file, $this->get())){ | |
$this->_files[] = $file; | |
} | |
$a = $this->get(); | |
sort($a); | |
$hash = md5(implode(', ', $a)); | |
$this->composed_file = "resources/$hash.{$this->type}"; | |
} | |
public function render(){ | |
if (Kohana::$environment == Kohana::PRODUCTION) | |
return View::factory("system/{$this->type}")->set('source', $this->get_composed()); | |
elseif (Kohana::$environment == Kohana::DEVELOPMENT){ | |
$res = array(); | |
foreach($this->get() as $resource){ | |
$res[] = View::factory("system/{$this->type}")->set('source', $this->type . '/' . $resource . '.' . $this->type); | |
} | |
return implode('', $res); | |
} | |
} | |
public function get_composed(){ | |
$files = $this->get(); | |
if (!file_exists($this->composed_file)){ | |
$this->compose(); | |
} | |
else{ | |
$this->check(); | |
} | |
return $this->composed_file; | |
} | |
protected function check(){ | |
$time = filemtime($this->composed_file); | |
$files_to_update = array(); | |
foreach($this->get() as $file){ | |
$file_name = "{$this->type}/$file.{$this->type}"; | |
if (filemtime($file_name) > $time){ | |
$files_to_update[] = $file; | |
}; | |
} | |
if ($files_to_update) | |
$this->compose($files_to_update); | |
} | |
protected function compose(array $files_to_update = array()){ | |
if (file_exists($this->composed_file)) unlink(realpath($this->composed_file)); | |
foreach($this->get() as $file){ | |
$resourse_file = 'resources/' . strtolower(str_replace('/', '__', $file) . '.' . $this->type); | |
if (!file_exists($resourse_file) OR in_array($file, $files_to_update)){ | |
$command = "yui-compressor --type {$this->type} -o $resourse_file {$this->type}/$file.{$this->type}"; | |
echo shell_exec($command); | |
} | |
$text = file_get_contents($resourse_file); | |
file_put_contents($this->composed_file, $text, FILE_APPEND); | |
//shell_exec("yui-compressor --type {$this->type} -o {$this->composed_file} {$this->composed_file}"); | |
} | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment