Axiom (v1.2.0)

A lightweight PHP framework
H:/Workspace/php-axiom/www/php-axiom/libraries/axiom/axLibrary.class.php
Go to the documentation of this file.
00001 <?php
00021 class axLibrary {
00022                 
00027                 const CACHE_FILE = 'library.cache.php';
00028                 
00033                 protected $_directories;
00034                 
00039                 protected $_classes;
00040                 
00045                 protected $_cache_dir;
00046                 
00056                 protected $_regenerate_flag = true;
00057                 
00065                 public function __construct ($cache_dir = false) {
00066                                 $this->_directories = array();
00067                                 $this->_classes     = array();
00068                                 $this->_cache_dir   = $cache_dir !== false ? realpath($cache_dir) : false;
00069                 }
00070                 
00090                 public function add ($name, array $options = array()) {
00091                                 $default = array(
00092                                                 'recursive' => false,
00093                                                 'extension' => null,
00094                                 );
00095                                 
00096                                 $options += $default;
00097                                 if (!is_dir($dir = $name) 
00098                                  && !is_dir($dir = AXIOM_LIB_PATH .'/'. $name) 
00099                                  && !is_dir($dir = AXIOM_APP_PATH .'/library/'. $name))
00100                                                 throw new RuntimeException("Cannot find library {$name}");
00101                                 
00102                                 if (!is_readable($dir))
00103                                                 throw new RuntimeException("{$dir} is not readable");
00104                                 
00105                                 $this->_directories[$dir] = $options;
00106                                 return $this;
00107                 }
00108                 
00118                 public function autoload ($classname) {
00119                                 if ($this->_load($classname))
00120                                                 return true;
00121                                                 
00122                                 if ($this->_regenerate_flag) {
00123                                                 $this->_regenerate_flag = false;
00124                                                 $this->_includeAll();
00125                                                 $this->_cache();
00126                                                 return $this->autoload($classname);
00127                                 }
00128                                 
00129                                 return false;
00130                 }
00131                 
00136                 public function register () {
00137                                 return spl_autoload_register(array($this, 'autoload'));
00138                 }
00139                 
00146                 protected function _includeAll () {
00147                                 foreach ($this->_directories as $dir => $opt) {
00148                                                 $directories = new AppendIterator();
00149                                                 
00150                                                 if (isset($opt['recursive']) && $opt['recursive'])
00151                                                                 $directories->append(new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir)));
00152                                                 else
00153                                                                 $directories->append(new DirectoryIterator($dir));
00154                                                 
00155                                                 $ext = isset($opt['extensions']) ? $opt['extensions'] : '.class.php';
00156                                                 $files = new axExtensionFilterIterator($directories, $ext);
00157                                                 
00158                                                 foreach ($files as $file) {
00159                                                     $basename  = $file->getBasename();
00160                                                                 $classname = substr($basename, 0, strpos($basename, '.'));
00161                                                                 $this->_classes[$classname] = $file->getRealPath();
00162                                                 }
00163                                 }
00164                 }
00165                 
00171                 protected function _load ($classname) {
00172                                 if (empty($this->_classes)) {
00173                                                 if ($this->_cache_dir && is_readable($c = $this->_cache_dir . '/' . self::CACHE_FILE)) {
00174                                                                 require $c;
00175                                                                 $this->_classes = $classes;
00176                                                 }
00177                                 }
00178                                 
00179                                 return isset($this->_classes[$classname]) && require_once $this->_classes[$classname];
00180                 }
00181                 
00189                 protected function _cache () {
00190                     if (!$this->_cache_dir)
00191                         return false;
00192                     
00193                                 $buffer = '<?php $classes=' . var_export($this->_classes, true) . '; ?>';
00194                                 return (boolean)file_put_contents($this->_cache_dir . '/' . self::CACHE_FILE, $buffer);
00195                 }
00196 }
 All Data Structures Files Functions Variables