Exemplo n.º 1
0
 public static function loadClass()
 {
     $system = CoreLoader::$system;
     $methodInfo = self::parseURI();
     self::$info =& $methodInfo;
     // 在解析路由之后,就注册自动加载,这样控制器可以继承类库文件夹里面的自定义父控制器,实现hook功能,达到拓展控制器的功能
     // 但是plugin模式下,路由器不再使用,那么这里就不会被执行,自动加载功能会失效,所以在每个instance方法里面再尝试加载一次即可,
     // 如此一来就能满足两种模式
     CoreLoader::classAutoloadRegister();
     // var_dump($methodInfo);
     if (file_exists($methodInfo['file'])) {
         include $methodInfo['file'];
         CoreInput::$router = $methodInfo;
         if (!CoreInput::isCli()) {
             // session自定义配置检查,只在非命令行模式下启用
             self::checkSession();
         }
         $class = new $methodInfo['class']();
         if (method_exists($class, $methodInfo['method'])) {
             $methodInfo['parameters'] = is_array($methodInfo['parameters']) ? $methodInfo['parameters'] : array();
             $result = true;
             if (method_exists($class, '__before')) {
                 $class->__before();
             }
             if (method_exists($class, '__output')) {
                 ob_start();
                 $result = call_user_func_array(array($class, $methodInfo['method']), $methodInfo['parameters']);
                 if (method_exists($class, '__after')) {
                     $class->__after($result);
                 }
                 $buffer = ob_get_contents();
                 @ob_end_clean();
                 $class->__output($buffer);
             } else {
                 $result = call_user_func_array(array($class, $methodInfo['method']), $methodInfo['parameters']);
                 if (method_exists($class, '__after')) {
                     $class->__after($result);
                 }
             }
             $result = null;
         } else {
             Fn::trigger404($methodInfo['class'] . ':' . $methodInfo['method'] . ' not found.');
         }
     } else {
         if ($system['debug']) {
             Fn::trigger404('file:' . $methodInfo['file'] . ' not found.');
         } else {
             Fn::trigger404();
         }
     }
 }
Exemplo n.º 2
0
 /**
  * 实例化一个模型
  * @param type $classname_path
  * @param type $hmvc_module_floder
  * @return type CoreModel
  */
 public static function instance($classname_path = null, $hmvc_module_floder = NULL)
 {
     if (!empty($hmvc_module_floder)) {
         CoreRouter::switchHmvcConfig($hmvc_module_floder);
     }
     //这里调用控制器instance是为了触发自动加载,从而避免了插件模式下,直接instance模型,自动加载失效的问题
     CoreController::instance();
     if (empty($classname_path)) {
         $renew = is_bool($classname_path) && $classname_path === true;
         CoreLoader::classAutoloadRegister();
         return empty(self::$instance) || $renew ? self::$instance = new self() : self::$instance;
     }
     $system = CoreLoader::$system;
     $classname_path = str_replace('.', DIRECTORY_SEPARATOR, $classname_path);
     $classname = basename($classname_path);
     $model_folders = $system['model_folder'];
     if (!is_array($model_folders)) {
         $model_folders = array($model_folders);
     }
     $count = count($model_folders);
     CoreLoader::classAutoloadRegister();
     foreach ($model_folders as $key => $model_folder) {
         $filepath = $model_folder . DIRECTORY_SEPARATOR . $classname_path . $system['model_file_suffix'];
         $alias_name = $classname;
         if (isset(CoreModelLoader::$model_files[$alias_name])) {
             return CoreModelLoader::$model_files[$alias_name];
         }
         if (file_exists($filepath)) {
             //在plugin模式下,路由器不再使用,那么自动注册不会被执行,自动加载功能会失效,所以在这里再尝试加载一次,
             //如此一来就能满足两种模式
             //CoreLoader::classAutoloadRegister();
             if (!class_exists($classname, FALSE)) {
                 CoreLoader::includeOnce($filepath);
             }
             if (class_exists($classname, FALSE)) {
                 return CoreModelLoader::$model_files[$alias_name] = new $classname();
             } else {
                 if ($key == $count - 1) {
                     Fn::trigger404('Model Class:' . $classname . ' not found.');
                 }
             }
         } else {
             if ($key == $count - 1) {
                 Fn::trigger404($filepath . ' not  found.');
             }
         }
     }
 }
Exemplo n.º 3
0
 /**
  * 实例化一个控制器
  * @staticvar array $loadedClasses
  * @param type $classname_path
  * @param type $hmvc_module_floder
  * @return CoreController
  */
 public static function instance($classname_path = null, $hmvc_module_floder = NULL)
 {
     if (!empty($hmvc_module_floder)) {
         CoreRouter::switchHmvcConfig($hmvc_module_floder);
     }
     if (empty($classname_path)) {
         CoreLoader::classAutoloadRegister();
         return new self();
     }
     $system = CoreLoader::$system;
     $classname_path = str_replace('.', DIRECTORY_SEPARATOR, $classname_path);
     $classname = basename($classname_path);
     $filepath = $system['controller_folder'] . DIRECTORY_SEPARATOR . $classname_path . $system['controller_file_suffix'];
     $alias_name = strtolower($classname);
     static $loadedClasses = array();
     if (isset($loadedClasses[$alias_name])) {
         return $loadedClasses[$alias_name];
     }
     if (file_exists($filepath)) {
         //在plugin模式下,路由器不再使用,那么自动注册不会被执行,自动加载功能会失效,所以在这里再尝试加载一次,
         //如此一来就能满足两种模式
         CoreLoader::classAutoloadRegister();
         CoreLoader::includeOnce($filepath);
         if (class_exists($classname, FALSE)) {
             return $loadedClasses[$alias_name] = new $classname();
         } else {
             Fn::trigger404('Ccontroller Class:' . $classname . ' not found.');
         }
     } else {
         Fn::trigger404($filepath . ' not found.');
     }
 }