nyroFwk  0.2
factory.class.php
Go to the documentation of this file.
1 <?php
10 final class factory {
11 
17  private static $cfg;
18 
24  private static $loadedCfg = array();
25 
31  private static $loadedClass = array();
32 
38  private static $loadFiles = array();
39 
45  private static $cacheLoad = null;
46 
52  private static $saveCacheLoad = false;
53 
59  private static $constants;
60 
66  public static $throwOnLoad = true;
67 
68 
72  private function __construct() {}
73 
77  public static function init() {
78  self::$constants = get_defined_constants(true);
79  self::initCache();
80  self::$cfg = new config(factory::loadCfg(__CLASS__));
81  }
82 
86  public static function initCache() {
87  self::$saveCacheLoad = false;
88  self::$cacheLoad = cache::getInstance();
89  self::$cacheLoad->get(self::$loadFiles, array(
90  'ttl'=>0,
91  'id'=>'load',
92  'request'=>array('uri'=>false,'meth'=>array()),
93  'serialize'=>true
94  ));
95  }
96 
100  public static function saveCache() {
101  if (self::$saveCacheLoad)
102  self::$cacheLoad->save();
103  file::saveCache();
104  }
105 
113  public static function loadCfg($className, $searchParent=true) {
114  if (!array_key_exists($className, self::$loadedCfg)) {
115  self::$loadedCfg[$className] = array();
116 
117  if ($searchParent) {
118  $ref = new nReflection($className);
119  // Load the parent class configuration
120  if ($parent = $ref->getParentClass())
121  self::mergeCfg(self::$loadedCfg[$className], self::loadCfg($parent->getName()));
122 
123  // Load the implements class configuration
124  if (count($implements = $ref->getInterfaces()) > 0)
125  foreach($implements as $imp)
126  self::mergeCfg(self::$loadedCfg[$className], self::loadCfg($imp->getName()));
127  }
128 
129  $listCfg = file::nyroExists(Array(
130  'name'=>$className,
131  'type'=>'cfg',
132  'rtl'=>false,
133  'list'=>true
134  ));
135 
136  if (!empty($listCfg))
137  foreach($listCfg as $lc) {
138  include($lc);
139  if (isset($cfg))
140  self::mergeCfg(self::$loadedCfg[$className], $cfg, $className);
141  $cfg = null;
142  }
143  self::mergeCfg(self::$loadedCfg[$className], nyro::getGlobalCfg($className));
144  self::removeKeepUnique(self::$loadedCfg[$className]);
145  }
146  return self::$loadedCfg[$className];
147  }
148 
155  public static function mergeCfg(array &$prm, array $cfg) {
156  if (!array_key_exists(REPLACECONF, $cfg)) {
157  $keepUnique = array_key_exists(KEEPUNIQUE, $prm);
158  foreach($cfg as $k=>&$v) {
159  if (is_numeric($k) && !$keepUnique) {
160  $prm[] = &$v;
161  } else if (is_array($v) && array_key_exists($k, $prm) && is_array($prm[$k]))
162  self::mergeCfg($prm[$k], $v);
163  else
164  $prm[$k] = &$v;
165  }
166  } else {
167  unset($cfg[REPLACECONF]);
168  $prm = $cfg;
169  }
170  }
171 
177  private static function removeKeepUnique(array &$prm) {
178  unset($prm[KEEPUNIQUE]);
179  foreach($prm as &$v) {
180  if (is_array($v))
181  self::removeKeepUnique($v);
182  }
183  }
184 
192  public static function get($className, array $cfg = array()) {
193  if (self::$cfg && $tmp = self::$cfg->getInArray('classAlias', $className))
194  $className = $tmp;
195  self::load($className);
196  $ref = new nReflection();
197  if ($ref->rebuild($className)) {
198  $prm = self::loadCfg($className);
199  self::mergeCfg($prm, $cfg);
200  $inst = $ref->newInstanceCfg(new config($prm));
201  return $inst;
202  } else
203  throw new nException('Factory - load: Unable to bluid '.$className.'.');
204  }
205 
213  public static function getHelper($className, array $cfg = array()) {
214  return self::get('helper_'.$className, $cfg);
215  }
216 
227  public static function getModule($name, array $cfg=array(), &$scaffold = false, $allowScaffold=true) {
228  $className = 'module_'.$name.'_controller';
229  if (!self::isCreable($className)) {
230  if ($allowScaffold && in_array($name, db::getInstance()->getTables())) {
231  $className = self::$cfg->scaffoldController;
232  $cfg['name'] = $name;
233  $scaffold = true;
234  } else
235  throw new module_exception('Factory - getModule: Name '.$name.' unknown.');
236  }
237  return self::get($className, $cfg);
238  }
239 
247  public static function load($className) {
248  if (!class_exists($className) && !in_array($className, self::$loadedClass)) {
249  if (!array_key_exists($className, self::$loadFiles)) {
250  if ($file = file::nyroExists(array('name'=>$className))) {
251  require($file);
252  self::$loadFiles[$className] = array($file);
253  self::$saveCacheLoad = true;
254  self::$loadedClass[] = $className;
255 
256  if (defined('RUNKIT_VERSION')) {
257  $filesExtend = file::nyroExists(Array(
258  'name'=>$className,
259  'type'=>'extend',
260  'rtl'=>false,
261  'list'=>true
262  ));
263  if (!empty($filesExtend)) {
264  self::$loadFiles[$className][1] = $filesExtend;
265  foreach($filesExtend as $fe)
266  runkit_import($fe);
267  }
268  }
269  } else if (!lib::load($className)) {
270  if (self::$throwOnLoad)
271  throw new nException('Factory - load: Unable to find the file for '.$className.'.');
272  else
273  return false;
274  }
275  } else {
276  require(self::$loadFiles[$className][0]);
277  self::$loadedClass[] = $className;
278  if (defined('RUNKIT_VERSION') && array_key_exists(1, self::$loadFiles[$className])) {
279  foreach(self::$loadFiles[$className][1] as $fe)
280  runkit_import($fe);
281  }
282  }
283  }
284  return true;
285  }
286 
292  public static function isCreable($className) {
293  return (file::nyroExists(array('name'=>$className)) !== false);
294  }
295 
296 }
static init()
static saveCache()
Definition: file.class.php:84
static $constants
static getModule($name, array $cfg=array(), &$scaffold=false, $allowScaffold=true)
static $cacheLoad
static getInstance(array $cfg=array())
Definition: cache.class.php:30
static $loadedClass
static getGlobalCfg($name)
Definition: nyro.class.php:122
static getHelper($className, array $cfg=array())
static removeKeepUnique(array &$prm)
static $loadFiles
static $loadedCfg
static initCache()
static $throwOnLoad
static $cfg
static nyroExists($prm)
Definition: file.class.php:137
static loadCfg($className, $searchParent=true)
static mergeCfg(array &$prm, array $cfg)
static $saveCacheLoad
static saveCache()
static load($name)
Definition: lib.class.php:51
static isCreable($className)
static getInstance($cfg=null)
Definition: db.class.php:58
static load($className)
Generated on Sun Oct 15 2017 22:25:20 for nyroFwk by doxygen 1.8.13