1 <?php
2 3 4 5 6 7
8
9 namespace Cross\MVC;
10
11 use Cross\Cache\Driver\RedisDriver;
12 use Cross\Exception\CoreException;
13 use Cross\DB\Drivers\PDOSqlDriver;
14 use Cross\DB\Drivers\CouchDriver;
15 use Cross\DB\Drivers\MongoDriver;
16 use Cross\DB\DBFactory;
17 use Cross\Core\FrameBase;
18 use Cross\Core\Config;
19 use Cross\Http\Response;
20 use Cross\Http\Request;
21
22 23 24 25 26 27
28 class Module extends FrameBase
29 {
30 31 32 33 34
35 private $linkName;
36
37 38 39 40 41
42 private $linkType;
43
44 45 46 47 48
49 private $linkConfig;
50
51 52 53 54 55 56 57 58 59
60 protected $db_config_file;
61
62 63 64 65 66 67
68 function __construct($params = '')
69 {
70 parent::__construct();
71
72 $config = $this->parseModelParams($params);
73 $this->linkName = &$config['model_name'];
74 $this->linkType = &$config['model_type'];
75 $this->linkConfig = &$config['model_config'];
76 }
77
78 79 80 81 82 83 84 85
86 function getModel($params = '', &$config = array())
87 {
88 static $cache = array();
89 if (!isset($cache[$params])) {
90 $config = $this->parseModelParams($params);
91 $model = DBFactory::make($config['model_type'], $config['model_config'], array($this->getConfig()));
92 $cache[$params] = array('config' => $config, 'model' => $model);
93 } else {
94 $model = $cache[$params]['model'];
95 $config = $cache[$params]['config'];
96 }
97
98 return $model;
99 }
100
101 102 103 104 105
106 function getLinkName()
107 {
108 return $this->linkName;
109 }
110
111 112 113 114 115
116 function getLinkType()
117 {
118 return $this->linkType;
119 }
120
121 122 123 124 125
126 function getLinkConfig()
127 {
128 return $this->linkConfig;
129 }
130
131 132 133 134 135 136
137 function getPrefix($table = '')
138 {
139 return $this->link->getPrefix() . $table;
140 }
141
142 143 144 145 146 147
148 protected function databaseConfig()
149 {
150 static $database_config = null;
151 if (null === $database_config) {
152 $database_config = parent::loadConfig($this->getModuleConfigFile());
153 }
154
155 return $database_config;
156 }
157
158 159 160 161 162
163 protected function setDatabaseConfigFile($link_config_file)
164 {
165 $this->db_config_file = $link_config_file;
166 }
167
168 169 170 171 172 173 174
175 protected function parseModelParams($params = '')
176 {
177 $db_config_params = '';
178 if ($params) {
179 $db_config_params = $params;
180 } else {
181 static $default_db_config = '';
182 if ($default_db_config === '') {
183 $default_db_config = $this->getConfig()->get('sys', 'default_db');
184 }
185
186 if ($default_db_config) {
187 $db_config_params = $default_db_config;
188 }
189 }
190
191 if ($db_config_params) {
192 if (strpos($db_config_params, ':') === false) {
193 throw new CoreException("数据库参数配置格式不正确: {$db_config_params}");
194 }
195
196 list($model_type, $model_name) = explode(':', $db_config_params);
197 } else {
198 $model_name = 'db';
199 $model_type = 'mysql';
200 }
201
202 static $cache;
203 if (!isset($cache[$model_type][$model_name])) {
204 $databaseConfig = $this->databaseConfig();
205 $model_config = $databaseConfig->get($model_type, $model_name);
206 if (empty($model_config)) {
207 throw new CoreException("未配置的Model: {$model_type}:{$model_name}");
208 }
209
210 $cache[$model_type][$model_name] = array(
211 'model_name' => $model_name,
212 'model_type' => $model_type,
213 'model_config' => $model_config,
214 );
215 }
216
217 return $cache[$model_type][$model_name];
218 }
219
220 221 222 223 224 225
226 private function getLink()
227 {
228 return DBFactory::make($this->linkType, $this->linkConfig, array($this->getConfig()));
229 }
230
231 232 233 234 235
236 private function getModuleConfigFile()
237 {
238 if (!$this->db_config_file) {
239 $db_config_file = $this->getConfig()->get('sys', 'db_config');
240 if (!$db_config_file) {
241 $db_config_file = 'db.config.php';
242 }
243
244 $this->setDatabaseConfigFile($db_config_file);
245 }
246
247 return $this->db_config_file;
248 }
249
250 251 252 253 254 255 256
257 function __get($property)
258 {
259 switch ($property) {
260 case 'link' :
261 return $this->link = $this->getLink();
262
263 default :
264 return parent::__get($property);
265 }
266 }
267 }
268