Advertisement
fruffl

Class- & ClassFile-Loader

Nov 12th, 2011
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.50 KB | None | 0 0
  1. <?PHP
  2.     /**
  3.      * @todo exception handle
  4.      */
  5.     CLASS ILLI
  6.     {
  7.         const CLASSLOADER       = 'Loader';
  8.        
  9.         private static $LOADER      = NULL;
  10.         private static $BOOTLOADER  = NULL;
  11.         private static $SYSTEM      = NULL;
  12.         private static $SELF        = NULL;
  13.        
  14.         private function __construct()
  15.         {
  16.             try
  17.             {
  18.                 if(FALSE === class_exists('ILLI_Loader', FALSE))
  19.                     try
  20.                     {
  21.                         $loader = dirname(__FILE__).DIRECTORY_SEPARATOR.self::CLASSLOADER.DIRECTORY_SEPARATOR.'Class.php';
  22.                         if(FALSE === is_file($loader))
  23.                             throw new Exception('Class-file not found');
  24.                            
  25.                         if(FALSE === is_readable($loader))
  26.                             throw new Exception('Class-file not readable');
  27.                            
  28.                         require_once $loader;
  29.                        
  30.                         if(FALSE === class_exists('ILLI_Loader', FALSE))
  31.                             throw new Exception('Class-file is empty.');
  32.                     }
  33.                     catch(Exception $e)
  34.                     {
  35.                         throw new Exception('Class ILLI_Loader is not avail.', NULL, $e);
  36.                     }
  37.                
  38.                
  39.                 self::$LOADER = ILLI_Loader::init();
  40.             }
  41.             catch(Exception $e) { throw new Exception('Unable to use ILLI_Loader.', NULL, $e); }
  42.            
  43.                                
  44.             self::$SYSTEM       = ILLI_System::init();
  45.             self::$BOOTLOADER   = ILLI_Bootloader::init();
  46.            
  47.             var_dump(self::$LOADER);
  48.             var_dump(self::$SYSTEM);
  49.             var_dump(self::$BOOTLOADER);
  50.         }
  51.        
  52.         public static function init()
  53.         {
  54.             if(NULL !== self::$SELF)
  55.                 return;
  56.            
  57.             self::$SELF = new self;
  58.             return self::$SELF;
  59.         }
  60.     }
  61.  
  62.     CLASS ILLI_Loader
  63.     {
  64.         const PHP_FILE_EXT      = '.php';
  65.        
  66.         private static $SELF        = NULL;
  67.        
  68.         private static $installDir  = NULL;    
  69.         private static $autoload    = array(
  70.             'Package',
  71.             'Interface',
  72.             'Exception',
  73.             'Abstract',
  74.             'Class'
  75.         );
  76.        
  77.         private function __construct() {}
  78.        
  79.         public static function init()
  80.         {
  81.             if(NULL !== self::$SELF)
  82.                 return;
  83.            
  84.             self::registerIncludePath(self::getInstallDir());
  85.            
  86.             if(FALSE === function_exists('__autoload'))
  87.                 { function __autoload($class) { ILLI_Loader::load($class); } }
  88.             else    throw new Exception('Unable to register __autoload');
  89.                
  90.             return (self::$SELF = new self);
  91.         }
  92.        
  93.         public static function registerIncludePath()
  94.         {
  95.             if(FALSE === (bool) ($args = func_get_args()))
  96.                 return;
  97.            
  98.             set_include_path(implode
  99.             (
  100.                 PATH_SEPARATOR, array(implode
  101.                 (
  102.                     PATH_SEPARATOR,
  103.                     array_map('realpath', $args)
  104.                 ),
  105.                 get_include_path())
  106.             ));
  107.         }
  108.        
  109.         public static function getInstallDir()
  110.         {
  111.             return ((NULL !== self::$installDir)
  112.                 ? self::$installDir
  113.                 : (self::$installDir = dirname(dirname(dirname(__FILE__))).DIRECTORY_SEPARATOR));
  114.         }
  115.        
  116.         private static function loadFile($file)
  117.         {
  118.             if(is_file($file))
  119.             {
  120.                 require_once $file;
  121.                 return TRUE;
  122.             }
  123.            
  124.             return ((substr($file, 0, strlen(self::getInstallDir())) != self::getInstallDir())
  125.                 ? self::loadFile(self::getInstallDir().$file)
  126.                 : FALSE);
  127.         }
  128.        
  129.         public static function load($file)
  130.         {
  131.             $request    = $file;           
  132.             $class      = str_replace(array('/', '\\'), '_', $file);
  133.             $full       = explode('_', $class);        
  134.             $part       = $full;
  135.             $suffix     = array_pop($part);
  136.            
  137.             if(!in_array($suffix, self::$autoload))
  138.             {
  139.                 $suffix     = FALSE;
  140.                 $filename   = 'Class';
  141.                 $file       = implode(DIRECTORY_SEPARATOR, $full);
  142.             }
  143.             else
  144.             {
  145.                 $filename   = $suffix;
  146.                 $file       = implode(DIRECTORY_SEPARATOR, $part);
  147.             }
  148.            
  149.             // request: ILLI/Foo/Bar.php => file-name
  150.             // content: misc
  151.             //  try to load ILLI/Foo/Bar.php
  152.             if(substr($file, -4) == self::PHP_FILE_EXT)
  153.             {
  154.                 return self::loadFile($file);
  155.             }
  156.            
  157.             // request: ILLI/Foo/Baz => class-name
  158.             // content: ILLI_Foo_Baz
  159.             //  try to load ILLI/Foo/Baz.php
  160.             if(TRUE === self::loadFile($file.self::PHP_FILE_EXT))
  161.             {
  162.                 //return true;
  163.                 self::loadFile($file.self::PHP_FILE_EXT);
  164.                 return class_exists($request);
  165.             }
  166.            
  167.             // request: ILLI/Foo/Baz => class-name
  168.             // switch:  ILLI/Foo/Baz.php not found
  169.             // content: ILLI_Foo_Baz
  170.             //  try to load ILLI/Foo/Baz/Package.php
  171.             //  try to load ILLI/Foo/Baz/Interface.php
  172.             //  try to load ILLI/Foo/Baz/Exception.php
  173.             //  try to load ILLI/Foo/Baz/Abstract.php
  174.             //  try to load ILLI/Foo/Baz/Class.php
  175.             foreach(self::$autoload as $type)
  176.             {
  177.                 if(FALSE === $suffix)
  178.                 {
  179.                     self::loadFile($file.DIRECTORY_SEPARATOR.$type.self::PHP_FILE_EXT);
  180.                     continue;
  181.                 }
  182.                
  183.                
  184.                 if($type == $filename)
  185.                     self::loadFile($file.DIRECTORY_SEPARATOR.$type.self::PHP_FILE_EXT);
  186.             }
  187.            
  188.             return class_exists($class);
  189.         }
  190.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement