1c5396f4d298e59b8b20ad1e9dc5b280642d73b8.svn-base 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. <?php namespace Maze\Config;
  2. use Maze\Http\Output;
  3. use Maze\File\Path;
  4. class Database
  5. {
  6. /**
  7. * read file
  8. *
  9. * @var string
  10. */
  11. const PATH = 'database/';
  12. /**
  13. * content
  14. *
  15. * @var array
  16. */
  17. static private $config;
  18. /**
  19. * content
  20. *
  21. * @return mixed
  22. */
  23. static public function get($file)
  24. {
  25. $file = $file;
  26. $key = md5($file);
  27. if(isset(self::$config[$key]))
  28. {
  29. return self::$config[$key];
  30. }
  31. if(!is_file($file))
  32. {
  33. Output::abert('file_exists', $file);
  34. }
  35. /*
  36. $temp = Path::create(MAZE_PATH . 'data/database/temp/', $key);
  37. if(is_file($temp) && filemtime($file) < filemtime($temp))
  38. {
  39. return self::$config[$key] = include($temp);
  40. }
  41. */
  42. self::$config[$key] = self::handle($file);
  43. //print_r(self::$config[$key]);
  44. //file_put_contents($temp, '<?php return ' . var_export(self::$config[$key], true) . ';');
  45. return self::$config[$key];
  46. }
  47. /**
  48. * register project file
  49. *
  50. * @return mixed
  51. */
  52. static private function handle($file)
  53. {
  54. $config = include($file);
  55. if(isset($config['struct']) && is_array($config['struct']['id']))
  56. {
  57. //$config['request'] = array();
  58. if(empty($config['request']['one'])) $config['request']['one']['type'] = 'one';
  59. $config['request']['insert']['type'] = 'insert';
  60. $config['request']['update']['type'] = 'update';
  61. $config['request']['delete']['type'] = 'delete';
  62. foreach($config['struct'] as $k => $v)
  63. {
  64. if(isset($v['match']))
  65. {
  66. # 这里增加value,用以区分update/select的值
  67. if(is_array($v['match']))
  68. {
  69. $v['value'] = $v['match'][1];
  70. $v['match'] = $v['match'][0];
  71. }
  72. else
  73. {
  74. $v['value'] = $v['match'];
  75. }
  76. if($k == 'id')
  77. {
  78. $config['request']['delete']['where'][$k] = $v['match'];
  79. if(isset($config['request']['one'])) $config['request']['one']['where'][$k] = $v['match'];
  80. $config['request']['update']['where'][$k] = $v['match'];
  81. }
  82. //elseif(isset($v['update']) || isset($v['insert']))
  83. elseif(!isset($v['table']))
  84. {
  85. $config['request']['insert']['add'][$k] = $v['value'];
  86. if(empty($v['insert'])) $config['request']['update']['set'][$k] = $v['value'];
  87. }
  88. }
  89. if(isset($v['table']) && !isset($config['end']))
  90. {
  91. $config['end']['insert'] = $v['value'];
  92. }
  93. if(isset($v['list']))
  94. {
  95. $config['manage']['list'][$k]['col'] = $k;
  96. $config['manage']['list'][$k]['name'] = $v['name'];
  97. if(is_string($v['list']))
  98. {
  99. $config['manage']['list'][$k]['value'] = $v['list'];
  100. }
  101. if(isset($v['option']))
  102. {
  103. $config['manage']['list'][$k]['option'] =& $v['option'];
  104. }
  105. if(isset($v['modal']))
  106. {
  107. $config['manage']['list'][$k]['modal'] = $v['modal'];
  108. }
  109. if(isset($v['edit']))
  110. {
  111. $config['manage']['list'][$k]['edit'] = $v['edit'];
  112. }
  113. }
  114. if(empty($config['request']['list']))
  115. {
  116. $config['request']['list']['type'] = 'all';
  117. $config['request']['list']['order'] = array('id', 'desc');
  118. $config['request']['list']['page'] = array(15, 'list');
  119. $config['request']['list']['col'] = '*|id';
  120. $config['request']['list']['option'] = array();
  121. }
  122. if(isset($v['match']))
  123. {
  124. $config['request']['list']['option'][$k] = $v['match'];
  125. }
  126. if(isset($v['order']))
  127. {
  128. if(isset($config['request']['list']['order']))
  129. {
  130. if($config['request']['list']['order'][0] && $config['request']['list']['order'][0] != $k)
  131. {
  132. $config['request']['list']['order'][0] = $k . '` '.(is_string($v['order']) ? $v['order'] : 'desc').',`' . $config['request']['list']['order'][0];
  133. }
  134. }
  135. }
  136. if(isset($v['search']))
  137. {
  138. if(strpos($v['search'], 'order') !== false)
  139. {
  140. $config['manage']['search']['order'][$k] = $v['name'];
  141. }
  142. if(strpos($v['search'], 'fulltext') !== false)
  143. {
  144. $config['manage']['search']['fulltext'][$k] = $v['name'];
  145. isset($v['match']) && $config['request']['list']['option'][$k] = array('option', 'like');
  146. }
  147. if(isset($v['option']) && strpos($v['search'], 'select') !== false)
  148. {
  149. $config['manage']['search']['select'][$k]['option'] = $v['option'];
  150. $config['manage']['search']['select'][$k]['lang'] = $v['name'];
  151. if(isset($v['default']))
  152. {
  153. $config['manage']['search']['select'][$k]['default'] = $v['default'];
  154. }
  155. }
  156. }
  157. if(isset($v['update']))
  158. {
  159. $config['manage']['update'][$k]['col'] = $k;
  160. $config['manage']['update'][$k]['name'] = $v['name'];
  161. if(isset($v['desc']))
  162. {
  163. $config['manage']['update'][$k]['desc'] = $v['desc'];
  164. }
  165. if(isset($v['default']))
  166. {
  167. $config['manage']['update'][$k]['default'] = $v['default'];
  168. }
  169. if(isset($v['option']))
  170. {
  171. $config['manage']['update'][$k]['option'] =& $v['option'];
  172. }
  173. if(isset($v['child']))
  174. {
  175. $config['manage']['update'][$k]['child'] = $v['child'];
  176. }
  177. if(isset($v['child_name']))
  178. {
  179. $config['manage']['update'][$k]['child_name'] = $v['child_name'];
  180. }
  181. if(isset($v['key']))
  182. {
  183. $config['manage']['update'][$k]['key'] = $v['key'];
  184. }
  185. if(isset($v['place']))
  186. {
  187. $config['manage']['update'][$k]['place'] = $v['place'];
  188. }
  189. if(isset($v['table']))
  190. {
  191. $config['manage']['update'][$k]['table'] = $v['table'];
  192. }
  193. if(isset($v['show']))
  194. {
  195. $config['manage']['update'][$k]['show'] = $v['show'];
  196. }
  197. if(isset($v['autocomplete']))
  198. {
  199. $config['manage']['update'][$k]['autocomplete'] = $v['autocomplete'];
  200. }
  201. if(isset($v['match']) && $v['match'] != 'option')
  202. {
  203. if(is_string($v['match']) && strpos($v['match'], '/') !== false)
  204. {
  205. $config['manage']['update'][$k]['valid'] = 'validate[required,custom['.$k.']]';
  206. }
  207. else
  208. {
  209. $config['manage']['update'][$k]['valid'] = 'validate[required]';
  210. }
  211. }
  212. if($v['update'])
  213. {
  214. $config['manage']['update'][$k]['type'] = $v['update'];
  215. }
  216. else
  217. {
  218. $config['manage']['update'][$k]['type'] = 'text';
  219. }
  220. }
  221. if(isset($v['update']) && $v['update'] == 'radio' && (is_array($v['option']) || is_object($v['option'])))
  222. {
  223. $config['request']['update' . $k]['type'] = 'update';
  224. $config['request']['update' . $k]['set'][$k] = $v['value'];
  225. $config['request']['update' . $k]['where']['id'] = 'yes';
  226. }
  227. }
  228. if(isset($config['manage']['list']) && is_array($config['manage']['list']))
  229. {
  230. $config['manage']['list']['_manage'] = array
  231. (
  232. 'col' => 'manage',
  233. 'name' => '管理',
  234. 'value' => array(),
  235. );
  236. if(isset($config['manage']['update']) && !isset($config['manage']['edit']))
  237. {
  238. # 3和6 对应database类里的配置
  239. $config['manage']['list']['_manage']['value'] = array(3 => '编辑');
  240. }
  241. if(isset($config['manage']['list'][0]))
  242. {
  243. $config['manage']['list']['_manage']['value'] += $config['manage']['list'][0];
  244. unset($config['manage']['list'][0]);
  245. }
  246. elseif(isset($config['manage']['list_button']))
  247. {
  248. $config['manage']['list']['_manage']['value'] += $config['manage']['list_button'];
  249. }
  250. if(!$config['manage']['list']['_manage']['value'])
  251. {
  252. unset($config['manage']['list']['_manage']);
  253. }
  254. }
  255. }
  256. return $config;
  257. }
  258. }