adcb26643c050174387482e9e529ee0a71c92942.svn-base 8.7 KB

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