1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
class Db extends Module { |
4
|
|
|
|
5
|
|
|
public $connection = null; |
6
|
|
|
public $connect = false; |
7
|
|
|
public $dbConfig = []; |
8
|
|
|
public $curQuery = null; |
9
|
|
|
public $className = ''; |
10
|
|
|
public $QueryClassName = ''; |
11
|
|
|
public $ResultClassName = ''; |
12
|
|
|
public $migrationsVersions = []; |
13
|
|
|
|
14
|
|
|
public function init($param = null) { |
15
|
|
|
if (!$param) { |
16
|
|
|
$param = isset($this->config['default']) ? $this->config['default'] : 'local'; |
17
|
|
|
} |
18
|
|
|
if (!is_array($param)) { |
19
|
|
|
if (!($dbOption = Db\Options::get($param, 'connect_alias', ['array' => true]))) { |
20
|
|
|
return false; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
$db = $dbOption; |
24
|
|
|
} else { |
25
|
|
|
$db = $param; |
26
|
|
|
} |
27
|
|
|
$className = 'Db\\' . $db['driver']; |
28
|
|
|
$this->connection = new $className(); |
29
|
|
|
$this->connection->init($db); |
30
|
|
|
$this->connection->dbInstance = $this; |
31
|
|
|
$this->connect = $this->connection->connect; |
32
|
|
|
$this->dbConfig = $db; |
33
|
|
|
|
34
|
|
|
$this->className = 'Db\\' . $this->dbConfig['driver']; |
35
|
|
|
$this->QueryClassName = 'Db\\' . $this->dbConfig['driver'] . '\\Query'; |
36
|
|
|
$this->ResultClassName = 'Db\\' . $this->dbConfig['driver'] . '\\Result'; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function loadMigrationsVersion($code) { |
40
|
|
|
if (!$this->connect) { |
41
|
|
|
return false; |
42
|
|
|
} |
43
|
|
|
$version = Db\Migration::getList(['where' => ['code', $code], 'order' => ['date_create', 'desc'], 'limit' => 1, 'key' => false]); |
44
|
|
|
if ($version) { |
|
|
|
|
45
|
|
|
$this->migrationsVersions[$code] = $version[0]->version; |
46
|
|
|
} |
47
|
|
|
return true; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function compareMigrations($code, $migrations) { |
51
|
|
|
if (!isset($this->migrationsVersions[$code])) { |
52
|
|
|
$this->loadMigrationsVersion($code); |
53
|
|
|
} |
54
|
|
|
if (!isset($this->migrationsVersions[$code]) || !isset($migrations[$this->migrationsVersions[$code]])) { |
55
|
|
|
return $migrations; |
56
|
|
|
} |
57
|
|
|
$startVersion = $this->migrationsVersions[$code]; |
58
|
|
|
end($migrations); |
59
|
|
|
if ($startVersion == key($migrations)) { |
60
|
|
|
return []; |
61
|
|
|
} |
62
|
|
|
$pos = 0; |
63
|
|
|
foreach ($migrations as $migrationVersion => $migration) { |
64
|
|
|
$pos++; |
65
|
|
|
if ($startVersion == $migrationVersion) { |
66
|
|
|
return array_slice($migrations, $pos, null, true); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
} |
70
|
|
|
return []; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function makeMigration($code, $version, $migration) { |
74
|
|
|
if (!isset($migration['up'])) { |
75
|
|
|
return false; |
76
|
|
|
} |
77
|
|
|
if (is_callable($migration['up'])) { |
78
|
|
|
$migration['up'](); |
79
|
|
|
} |
80
|
|
|
$this->migrationsVersions[$code] = $version; |
81
|
|
|
$migrationVersion = new Db\Migration(['code' => $code, 'version' => $version]); |
82
|
|
|
$migrationVersion->save(); |
83
|
|
|
return true; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function __call($name, $params) { |
87
|
|
|
if (!is_object($this->connection)) { |
88
|
|
|
return false; |
89
|
|
|
} |
90
|
|
|
if (method_exists($this->className, $name)) { |
91
|
|
|
return call_user_func_array(array($this->connection, $name), $params); |
92
|
|
|
} |
93
|
|
|
if (method_exists($this->QueryClassName, $name)) { |
94
|
|
|
if (!is_object($this->curQuery)) { |
95
|
|
|
$this->curQuery = new $this->QueryClassName($this->connection); |
96
|
|
|
} |
97
|
|
|
return call_user_func_array(array($this->curQuery, $name), $params); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return false; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function newQuery() { |
104
|
|
|
if ($this->QueryClassName) { |
105
|
|
|
return new $this->QueryClassName($this->connection); |
106
|
|
|
} |
107
|
|
|
return false; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
View Code Duplication |
public function __get($name) { |
111
|
|
|
if (isset($this->connection->$name)) { |
112
|
|
|
return $this->connection->$name; |
113
|
|
|
} |
114
|
|
|
if (!is_object($this->curQuery)) { |
115
|
|
|
$this->curQuery = $this->newQuery(); |
116
|
|
|
} |
117
|
|
|
if (isset($this->curQuery->$name)) { |
118
|
|
|
return $this->curQuery->$name; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
122
|
|
View Code Duplication |
public function __set($name, $value) { |
123
|
|
|
if (isset($this->connection->$name)) { |
124
|
|
|
return $this->connection->$name = $value; |
125
|
|
|
} |
126
|
|
|
if (!is_object($this->curQuery)) { |
127
|
|
|
$this->curQuery = $this->newQuery(); |
128
|
|
|
} |
129
|
|
|
if (isset($this->curQuery->$name)) { |
130
|
|
|
return $this->curQuery->$name = $value; |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
} |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.