123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374 |
- <?php namespace Maze\Routing;
- use Maze\Data\Model;
- use Maze\Http\Output;
- use Maze\Config\Project;
- use Maze\Config\Load as Config;
- class Load
- {
- /**
- * database 定义database目录
- *
- * @var string
- */
- const DATABASE = 'database/';
- /**
- * library 定义library目录
- *
- * @var string
- */
- const LIBRARY = 'src/';
- /**
- * NAME 定义开发的命名空间
- *
- * @var string
- */
- const NAME = 'MazeApp\\';
- /**
- * config
- *
- * @var array
- */
- protected $config;
- /**
- * class
- *
- * @var array
- */
- protected $class;
- /**
- * data
- *
- * @var array
- */
- protected $data;
- /**
- * param
- *
- * @var array
- */
- protected $param;
- /**
- * path project path 临时的项目路径
- *
- * @var string
- */
- protected $path;
- /**
- * attr
- *
- * @var string
- */
- protected $attr;
- /**
- * instance
- *
- * @var string
- */
- static protected $instance;
- /**
- * get
- *
- * @param string $method
- * @param array $param
- * @return \Maze\Routing\Loader
- */
- static public function get($method, $param = array())
- {
- if(empty(self::$instance))
- {
- self::$instance = new self();
- spl_autoload_register(array(self::$instance, 'autoload'));
- }
- return self::$instance->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);
- }
- }
|