Axiom (v1.2.0)

A lightweight PHP framework
H:/Workspace/php-axiom/www/php-axiom/libraries/axiom/axModuleManager.class.php
Go to the documentation of this file.
00001 <?php
00017 class axModuleManager {
00018     
00023     const CACHE_FILE = "module.cache.php";
00024     
00029     protected $_path;
00030     
00035     protected $_axiomVersion;
00036     
00041     protected $_options;
00042     
00047     protected $_modules;
00048     
00060     public function __construct ($path, $axiom_version, array $options = array()) {
00061         $default = array(
00062             'check_dependencies' => true,
00063             'cache_dir' => false,
00064         );
00065         
00066         if (!$this->_path = realpath($path)) {
00067             throw new axMissingFileException($path);
00068         }
00069         
00070         $this->_axiomVersion = $axiom_version;
00071         $this->_options      = $options + $default;
00072         $this->_modules      = array();
00073     }
00074     
00079     public function getModules () {
00080         if (empty($this->_modules)) {
00081             if ($this->_options['cache_dir'] && is_readable($c = $this->_options['cache_dir'] . '/' . self::CACHE_FILE)) {
00082                 require $c;
00083                 $this->_modules = $modules;
00084             }
00085             else {
00086                 $directories = new DirectoryIterator($this->_path);
00087                 $iterator    = new axDirectoryFilterIterator($directories, array('.', '..', '.svn', '.git'));
00088                 foreach ($iterator as $item) {
00089                     $this->getInformations((string)$item);
00090                 }
00091                 $this->_cache();
00092             }
00093         }
00094         return $this->_modules;
00095     }
00096     
00102     public function exists ($module) {
00103         return array_key_exists($module, $this->getModules()) && $this->_modules[$module];
00104     }
00105     
00114     public function getInformations ($module) {
00115         if (!empty($this->_modules[$module]))
00116             return $this->_modules[$module];
00117         
00118         if (!is_file($p = $this->_path . "/$module/module.ini"))
00119             return false;
00120         
00121         if (($meta = parse_ini_file($p, false)) === false)
00122             return false;
00123             
00124         $meta['path'] = $this->_path . "/$module";
00125         return $this->_modules[$module] = $meta;
00126     }
00127     
00133     public function load ($module) {
00134         if (!$this->exists($module))
00135             return false;
00136             
00137         if (!$meta = $this->getInformations($module))
00138             throw new RuntimeException("Module {$module} informations are not avaialble, check that module.ini isn't missing", 2050);
00139         
00140         if ($this->_options['check_dependencies'] && !$this->_checkDependencies($module))
00141             throw new RuntimeException("Module {$module} dependencies check failed");
00142 
00143         if (!$this->_loadDependencies($module))
00144             throw new RuntimeException("Cannot load dependency module for {$module}");
00145         
00146         return (boolean)require_once $meta['path'] . "/config/bootstrap.php";
00147     }
00148     
00154     public function checkUpdates ($module) {
00155         // TODO axModuleManager::checkUpdates
00156     }
00157     
00164     protected function _getDependencies ($module) {
00165         if (!isset($this->_modules[$module]) || !$this->_modules[$module])
00166             throw new RuntimeException("Unknown module {$module}");
00167             
00168         return isset($this->_modules[$module]['dependencies']) ? 
00169             $this->_modules[$module]['dependencies'] : array();
00170     }
00171     
00178     protected function _checkDependencies ($module) {
00179         foreach ($this->_getDependencies($module) as $dep) {
00180             list($dep_module_name, $dep_module_version) = explode('-', $dep);
00181             
00182             if ($dep_module_name == 'axiom') {
00183                 if (!self::_compareVersions($this->_axiomVersion, $dep_module_version))
00184                     return false;
00185                 continue;
00186             }
00187             if (!$this->exists($dep_module_name))
00188                 return false;
00189             
00190             if (!$dep_meta = $this->getInformations($dep_module_name))
00191                 throw new RuntimeException("Cannot load dependencie module information");
00192                 
00193             if (!self::_compareVersions($dep_meta['version'], $dep_module_version))
00194                 return false;
00195         }
00196         return true;
00197     }
00198     
00204     protected function _loadDependencies ($module) {
00205         foreach ($this->_getDependencies($module) as $module => $dep) {
00206             list($dep_module_name,$dep_module_version) = explode('-', $dep);
00207             
00208             if ($dep_module_name == 'axiom')
00209                 continue;
00210             
00211             try {
00212                 if (!$this->load($dep_module_name)) {
00213                     return false;
00214                 }
00215             }
00216             catch (Exception $e) {
00217                 return false;
00218             }
00219         }
00220         return true;
00221     }
00222     
00230     protected function _cache () {
00231         if (!$this->_options['cache_dir'])
00232                                                 return false;
00233                                 
00234                                 $buffer = '<?php $modules=' . var_export($this->_modules, true) . '; ?>';
00235                                 return (boolean)file_put_contents($this->_options['cache_dir'] . '/' . self::CACHE_FILE, $buffer);
00236     }
00237     
00244     protected static function _parseModuleVersion ($version) {
00245         list($maj,$min,$build) = explode('.', $version);
00246         return $maj * 10000 + $min * 100 + $build;
00247     }
00248     
00262     protected static function _compareVersions ($version_a, $version_b) {
00263         if (is_string($version_a))
00264             $version_a = self::_parseModuleVersion($version_a);
00265             
00266         if (is_string($version_b))
00267             $version_b = self::_parseModuleVersion($version_b);
00268             
00269         return $version_a >= $version_b;
00270     }
00271 }
 All Data Structures Files Functions Variables