123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288 |
- <?php namespace Maze\Config;
- use Maze\Http\Output;
- use Maze\File\Path;
- class Database
- {
- /**
- * read file
- *
- * @var string
- */
- const PATH = 'database/';
- /**
- * content
- *
- * @var array
- */
- static private $config;
- /**
- * content
- *
- * @return mixed
- */
- static public function get($file)
- {
- $file = $file;
- $key = md5($file);
- if(isset(self::$config[$key]))
- {
- return self::$config[$key];
- }
- if(!is_file($file))
- {
- Output::abert('file_exists', $file);
- }
- /*
- $temp = Path::create(MAZE_PATH . 'data/database/temp/', $key);
- if(is_file($temp) && filemtime($file) < filemtime($temp))
- {
- return self::$config[$key] = include($temp);
- }
- */
- self::$config[$key] = self::handle($file);
-
- //print_r(self::$config[$key]);
- //file_put_contents($temp, '<?php return ' . var_export(self::$config[$key], true) . ';');
- return self::$config[$key];
- }
-
- /**
- * register project file
- *
- * @return mixed
- */
- static private function handle($file)
- {
- $config = include($file);
- if(isset($config['struct']) && is_array($config['struct']['id']))
- {
- //$config['request'] = array();
- if(empty($config['request']['one'])) $config['request']['one']['type'] = 'one';
- $config['request']['insert']['type'] = 'insert';
- $config['request']['update']['type'] = 'update';
- $config['request']['delete']['type'] = 'delete';
- foreach($config['struct'] as $k => $v)
- {
- if(isset($v['match']))
- {
- # 这里增加value,用以区分update/select的值
- if(is_array($v['match']))
- {
- $v['value'] = $v['match'][1];
- $v['match'] = $v['match'][0];
- }
- else
- {
- $v['value'] = $v['match'];
- }
-
- if($k == 'id')
- {
- $config['request']['delete']['where'][$k] = $v['match'];
- if(isset($config['request']['one'])) $config['request']['one']['where'][$k] = $v['match'];
- $config['request']['update']['where'][$k] = $v['match'];
- }
- //elseif(isset($v['update']) || isset($v['insert']))
- elseif(!isset($v['table']))
- {
- $config['request']['insert']['add'][$k] = $v['value'];
- if(empty($v['insert'])) $config['request']['update']['set'][$k] = $v['value'];
- }
- }
-
- if(isset($v['table']) && !isset($config['end']))
- {
- $config['end']['insert'] = $v['value'];
- }
- if(isset($v['list']))
- {
- $config['manage']['list'][$k]['col'] = $k;
- $config['manage']['list'][$k]['name'] = $v['name'];
- if(is_string($v['list']))
- {
- $config['manage']['list'][$k]['value'] = $v['list'];
- }
- if(isset($v['option']))
- {
- $config['manage']['list'][$k]['option'] =& $v['option'];
- }
- if(isset($v['modal']))
- {
- $config['manage']['list'][$k]['modal'] = $v['modal'];
- }
- if(isset($v['edit']))
- {
- $config['manage']['list'][$k]['edit'] = $v['edit'];
- }
- }
-
- if(empty($config['request']['list']))
- {
- $config['request']['list']['type'] = 'all';
- $config['request']['list']['order'] = array('id', 'desc');
- $config['request']['list']['page'] = array(15, 'list');
- $config['request']['list']['col'] = '*|id';
- $config['request']['list']['option'] = array();
- }
- if(isset($v['match']))
- {
- $config['request']['list']['option'][$k] = $v['match'];
- }
-
- if(isset($v['order']))
- {
- if(isset($config['request']['list']['order']))
- {
- if($config['request']['list']['order'][0] && $config['request']['list']['order'][0] != $k)
- {
- $config['request']['list']['order'][0] = $k . '` '.(is_string($v['order']) ? $v['order'] : 'desc').',`' . $config['request']['list']['order'][0];
- }
- }
- }
- if(isset($v['search']))
- {
- if(strpos($v['search'], 'order') !== false)
- {
- $config['manage']['search']['order'][$k] = $v['name'];
- }
- if(strpos($v['search'], 'fulltext') !== false)
- {
- $config['manage']['search']['fulltext'][$k] = $v['name'];
- isset($v['match']) && $config['request']['list']['option'][$k] = array('option', 'like');
- }
- if(isset($v['option']) && strpos($v['search'], 'select') !== false)
- {
- $config['manage']['search']['select'][$k]['option'] = $v['option'];
- $config['manage']['search']['select'][$k]['lang'] = $v['name'];
- if(isset($v['default']))
- {
- $config['manage']['search']['select'][$k]['default'] = $v['default'];
- }
- }
- }
- if(isset($v['update']))
- {
- $config['manage']['update'][$k]['col'] = $k;
- $config['manage']['update'][$k]['name'] = $v['name'];
- if(isset($v['desc']))
- {
- $config['manage']['update'][$k]['desc'] = $v['desc'];
- }
- if(isset($v['default']))
- {
- $config['manage']['update'][$k]['default'] = $v['default'];
- }
- if(isset($v['option']))
- {
- $config['manage']['update'][$k]['option'] =& $v['option'];
- }
- if(isset($v['child']))
- {
- $config['manage']['update'][$k]['child'] = $v['child'];
- }
- if(isset($v['child_name']))
- {
- $config['manage']['update'][$k]['child_name'] = $v['child_name'];
- }
- if(isset($v['key']))
- {
- $config['manage']['update'][$k]['key'] = $v['key'];
- }
- if(isset($v['place']))
- {
- $config['manage']['update'][$k]['place'] = $v['place'];
- }
- if(isset($v['table']))
- {
- $config['manage']['update'][$k]['table'] = $v['table'];
- }
- if(isset($v['show']))
- {
- $config['manage']['update'][$k]['show'] = $v['show'];
- }
- if(isset($v['autocomplete']))
- {
- $config['manage']['update'][$k]['autocomplete'] = $v['autocomplete'];
- }
- if(isset($v['match']) && $v['match'] != 'option')
- {
- if(is_string($v['match']) && strpos($v['match'], '/') !== false)
- {
- $config['manage']['update'][$k]['valid'] = 'validate[required,custom['.$k.']]';
- }
- else
- {
- $config['manage']['update'][$k]['valid'] = 'validate[required]';
- }
- }
- if($v['update'])
- {
- $config['manage']['update'][$k]['type'] = $v['update'];
- }
- else
- {
- $config['manage']['update'][$k]['type'] = 'text';
- }
- }
-
- if(isset($v['update']) && $v['update'] == 'radio' && (is_array($v['option']) || is_object($v['option'])))
- {
- $config['request']['update' . $k]['type'] = 'update';
- $config['request']['update' . $k]['set'][$k] = $v['value'];
- $config['request']['update' . $k]['where']['id'] = 'yes';
- }
- }
- if(isset($config['manage']['list']) && is_array($config['manage']['list']))
- {
- $config['manage']['list']['_manage'] = array
- (
- 'col' => 'manage',
- 'name' => '管理',
- 'value' => array(),
- );
-
- if(isset($config['manage']['update']) && !isset($config['manage']['edit']))
- {
- # 3和6 对应database类里的配置
- $config['manage']['list']['_manage']['value'] = array(3 => '编辑');
- }
- if(isset($config['manage']['list'][0]))
- {
- $config['manage']['list']['_manage']['value'] += $config['manage']['list'][0];
- unset($config['manage']['list'][0]);
- }
- elseif(isset($config['manage']['list_button']))
- {
- $config['manage']['list']['_manage']['value'] += $config['manage']['list_button'];
- }
-
- if(!$config['manage']['list']['_manage']['value'])
- {
- unset($config['manage']['list']['_manage']);
- }
- }
- }
-
- return $config;
- }
- }
|