Пример #1
0
 public static function autoload($className)
 {
     $debug = false;
     // turn on for performance checking of the autoloader
     if (in_array($className, self::$incompatibleClasses)) {
         throw new Ajde_Exception('Could not create instance of incompatible class ' . $className . '.', 90018);
     }
     self::$files = array();
     $isNamespace = false;
     foreach (self::$namespaces as $namespace) {
         if (substr($className, 0, strlen($namespace . '_')) == $namespace . '_') {
             $isNamespace = true;
             break;
         }
     }
     if ($isNamespace) {
         // LIB class
         $dirs = array(LIB_DIR);
         self::initFiles($className);
     } else {
         // Non LIB related classes
         if (substr_count($className, 'Controller') > 0) {
             $dirs = array(MODULE_DIR);
             $controllerName = str_replace('Controller', '', $className);
             if (strtolower(substr($controllerName, 1)) != substr($controllerName, 1)) {
                 // See if we got more capitals
                 // ModuleSubcontrollerController.php naming
                 $combinedName = substr($controllerName, 0, 1) . preg_replace('/([A-Z])/', ':\\1', substr($controllerName, 1));
                 list($moduleName, $controllerName) = explode(":", $combinedName);
                 self::addFile(strtolower($moduleName) . "/" . $moduleName . $controllerName . 'Controller.php');
             } else {
                 // ModuleController.php naming
                 self::addFile(strtolower(str_replace('Controller', '', $className)) . "/" . $className . '.php');
             }
         } elseif (substr_count($className, 'Config') > 0) {
             $dirs = array(CONFIG_DIR);
             // Namespace_Class.php naming
             self::addFile($className . ".php");
         } else {
             $dirs = self::$dirs;
             // FooModel.php, BarCollection.php, etc. naming
             self::addFile($className . '.php');
         }
     }
     /*// In order to use Ajde_Event here, require neccesary files statically :(
     		require_once(LIB_DIR.'Ajde/Object/Object.php');
     		require_once(LIB_DIR.'Ajde/Object/Static.php');
     		require_once(LIB_DIR.'Ajde/Event/Event.php');
     		require_once(LIB_DIR.'Ajde/Exception/Exception.php');
     		Ajde_Event::trigger('Ajde_Core_Autoloader', 'beforeSearch', array($className));*/
     if ($debug) {
         self::$queries++;
         echo "<span style='color:orange;'>LOOKING FOR</span> {$className} <br/>";
     }
     foreach ($dirs as $dir) {
         foreach (self::$files as $file) {
             $path = self::$dirPrepend . $dir . $file;
             if (is_file($path)) {
                 // TODO: performance gain?
                 // if (class_exists('Ajde_Cache')) {
                 // 	Ajde_Cache::getInstance()->addFile($path);
                 // }
                 if ($debug) {
                     echo "<span style='color:green;'>FOUND</span> {$path} <br/>";
                     echo "<span style='font-size:smaller;color:gray;'>stats : (missed/lookups) : " . self::$missed . "/" . self::$queries . " : " . (int) (self::$missed / self::$queries * 100) . "% missed</span><br/>";
                 }
                 require_once $path;
                 return;
             } else {
                 if ($debug) {
                     echo "<span style='color:red;'>CONTINUE</span> {$path} <br/>";
                     self::$missed++;
                 }
             }
         }
     }
     /*
      * Throwing exceptions is only possible as of PHP 5.3.0
      * See: http://php.net/manual/en/language.oop5.autoload.php
      */
     if (version_compare(PHP_VERSION, '5.3.0') >= 0 && self::exists('Ajde_Core_Autoloader_Exception')) {
         // TODO: Custom Exceptions are still causing problems
         // throw new Ajde_Core_Autoloader_Exception("Unable to load $className", 90005);
         throw new Exception("Unable to load {$className}", 90005);
     }
 }