Advertisement
fruffl

Untitled

Feb 16th, 2012
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.85 KB | None | 0 0
  1. <?PHP
  2.     /**
  3.      * ILLI
  4.      *
  5.      * @category   ILLI
  6.      * @package    ILLI
  7.      * @link       http://illi.be
  8.      * @license    http://l.illi.be
  9.      * @copyright  2007-2011, ILLI Conference
  10.      */
  11.  
  12.     /**
  13.      * ILLI Core
  14.      *
  15.      * init -> configure -> run
  16.      *
  17.      * @category   ILLI
  18.      * @package    ILLI
  19.      * @link       http://illi.be
  20.      * @license    http://l.illi.be
  21.      * @copyright  2007-2011, ILLI Conference
  22.      */
  23.     CLASS ILLI
  24.     {
  25.         /**
  26.          * instance
  27.          *
  28.          * @var ILLI $__ILLI
  29.          * @static
  30.          */
  31.         private static $__ILLI = NULL;
  32.        
  33.         /**
  34.          * VENDOR-path
  35.          *
  36.          * @var string $__installDir
  37.          * @static
  38.          */
  39.         private static $__installDir = NULL;
  40.        
  41.         /**
  42.          * initialize instance of ILLI
  43.          *
  44.          * @return ILLI instance
  45.          * @uses ILLI::getInstallDir()
  46.          * @uses ILLI::registerIncludePath()
  47.          * @uses ILLI::ILLI
  48.          * @uses ILLI::$__ILLI
  49.          * @uses ILLI::__construct()
  50.          * @static
  51.          */
  52.         public static function init()
  53.         {
  54.             self::registerIncludePath(self::getInstallDir());
  55.             self::registerIncludePath(self::getInstallDir().__CLASS__);
  56.             self::registerAutoloadFunction();
  57.                    
  58.             return ((NULL !== self::$__ILLI)
  59.                 ? self::$__ILLI
  60.                 : self::$__ILLI = new self);
  61.         }
  62.        
  63.         /**
  64.          * register include-path
  65.          *
  66.          * @param string $path full directory
  67.          * @return ILLI|NULL instance of ILLI
  68.          * @uses ILLI::$__ILLI
  69.          * @uses ILLI_Exception_DataHandlerNotFound
  70.          * @uses ILLI_Exception_DataHandlerNotFound::ERROR_PATH_NOT_FOUND
  71.          * @throws {@link ILLI_Exception_DataHandlerNotFound::ERROR_PATH_NOT_FOUND}
  72.          * @static
  73.          */
  74.         public static function registerIncludePath($path = NULL)
  75.         {
  76.             $path = realpath($path);
  77.             if(FALSE === $path)
  78.                 throw new ILLI_Exception_DataHandlerNotFound
  79.                 (ILLI_Exception_DataHandlerNotFound::ERROR_PATH_NOT_FOUND, $file);
  80.                
  81.             set_include_path(implode(PATH_SEPARATOR, array
  82.             (
  83.                 $path,
  84.                 get_include_path()
  85.             )));
  86.            
  87.             return self::$__ILLI;
  88.         }
  89.        
  90.         /**
  91.          * get install-path (VENDOR-path)
  92.          *
  93.          * @return string install-path (VENDOR-path)
  94.          * @uses ILLI::$__installDir
  95.          * @static
  96.          */
  97.         public static function getInstallDir()
  98.         {
  99.             return ((NULL !== self::$__installDir)
  100.                 ? self::$__installDir
  101.                 : (self::$__installDir = dirname(dirname(__FILE__)).DIRECTORY_SEPARATOR));
  102.         }
  103.        
  104.         /**
  105.          * load php-file
  106.          *
  107.          * @param string $file absolute or relative path below {@link ILLI::$__installDir}
  108.          * @return bool
  109.          * @uses ILLI::getInstallDir()
  110.          * @uses ILLI::loadFile()
  111.          * @uses ILLI_Exception_DataHandlerNotFound
  112.          * @uses ILLI_Exception_DataHandlerNotFound::ERROR_FILE_NOT_FOUND
  113.          * @throws {@link ILLI_Exception_DataHandlerNotFound::ERROR_FILE_NOT_FOUND}
  114.          * @static
  115.          */
  116.         public static function loadFile($file)
  117.         {
  118.             if(is_file($file))
  119.             {
  120.                 require_once $file;
  121.                 return TRUE;
  122.             }
  123.            
  124.             if(substr($file, 0, strlen(self::getInstallDir())) != self::getInstallDir())
  125.                 return self::loadFile(self::getInstallDir().$file);
  126.            
  127.             throw new ILLI_Exception_DataHandlerNotFound
  128.                 (ILLI_Exception_DataHandlerNotFound::ERROR_FILE_NOT_FOUND, $file);
  129.                
  130.             return FALSE;
  131.         }
  132.        
  133.         /**
  134.          * load class
  135.          *
  136.          * @param string $request class-name or equivalent file-path below {@link ILLI::$__installDir}
  137.          * @return bool
  138.          * @uses ILLI::registerAutoloadFunction()
  139.          * @uses ILLI::loadFile()
  140.          * @uses ILLI::getInstallDir()
  141.          * @uses ILLI::isInstantiable()
  142.          * @static
  143.          */
  144.         public static function loadClass($request)
  145.         {
  146.             $class      = str_replace(array('/', '\\'), '_', $request);
  147.             $path       = str_replace(array('_'), DIRECTORY_SEPARATOR, $class);
  148.             $file       = $path.'.php';
  149.            
  150.             //var_dump(array($request, $class, $path, $file));
  151.            
  152.             return ((TRUE === self::loadFile(self::getInstallDir().$file))
  153.                 ? self::isInstantiable($class)
  154.                 : FALSE);
  155.         }
  156.        
  157.         /**
  158.          * instantiable-check of a class
  159.          *
  160.          * @param string $class class-name
  161.          * @return bool
  162.          * @uses ILLI_Exception_DataHandlerNotFound
  163.          * @uses ILLI_Exception_DataHandlerNotFound::ERROR_CLASS_NOT_FOUND
  164.          * @throws {@link ILLI_Exception_DataHandlerNotFound::ERROR_CLASS_NOT_FOUND}
  165.          * @static
  166.          */
  167.         public static function isInstantiable($class)
  168.         {
  169.             try
  170.             {
  171.                 $r = new ReflectionClass($class);
  172.                 return $r->isInstantiable();
  173.             }
  174.             catch(ReflectionException $e)
  175.             {
  176.                 throw new ILLI_Exception_DataHandlerNotFound
  177.                 (ILLI_Exception_DataHandlerNotFound::ERROR_CLASS_NOT_FOUND, $e, $class);
  178.             }
  179.         }
  180.        
  181.         /**
  182.          * complicated stuff
  183.          */
  184.         private function __construct()
  185.         {
  186.         }
  187.        
  188.         /**
  189.          * register __autoload
  190.          *
  191.          * @uses ILLI::loadClass()
  192.          * @static
  193.          */
  194.         private static function registerAutoloadFunction()
  195.         {
  196.             spl_autoload_register(array(__CLASS__, 'loadClass'));
  197.         }
  198.        
  199.     }
  200.  
  201. try
  202. {
  203.     ILLI::init();
  204.     new blaaa;
  205. }
  206. catch(ILLI_Exception_DataHandlerNotFound $e)
  207. {
  208.     switch($e->getCode()...
  209. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement