Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?PHP
- /**
- * @todo exception handle
- */
- CLASS ILLI
- {
- const CLASSLOADER = 'Loader';
- private static $LOADER = NULL;
- private static $BOOTLOADER = NULL;
- private static $SYSTEM = NULL;
- private static $SELF = NULL;
- private function __construct()
- {
- try
- {
- if(FALSE === class_exists('ILLI_Loader', FALSE))
- try
- {
- $loader = dirname(__FILE__).DIRECTORY_SEPARATOR.self::CLASSLOADER.DIRECTORY_SEPARATOR.'Class.php';
- if(FALSE === is_file($loader))
- throw new Exception('Class-file not found');
- if(FALSE === is_readable($loader))
- throw new Exception('Class-file not readable');
- require_once $loader;
- if(FALSE === class_exists('ILLI_Loader', FALSE))
- throw new Exception('Class-file is empty.');
- }
- catch(Exception $e)
- {
- throw new Exception('Class ILLI_Loader is not avail.', NULL, $e);
- }
- self::$LOADER = ILLI_Loader::init();
- }
- catch(Exception $e) { throw new Exception('Unable to use ILLI_Loader.', NULL, $e); }
- self::$SYSTEM = ILLI_System::init();
- self::$BOOTLOADER = ILLI_Bootloader::init();
- var_dump(self::$LOADER);
- var_dump(self::$SYSTEM);
- var_dump(self::$BOOTLOADER);
- }
- public static function init()
- {
- if(NULL !== self::$SELF)
- return;
- self::$SELF = new self;
- return self::$SELF;
- }
- }
- CLASS ILLI_Loader
- {
- const PHP_FILE_EXT = '.php';
- private static $SELF = NULL;
- private static $installDir = NULL;
- private static $autoload = array(
- 'Package',
- 'Interface',
- 'Exception',
- 'Abstract',
- 'Class'
- );
- private function __construct() {}
- public static function init()
- {
- if(NULL !== self::$SELF)
- return;
- self::registerIncludePath(self::getInstallDir());
- if(FALSE === function_exists('__autoload'))
- { function __autoload($class) { ILLI_Loader::load($class); } }
- else throw new Exception('Unable to register __autoload');
- return (self::$SELF = new self);
- }
- public static function registerIncludePath()
- {
- if(FALSE === (bool) ($args = func_get_args()))
- return;
- set_include_path(implode
- (
- PATH_SEPARATOR, array(implode
- (
- PATH_SEPARATOR,
- array_map('realpath', $args)
- ),
- get_include_path())
- ));
- }
- public static function getInstallDir()
- {
- return ((NULL !== self::$installDir)
- ? self::$installDir
- : (self::$installDir = dirname(dirname(dirname(__FILE__))).DIRECTORY_SEPARATOR));
- }
- private static function loadFile($file)
- {
- if(is_file($file))
- {
- require_once $file;
- return TRUE;
- }
- return ((substr($file, 0, strlen(self::getInstallDir())) != self::getInstallDir())
- ? self::loadFile(self::getInstallDir().$file)
- : FALSE);
- }
- public static function load($file)
- {
- $request = $file;
- $class = str_replace(array('/', '\\'), '_', $file);
- $full = explode('_', $class);
- $part = $full;
- $suffix = array_pop($part);
- if(!in_array($suffix, self::$autoload))
- {
- $suffix = FALSE;
- $filename = 'Class';
- $file = implode(DIRECTORY_SEPARATOR, $full);
- }
- else
- {
- $filename = $suffix;
- $file = implode(DIRECTORY_SEPARATOR, $part);
- }
- // request: ILLI/Foo/Bar.php => file-name
- // content: misc
- // try to load ILLI/Foo/Bar.php
- if(substr($file, -4) == self::PHP_FILE_EXT)
- {
- return self::loadFile($file);
- }
- // request: ILLI/Foo/Baz => class-name
- // content: ILLI_Foo_Baz
- // try to load ILLI/Foo/Baz.php
- if(TRUE === self::loadFile($file.self::PHP_FILE_EXT))
- {
- //return true;
- self::loadFile($file.self::PHP_FILE_EXT);
- return class_exists($request);
- }
- // request: ILLI/Foo/Baz => class-name
- // switch: ILLI/Foo/Baz.php not found
- // content: ILLI_Foo_Baz
- // try to load ILLI/Foo/Baz/Package.php
- // try to load ILLI/Foo/Baz/Interface.php
- // try to load ILLI/Foo/Baz/Exception.php
- // try to load ILLI/Foo/Baz/Abstract.php
- // try to load ILLI/Foo/Baz/Class.php
- foreach(self::$autoload as $type)
- {
- if(FALSE === $suffix)
- {
- self::loadFile($file.DIRECTORY_SEPARATOR.$type.self::PHP_FILE_EXT);
- continue;
- }
- if($type == $filename)
- self::loadFile($file.DIRECTORY_SEPARATOR.$type.self::PHP_FILE_EXT);
- }
- return class_exists($class);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement