Public paste
Undefined
By: Guest | Date: Mar 27 2010 12:39 | Format: PHP | Expires: never | Size: 5.15 KB | Hits: 878

  1. <?php
  2.  
  3.         FINAL CLASS ILLI_InstanceLoader
  4.         {
  5.                 private $instanceQueue     = array();
  6.                 private $classNamePrefix   = '';
  7.                 private $classNameSuffix   = '';
  8.                 private $referenceObject   = NULL;
  9.                 private $fileLocation      = '';
  10.                 private $includeLocation   = '';
  11.  
  12.                 private function __clone() {}
  13.  
  14.                 public function __construct($fileLocation = '')
  15.                 {
  16.                         $this->fileLocation = $fileLocation;
  17.  
  18.  
  19.                         // yayaaa it suxx                                                                                                
  20.  
  21.                         require_once ILLI_INSTALL_DIR.'ILLI_Sequence'.DIRECTORY_SEPARATOR.'Sequence_InstanceConstructeur.Abstract.php';  
  22.                        
  23.                         require_once ILLI_INSTALL_DIR.'ILLI_Sequence'.DIRECTORY_SEPARATOR.'Sequence_ShortcutsPrimary.Abstract.php';
  24.                         require_once ILLI_INSTALL_DIR.'ILLI_Sequence'.DIRECTORY_SEPARATOR.'Sequence_ShortcutsSecondary.Abstract.php';
  25.                         require_once ILLI_INSTALL_DIR.'ILLI_Sequence'.DIRECTORY_SEPARATOR.'Sequence_ShortcutsTertiary.Abstract.php';
  26.                         require_once ILLI_INSTALL_DIR.'ILLI_Sequence'.DIRECTORY_SEPARATOR.'Sequence_ShortcutsQuarternary.Abstract.php';
  27.                         require_once ILLI_INSTALL_DIR.'ILLI_Sequence'.DIRECTORY_SEPARATOR.'Sequence_ShortcutsQuinary.Abstract.php';
  28.                         require_once ILLI_INSTALL_DIR.'ILLI_Sequence'.DIRECTORY_SEPARATOR.'Sequence_ShortcutsSenary.Abstract.php';
  29.                         require_once ILLI_INSTALL_DIR.'ILLI_Sequence'.DIRECTORY_SEPARATOR.'Sequence_ShortcutsSeptary.Abstract.php';
  30.                         require_once ILLI_INSTALL_DIR.'ILLI_Sequence'.DIRECTORY_SEPARATOR.'Sequence_ShortcutsOctanary.Abstract.php';
  31.                         require_once ILLI_INSTALL_DIR.'ILLI_Sequence'.DIRECTORY_SEPARATOR.'Sequence_ShortcutsNonary.Abstract.php';
  32.                         require_once ILLI_INSTALL_DIR.'ILLI_Sequence'.DIRECTORY_SEPARATOR.'Sequence_ShortcutsDenary.Abstract.php';
  33.                        
  34.                         require_once ILLI_INSTALL_DIR.'ILLI_Sequence'.DIRECTORY_SEPARATOR.'Exception.php';
  35.                         require_once ILLI_INSTALL_DIR.'ILLI_Sequence'.DIRECTORY_SEPARATOR.'Abstract.php';
  36.                 }
  37.  
  38.                 public function __get($name)
  39.                 {
  40.                         try
  41.                         {
  42.                                 if(!is_string($name) || empty($name) || is_null($name))
  43.                                         throw new ILLI_InstanceLoader_Exception
  44.                                                 ('Illegal instance-name given.');
  45.  
  46.                                 $name = $this->classNamePrefix.$name.$this->classNameSuffix;
  47.  
  48.                                 if(!array_key_exists($name, $this->instanceQueue))
  49.                                         throw new ILLI_InstanceLoader_Exception
  50.                                                 ('Instance '.$name.' not found.');
  51.                         }
  52.                         catch(ILLI_InstanceLoader_Exception $e){$e->w();}
  53.  
  54.                         return $this->instanceQueue[$name];
  55.                 }
  56.  
  57.                 public function setNameMask($prefix = '', $suffix = '')
  58.                 {
  59.                         try
  60.                         {
  61.                                 if(!is_string($prefix))
  62.                                         throw new ILLI_InstanceLoader_Exception
  63.                                                 ('Illegal prefix of instance-name given.');
  64.  
  65.                                 if(!is_string($suffix))
  66.                                         throw new ILLI_InstanceLoader_Exception
  67.                                                 ('Illegal suffix of instance-name given.');
  68.  
  69.                                 $this->classNamePrefix = $prefix;
  70.                                 $this->classNameSuffix = $suffix;
  71.                         }
  72.                         catch(ILLI_InstanceLoader_Exception $e){$e->w();}
  73.  
  74.                         return $this;
  75.                 }
  76.  
  77.                 public function setReferenceObject(&$OBJ)
  78.                 {
  79.                         try
  80.                         {
  81.                                 if(!is_object($OBJ))
  82.                                         throw new ILLI_InstanceLoader_Exception
  83.                                                 ('Illegal reference-object given.');
  84.  
  85.                                 $this->referenceObject =& $OBJ;
  86.                         }
  87.                         catch(ILLI_InstanceLoader_Exception $e){$e->w();}
  88.  
  89.                         return $this;
  90.                 }
  91.  
  92.                 public function getReferenceObject()
  93.                 {
  94.                         return $this->referenceObject;
  95.                 }
  96.  
  97.                 private function loadClassFile($type, $required = FALSE)
  98.                 {
  99.                         $path = $this->includeLocation.$type.'.php';
  100.                         try
  101.                         {
  102.                                 if($required)
  103.                                         if(!file_exists($path))
  104.                                                  throw new ILLI_InstanceLoader_Exception
  105.                                                         ('Class-File '.$path.' not found.');
  106.                         }
  107.                         catch(ILLI_InstanceLoader_Exception $e){$e->w();}
  108.  
  109.                         if(file_exists($path))
  110.                                 $loaded = require_once $path;
  111.  
  112.                         return $this;
  113.                 }
  114.  
  115.                 public function load($name = '')
  116.                 {
  117.                         try
  118.                         {
  119.                                 if(!is_string($name) || empty($name) || is_null($name))
  120.                                         throw new ILLI_InstanceLoader_Exception
  121.                                                 ('Illegal instance-name given.');
  122.  
  123.                                 if(empty($this->fileLocation))
  124.                                         $this->fileLocation = ILLI_INSTALL_DIR.$this->classNamePrefix.DIRECTORY_SEPARATOR;
  125.  
  126.                                 $this->includeLocation = $this->fileLocation.$name.DIRECTORY_SEPARATOR;
  127.  
  128.                                 $this
  129.                                         ->loadClassFile('Exception')
  130.                                         ->loadClassFile('Abstract')
  131.                                         ->loadClassFile('Class', TRUE)
  132.                                         ->loadClassFile('Package');
  133.  
  134.                                 $className = $this->classNamePrefix.$name.$this->classNameSuffix;
  135.  
  136.                                 if(!class_exists($className, FALSE))
  137.                                         throw new ILLI_InstanceLoader_Exception
  138.                                                 ('Class '.$className.' not found.');
  139.  
  140.                                 if(array_key_exists($className, $this->instanceQueue))
  141.                                         throw new ILLI_InstanceLoader_Exception
  142.                                                 ('Instance of '.$className.' exists.');
  143.  
  144.                                 !is_null($this->referenceObject)
  145.                                         ? $this->instanceQueue[$className] =& new $className($this->referenceObject, $this->includeLocation)
  146.                                         : $this->instanceQueue[$className] =& new $className();
  147.                         }
  148.                         catch(ILLI_InstanceLoader_Exception $e){$e->w();}
  149.  
  150.                         return $this;
  151.                 }
  152.  
  153.                 public function init()
  154.                 {
  155.                         foreach($this->instanceQueue as $instance)
  156.                                 $instance->init();
  157.  
  158.                         return $this;
  159.                 }
  160.  
  161.                 public function run()
  162.                 {
  163.                         foreach($this->instanceQueue as $instance)
  164.                                 $instance->execute();
  165.  
  166.                         return $this;
  167.                 }
  168.         }