Advertisement
Guest User

iharos_iharos

a guest
Sep 3rd, 2014
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.63 KB | None | 0 0
  1. <?php
  2. namespace Iharos;
  3.  
  4. class Iharos
  5. {
  6.     // The one and only instance of Iharos
  7.     public static $instance;
  8.    
  9.     /* Array of module aliases and their namespaces (locations)
  10.      * @var module_registry array
  11.     */
  12.     public $module_registry;
  13.    
  14.     /* Array of module instances
  15.      * @var $module_instances array
  16.     */
  17.     public $module_instances;
  18.    
  19.     public $base_dir;
  20.  
  21.    
  22.     public function __construct()
  23.     {
  24.         if (is_object(static::$instance)) {
  25.             return static::$instance;
  26.         } else {
  27.             static::$instance = $this;
  28.         }
  29.        
  30.         $this->base_dir = realpath(
  31.                 __DIR__
  32.                 . DIRECTORY_SEPARATOR
  33.                 . '..'
  34.                 . DIRECTORY_SEPARATOR
  35.                 . '..'
  36.             )
  37.             . DIRECTORY_SEPARATOR;
  38.        
  39.         $this->registerAutoLoader();
  40.        
  41.         $this->module_registry = array(
  42.             // kernel modules
  43.             'Iharos'        => 'Iharos\Kernel\Iharos',
  44.             'Label'         => 'Iharos\Label\Label',
  45.            
  46.             //'OtherVendor' => 'OtherVendor\Package\Class'
  47.         );
  48.        
  49.         $module_instances = array(
  50.             'Iharos\Kernel\Iharos'          => $this, // prevent redeclaration
  51.         );
  52.     }
  53.    
  54.     public static function getInstance()
  55.     {
  56.         echo "<br>app::getInstance()";
  57.         return static::$instance;
  58.     }
  59.    
  60.     /* Resolve the instance of a module identified by its alias.
  61.      * @var $name string
  62.      * @return module instance
  63.     */
  64.     public function manifest($name)
  65.     {
  66.         echo "<br>manifest($name)";
  67.         if (!isset($this->module_registry[$name])) {
  68.             $this->error("Module alias '$name' was not found in registry.");
  69.         }
  70.        
  71.         $module = $this->module_registry[$name];
  72.         $module_name = $module . 'Module';
  73.        
  74.         if (!isset($this->module_instances[$module])) {
  75.             $this->module_instances[$module] = new $module_name();
  76.         } else {
  77.             echo "<br>manifest: module instance '$module' has been found";
  78.         }
  79.        
  80.         return $this->module_instances[$module];
  81.     }
  82.  
  83.    
  84.     public function autoLoader($class)
  85.     {
  86.         echo "<br><br>app->autoLoader($class)";
  87.        
  88.         $class_path_parts = explode('\\', $class);
  89.         $class_name = array_pop($class_path_parts);
  90.        
  91.         if (isset($this->module_registry[$class_name])) {
  92.             $class = $this->module_registry[$class_name];
  93.         }
  94.        
  95.         $class = str_replace('\\', DIRECTORY_SEPARATOR, $class);
  96.         $class = rtrim($class, DIRECTORY_SEPARATOR);
  97.         $class = ltrim($class, DIRECTORY_SEPARATOR);
  98.        
  99.         $path = $this->base_dir . $class . '.php';
  100.        
  101.         echo "<br>autoLoader: " . $path;
  102.        
  103.         if (!isset($path)) {
  104.             echo "<br>NOT FOUND: ". $path;
  105.         }
  106.        
  107.         require_once $path;
  108.     }
  109.  
  110.    
  111.     public function registerAutoLoader()
  112.     {
  113.         echo "<br>app->registerAutoLoader()";
  114.         spl_autoload_register(array($this, 'autoLoader')); //__NAMESPACE__ . '\\Iharos::autoLoader'
  115.     }
  116.    
  117.    
  118.     public function error($msg)
  119.     {
  120.         die($msg);
  121.     }
  122. }
  123. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement