Go to the documentation of this file.00001 <?php
00010 final class factory {
00011
00017 private static $cfg;
00018
00024 private static $loadedCfg = array();
00025
00031 private static $loadedClass = array();
00032
00038 private static $loadFiles = array();
00039
00045 private static $cacheLoad = null;
00046
00052 private static $saveCacheLoad = false;
00053
00059 private static $constants;
00060
00061
00065 private function __construct() {}
00066
00070 public static function init() {
00071 self::$constants = get_defined_constants(true);
00072 self::initCache();
00073 self::$cfg = new config(factory::loadCfg(__CLASS__));
00074 }
00075
00079 public static function initCache() {
00080 self::$saveCacheLoad = false;
00081 self::$cacheLoad = cache::getInstance();
00082 self::$cacheLoad->get(self::$loadFiles, array(
00083 'ttl'=>0,
00084 'id'=>'load',
00085 'request'=>array('uri'=>false,'meth'=>array()),
00086 'serialize'=>true
00087 ));
00088 }
00089
00093 public static function saveCache() {
00094 if (self::$saveCacheLoad)
00095 self::$cacheLoad->save();
00096 file::saveCache();
00097 }
00098
00106 public static function loadCfg($className, $searchParent=true) {
00107 if (!array_key_exists($className, self::$loadedCfg)) {
00108 self::$loadedCfg[$className] = array();
00109
00110 if ($searchParent) {
00111 $ref = new nReflection($className);
00112
00113 if ($parent = $ref->getParentClass())
00114 self::mergeCfg(self::$loadedCfg[$className], self::loadCfg($parent->getName()));
00115
00116
00117 if (count($implements = $ref->getInterfaces()) > 0)
00118 foreach($implements as $imp)
00119 self::mergeCfg(self::$loadedCfg[$className], self::loadCfg($imp->getName()));
00120 }
00121
00122 $listCfg = file::nyroExists(Array(
00123 'name'=>$className,
00124 'type'=>'cfg',
00125 'rtl'=>false,
00126 'list'=>true
00127 ));
00128
00129 if (!empty($listCfg))
00130 foreach($listCfg as $lc) {
00131 include($lc);
00132 if (isset($cfg))
00133 self::mergeCfg(self::$loadedCfg[$className], $cfg, $className);
00134 $cfg = null;
00135 }
00136 self::mergeCfg(self::$loadedCfg[$className], nyro::getGlobalCfg($className));
00137 self::removeKeepUnique(self::$loadedCfg[$className]);
00138 }
00139 return self::$loadedCfg[$className];
00140 }
00141
00148 public static function mergeCfg(array &$prm, array $cfg) {
00149 if (!array_key_exists(REPLACECONF, $cfg)) {
00150 $keepUnique = array_key_exists(KEEPUNIQUE, $prm);
00151 foreach($cfg as $k=>&$v) {
00152 if (is_numeric($k) && !$keepUnique) {
00153 $prm[] = &$v;
00154 } else if (is_array($v) && array_key_exists($k, $prm) && is_array($prm[$k]))
00155 self::mergeCfg($prm[$k], $v);
00156 else
00157 $prm[$k] = &$v;
00158 }
00159 } else {
00160 unset($cfg[REPLACECONF]);
00161 $prm = $cfg;
00162 }
00163 }
00164
00170 private static function removeKeepUnique(array &$prm) {
00171 unset($prm[KEEPUNIQUE]);
00172 foreach($prm as &$v) {
00173 if (is_array($v))
00174 self::removeKeepUnique($v);
00175 }
00176 }
00177
00185 public static function get($className, array $cfg = array()) {
00186 self::load($className);
00187 $ref = new nReflection();
00188 if ($ref->rebuild($className)) {
00189 $prm = self::loadCfg($className);
00190 self::mergeCfg($prm, $cfg);
00191 $inst = $ref->newInstanceCfg(new config($prm));
00192 return $inst;
00193 } else
00194 throw new nException('Factory - load: Unable to bluid '.$className.'.');
00195 }
00196
00204 public static function getHelper($className, array $cfg = array()) {
00205 return self::get('helper_'.$className, $cfg);
00206 }
00207
00218 public static function getModule($name, array $cfg=array(), &$scaffold = false, $allowScaffold=true) {
00219 $className = 'module_'.$name.'_controller';
00220 if (!self::isCreable($className)) {
00221 if ($allowScaffold && in_array($name, db::getInstance()->getTables())) {
00222 $className = self::$cfg->scaffoldController;
00223 $cfg['name'] = $name;
00224 $scaffold = true;
00225 } else
00226 throw new module_exception('Factory - getModule: Name '.$name.' unknown.');
00227 }
00228 return self::get($className, $cfg);
00229 }
00230
00238 public static function load($className) {
00239 if (!class_exists($className) && !in_array($className, self::$loadedClass)) {
00240 if (!array_key_exists($className, self::$loadFiles)) {
00241 if ($file = file::nyroExists(array('name'=>$className))) {
00242 require($file);
00243 self::$loadFiles[$className] = array($file);
00244 self::$saveCacheLoad = true;
00245 self::$loadedClass[] = $className;
00246
00247 if (defined('RUNKIT_VERSION')) {
00248 $filesExtend = file::nyroExists(Array(
00249 'name'=>$className,
00250 'type'=>'extend',
00251 'rtl'=>false,
00252 'list'=>true
00253 ));
00254 if (!empty($filesExtend)) {
00255 self::$loadFiles[$className][1] = $filesExtend;
00256 foreach($filesExtend as $fe)
00257 runkit_import($fe);
00258 }
00259 }
00260 } else if (!lib::load($className)) {
00261 throw new nException('Factory - load: Unable to find the file for '.$className.'.');
00262 }
00263 } else {
00264 require(self::$loadFiles[$className][0]);
00265 self::$loadedClass[] = $className;
00266 if (defined('RUNKIT_VERSION') && array_key_exists(1, self::$loadFiles[$className])) {
00267 foreach(self::$loadFiles[$className][1] as $fe)
00268 runkit_import($fe);
00269 }
00270 }
00271 }
00272 return true;
00273 }
00274
00280 public static function isCreable($className) {
00281 return (file::nyroExists(array('name'=>$className)) !== false);
00282 }
00283
00284 }