67f568c90f2a14bd2c79622c33dbc7e165e65fbf.svn-base 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. <?php namespace Maze\Routing;
  2. use Maze\Data\Model;
  3. use Maze\Http\Output;
  4. use Maze\Config\Project;
  5. use Maze\Config\Load as Config;
  6. class Load
  7. {
  8. /**
  9. * database 定义database目录
  10. *
  11. * @var string
  12. */
  13. const DATABASE = 'database/';
  14. /**
  15. * library 定义library目录
  16. *
  17. * @var string
  18. */
  19. const LIBRARY = 'src/';
  20. /**
  21. * NAME 定义开发的命名空间
  22. *
  23. * @var string
  24. */
  25. const NAME = 'MazeApp\\';
  26. /**
  27. * config
  28. *
  29. * @var array
  30. */
  31. protected $config;
  32. /**
  33. * class
  34. *
  35. * @var array
  36. */
  37. protected $class;
  38. /**
  39. * data
  40. *
  41. * @var array
  42. */
  43. protected $data;
  44. /**
  45. * param
  46. *
  47. * @var array
  48. */
  49. protected $param;
  50. /**
  51. * path project path 临时的项目路径
  52. *
  53. * @var string
  54. */
  55. protected $path;
  56. /**
  57. * attr
  58. *
  59. * @var string
  60. */
  61. protected $attr;
  62. /**
  63. * instance
  64. *
  65. * @var string
  66. */
  67. static protected $instance;
  68. /**
  69. * get
  70. *
  71. * @param string $method
  72. * @param array $param
  73. * @return \Maze\Routing\Loader
  74. */
  75. static public function get($method, $param = array())
  76. {
  77. if(empty(self::$instance))
  78. {
  79. self::$instance = new self();
  80. spl_autoload_register(array(self::$instance, 'autoload'));
  81. }
  82. return self::$instance->load($method, $param);
  83. }
  84. /**
  85. * load file
  86. *
  87. * @param string $method
  88. * @param array $param
  89. * @return \Maze\Routing\Loader
  90. */
  91. static public function object($method, $param = array())
  92. {
  93. return (object) self::get($method, $param);
  94. }
  95. /**
  96. * autoload 该方法只能调取本项目里的类
  97. *
  98. * @return mixed
  99. */
  100. public function autoload($class)
  101. {
  102. # 以后这里可以加入方法和类引用记录
  103. if(strpos($class, self::NAME) !== false)
  104. {
  105. $class = str_replace('\\', '/', str_replace(self::NAME, '', $class));
  106. if($this->path)
  107. {
  108. $file = $this->path . self::LIBRARY . $class . '.php';
  109. }
  110. else
  111. {
  112. $file = MAZE_PROJECT_PATH . self::LIBRARY . $class . '.php';
  113. }
  114. if(is_file($file))
  115. {
  116. include($file);
  117. }
  118. else
  119. {
  120. Output::abert('file_exists', $file);
  121. }
  122. }
  123. else
  124. {
  125. //other include
  126. }
  127. }
  128. /**
  129. * setup 如果需要调取其他项目里的类,必须通过该方式
  130. *
  131. * @return mixed
  132. */
  133. private function load($key, $param = array())
  134. {
  135. $state = false;
  136. $attr = '';
  137. if(strpos($key, '#') !== false)
  138. {
  139. $temp = explode('#', $key);
  140. $key = $temp[0];
  141. $attr = $temp[1];
  142. }
  143. if(isset($this->param[$key]) && $this->param[$key] != $param)
  144. {
  145. $state = true;
  146. }
  147. $this->param[$key] = $param;
  148. if(empty($this->config[$key]))
  149. {
  150. $this->config($key);
  151. }
  152. if($state == true || empty($this->data[$key]))
  153. {
  154. $this->import(array('model','library'), $key);
  155. $this->path = false;
  156. }
  157. if($attr)
  158. {
  159. return $this->data[$key][$attr];
  160. }
  161. return $this->data[$key];
  162. }
  163. /**
  164. * config
  165. *
  166. * @return mixed
  167. */
  168. public function config($key)
  169. {
  170. $file = false;
  171. if(strpos($key, '/') !== false)
  172. {
  173. $temp = explode('/', $key);
  174. $method = $temp[1];
  175. $file = $temp[0];
  176. if(!$file && strpos($key, '-') !== false && isset(Config::$global['manage']['project']) && Config::$global['manage']['project'])
  177. {
  178. $file = Config::$global['manage']['project'];
  179. Config::$global['manage']['project'] = '';
  180. }
  181. }
  182. else
  183. {
  184. $method = $key;
  185. }
  186. $file = $file ? $file : MAZE_PROJECT_NAME;
  187. $project = Project::load($file);
  188. if($project)
  189. {
  190. $this->path = $project['path'];
  191. $file = $project['name'];
  192. }
  193. else
  194. {
  195. Output::abert('project_exists', $file);
  196. }
  197. $this->config[$key] = array
  198. (
  199. 'method' => $method,
  200. 'file' => isset($project['lib']) ? $project['lib'] : $file,
  201. 'path' => $this->path,
  202. );
  203. }
  204. /**
  205. * model 载入数据库操作
  206. *
  207. * @return string
  208. */
  209. private function import($method, $key)
  210. {
  211. $this->data[$key] = false;
  212. # 以后这里可以加入方法和类引用记录
  213. foreach($method as $k => $v)
  214. {
  215. $this->$v($key);
  216. }
  217. }
  218. /**
  219. * model 载入数据库操作
  220. *
  221. * @return string
  222. */
  223. private function model($key)
  224. {
  225. if(strpos($this->config[$key]['method'], '-') !== false)
  226. {
  227. $method = explode('-', $this->config[$key]['method']);
  228. $class = $this->config[$key]['file'] . '-' . $method[0];
  229. if(empty($this->class[$class]))
  230. {
  231. $file = $this->config[$key]['path'] . self::DATABASE . $method[0] . '.php';
  232. if(!is_file($file))
  233. {
  234. Output::abert('file_exists', $file);
  235. }
  236. $config = $this->file($file);
  237. if(isset($config['auth']))
  238. {
  239. if(is_string($config['auth']))
  240. {
  241. $config['auth_key'] = $config['auth'];
  242. $config['auth'] = $this->config[$key]['file'] . '-' . $config['auth'];
  243. }
  244. elseif(is_array($config['auth']))
  245. {
  246. $config['auth']['key'] = $this->config[$key]['file'] . '-' . $config['auth']['key'];
  247. }
  248. }
  249. $config['project'] = $this->config[$key]['file'];
  250. $this->class[$class] = new Model($config);
  251. }
  252. if(isset($method[1]) && $method[1])
  253. {
  254. //$this->api($this->config[$key]['path'], $method[0] . '-' . $method[1]);
  255. if(isset($this->param[$key]) && $this->param[$key])
  256. {
  257. $this->data[$key] = $this->class[$class]->method($method[1], $this->param[$key]);
  258. }
  259. else
  260. {
  261. $this->data[$key] = $this->class[$class]->method($method[1]);
  262. }
  263. }
  264. }
  265. }
  266. /**
  267. * library 载入基础类库
  268. *
  269. * @return string
  270. */
  271. private function library($key)
  272. {
  273. if(strpos($this->config[$key]['method'], '.') !== false)
  274. {
  275. $method = explode('.', $this->config[$key]['method']);
  276. $class = ucfirst($this->config[$key]['file']) . '\\' . ucfirst($method[0]);
  277. if(empty($this->class[$class]))
  278. {
  279. $file = $this->config[$key]['path'] . self::LIBRARY . str_replace('\\', '/', $class) . '.php';
  280. if(!is_file($file))
  281. {
  282. Output::abert('file_exists', $file);
  283. }
  284. require $file;
  285. $className = self::NAME . $class;
  286. $this->data[$key] = $this->class[$class] = new $className();
  287. }
  288. if(isset($method[1]) && $method[1])
  289. {
  290. //$this->api($this->config[$key]['path'], $method[0] . '.' . $method[1]);
  291. if(isset($this->class[$class]) && $this->class[$class] && !method_exists($this->class[$class], $method[1]))
  292. {
  293. //Output::abert('method_exists', array($class, $method[1]));
  294. }
  295. if($this->param && isset($this->param[$key]))
  296. {
  297. $this->data[$key] = $this->class[$class]->$method[1]($this->param[$key]);
  298. }
  299. else
  300. {
  301. $this->data[$key] = $this->class[$class]->$method[1]();
  302. }
  303. }
  304. else
  305. {
  306. $this->data[$key] = $this->data[$key] ? $this->data[$key] : $this->class[$class];
  307. }
  308. }
  309. }
  310. /**
  311. * 载入文件
  312. *
  313. * @return string
  314. */
  315. private function file($file)
  316. {
  317. return include($file);
  318. }
  319. }