123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391 |
- <?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
- *
- * @var string
- */
- protected $path;
- /**
- * attr
- *
- * @var string
- */
- protected $attr;
- /**
- * load
- *
- * @var array
- */
- protected $load;
- /**
- * 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
- */
- private function autoload($class)
- {
- $temp = explode('\\', $class);
- if(strpos($class, self::NAME) !== false && isset($temp[1]))
- {
- $project = Project::load(strtolower($temp[1]));
- if($project)
- {
- $file = $project['path'] . self::LIBRARY . str_replace($temp[0] . '/' . $temp[1] . '/', '', str_replace('\\', '/', $class)) . '.php';
- if(is_file($file))
- {
- $this->file($file);
- }
- else
- {
- Output::abert('file_exists', $file);
- }
- }
- }
- }
- /**
- * 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;
- $namespace = false;
- if(strpos($key, '/') !== false)
- {
- $temp = explode('/', $key);
- $method = $temp[1];
- $file = $temp[0];
- if($file && strpos($file, '.') !== false)
- {
- $temp = explode('.', $file);
- $file = $temp[0];
- $namespace = $temp[1];
-
- # 处理后台和公开的命名空间
- if($namespace == 'manage')
- {
- self::get('manage/auth.init');
- }
- elseif($namespace != 'public')
- {
- return;
- }
- }
- elseif(!$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 = isset($project['lib']) ? $project['lib'] : $project['name'];
- }
- else
- {
- Output::abert('project_exists', $file);
- }
- $this->config[$key] = array
- (
- 'method' => $method,
- 'file' => $file,
- 'path' => $this->path,
- 'namespace' => $namespace
- );
-
- }
- /**
- * 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]['namespace'] ? $this->config[$key]['namespace'] : $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);
- }
- }
|