load($method, $param); } /** * load file * * @param string $method * @param array $param * @return \Maze\Routing\Loader */ static public function object($method, $param = array()) { return (object) self::get($method, $param); } /** * autoload 该方法只能调取本项目里的类 * * @return mixed */ public function autoload($class) { # 以后这里可以加入方法和类引用记录 if(strpos($class, self::NAME) !== false) { $class = str_replace('\\', '/', str_replace(self::NAME, '', $class)); if($this->path) { $file = $this->path . self::LIBRARY . $class . '.php'; } else { $file = MAZE_PROJECT_PATH . self::LIBRARY . $class . '.php'; } if(is_file($file)) { include($file); } else { Output::abert('file_exists', $file); } } else { //other include } } /** * setup 如果需要调取其他项目里的类,必须通过该方式 * * @return mixed */ private function load($key, $param = array()) { $state = false; $attr = ''; if(strpos($key, '#') !== false) { $temp = explode('#', $key); $key = $temp[0]; $attr = $temp[1]; } if(isset($this->param[$key]) && $this->param[$key] != $param) { $state = true; } $this->param[$key] = $param; if(empty($this->config[$key])) { $this->config($key); } if($state == true || empty($this->data[$key])) { $this->import(array('model','library'), $key); $this->path = false; } if($attr) { return $this->data[$key][$attr]; } return $this->data[$key]; } /** * config * * @return mixed */ public function config($key) { $file = false; if(strpos($key, '/') !== false) { $temp = explode('/', $key); $method = $temp[1]; $file = $temp[0]; if(!$file && strpos($key, '-') !== false && isset(Config::$global['manage']['project']) && Config::$global['manage']['project']) { $file = Config::$global['manage']['project']; Config::$global['manage']['project'] = ''; } } else { $method = $key; } $file = $file ? $file : MAZE_PROJECT_NAME; $project = Project::load($file); if($project) { $this->path = $project['path']; $file = $project['name']; } else { Output::abert('project_exists', $file); } $this->config[$key] = array ( 'method' => $method, 'file' => isset($project['lib']) ? $project['lib'] : $file, 'path' => $this->path, ); } /** * model 载入数据库操作 * * @return string */ private function import($method, $key) { $this->data[$key] = false; # 以后这里可以加入方法和类引用记录 foreach($method as $k => $v) { $this->$v($key); } } /** * model 载入数据库操作 * * @return string */ private function model($key) { if(strpos($this->config[$key]['method'], '-') !== false) { $method = explode('-', $this->config[$key]['method']); $class = $this->config[$key]['file'] . '-' . $method[0]; if(empty($this->class[$class])) { $file = $this->config[$key]['path'] . self::DATABASE . $method[0] . '.php'; if(!is_file($file)) { Output::abert('file_exists', $file); } $config = $this->file($file); if(isset($config['auth'])) { if(is_string($config['auth'])) { $config['auth_key'] = $config['auth']; $config['auth'] = $this->config[$key]['file'] . '-' . $config['auth']; } elseif(is_array($config['auth'])) { $config['auth']['key'] = $this->config[$key]['file'] . '-' . $config['auth']['key']; } } $config['project'] = $this->config[$key]['file']; $this->class[$class] = new Model($config); } if(isset($method[1]) && $method[1]) { //$this->api($this->config[$key]['path'], $method[0] . '-' . $method[1]); if(isset($this->param[$key]) && $this->param[$key]) { $this->data[$key] = $this->class[$class]->method($method[1], $this->param[$key]); } else { $this->data[$key] = $this->class[$class]->method($method[1]); } } } } /** * library 载入基础类库 * * @return string */ private function library($key) { if(strpos($this->config[$key]['method'], '.') !== false) { $method = explode('.', $this->config[$key]['method']); $class = ucfirst($this->config[$key]['file']) . '\\' . ucfirst($method[0]); if(empty($this->class[$class])) { $file = $this->config[$key]['path'] . self::LIBRARY . str_replace('\\', '/', $class) . '.php'; if(!is_file($file)) { Output::abert('file_exists', $file); } require $file; $className = self::NAME . $class; $this->data[$key] = $this->class[$class] = new $className(); } if(isset($method[1]) && $method[1]) { //$this->api($this->config[$key]['path'], $method[0] . '.' . $method[1]); if(isset($this->class[$class]) && $this->class[$class] && !method_exists($this->class[$class], $method[1])) { //Output::abert('method_exists', array($class, $method[1])); } if($this->param && isset($this->param[$key])) { $this->data[$key] = $this->class[$class]->$method[1]($this->param[$key]); } else { $this->data[$key] = $this->class[$class]->$method[1](); } } else { $this->data[$key] = $this->data[$key] ? $this->data[$key] : $this->class[$class]; } } } /** * 载入文件 * * @return string */ private function file($file) { return include($file); } }