public static function &loadClass($class, $directory = '', $param = null, $isCache = true) { if (isset(self::$_classes[$class])) { $class = self::$_classes[$class]; if (is_subclass_of($class, 'CoreController')) { CoreController::$instance = $class; } return $class; } $name = false; foreach (array(APPPATH, BASEPATH) as $path) { if (file_exists($path . $directory . '/' . $class . '.php')) { $name = $class; if (class_exists($name, FALSE) === FALSE) { require_once $path . $directory . '/' . $class . '.php'; } break; } } if ($name === FALSE) { die('Unable to locate the specified class: ' . $class . '.php'); } else { $class = str_replace('.php', '', trim($class, '/')); if (($lastSlash = strrpos($class, '/')) !== FALSE) { $name = substr($class, ++$lastSlash); } } $instance = isset($param) ? new $name($param) : new $name(); if ($isCache) { self::$_classes[$class] = $instance; } return $instance; }
public function __construct() { Debug::write("Building controller " . get_class($this) . " ...", 0); self::$instance = $this; $this->view = new View(); //TODO: move check to Database.class.php if (isset($GLOBALS['config']['bdd']['hostname']) && !empty($GLOBALS['config']['bdd']['hostname'])) { $this->db = new Database(); } /** * start capturating controller's output */ self::startCapturing(); }
/** * 实例化一个模型 * @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.'); } } } }
public function __construct($autoload = true, $init_template = true) { // Add trace for debugging \Debug::trace('Initializing core controller...', __FILE__, __LINE__); // Set the instance here if (self::$instance == false) { self::$instance = $this; } // Set our Controller and Action $this->controller = $GLOBALS['controller']; $this->action = $GLOBALS['action']; $this->querystring = $GLOBALS['querystring']; // Initiate the Loader Input, and Config class $this->load = load_class('Loader'); $this->Config = load_class('Config'); $this->Input = load_class('Input'); // If site is updating, only allow Ajax requests if ($GLOBALS['controller'] != 'admin_ajax' && $this->Config->get('site_updating')) { die('Site Down for maintenance. Be back soon.'); } // Setup the selected users language $this->Language = load_class('Language'); $GLOBALS['language'] = $this->Language->selected_language(); // Autoload helpers and libraries if ($autoload == true) { $this->_autoload(); } // Setup the template system if ($init_template == true) { $this->_init_template(); } // Process DB updates if (!$this->Input->is_ajax()) { $this->_process_db(); } // Add trace for debugging \Debug::trace('Core controller initialized successfully', __FILE__, __LINE__); }
/** * 实例化一个loader * @param type $renew 是否强制重新new一个loader,默认只会new一次 * @param type $hmvc_module_floder hmvc模块文件夹名称 * @return type CoreLoader */ public static function instance($renew = null, $hmvc_module_floder = null) { $default = CoreLoader::$system; if (!empty($hmvc_module_floder)) { CoreRouter::switchHmvcConfig($hmvc_module_floder); } //在plugin模式下,路由器不再使用,那么自动注册不会被执行,自动加载功能会失效,所以在这里再尝试加载一次, //如此一来就能满足两种模式 self::classAutoloadRegister(); //这里调用控制器instance是为了触发自动加载,从而避免了插件模式下,直接instance模型,自动加载失效的问题 CoreController::instance(); $renew = is_bool($renew) && $renew === true; $ret = empty(self::$instance) || $renew ? self::$instance = new self() : self::$instance; CoreLoader::$system = $default; return $ret; }