1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Inji; |
4
|
|
|
class Db extends Module { |
5
|
|
|
public $name = 'Db'; |
6
|
|
|
public $connection = null; |
7
|
|
|
public $connect = false; |
8
|
|
|
public $dbConfig = []; |
9
|
|
|
public $curQuery = null; |
10
|
|
|
public $className = ''; |
11
|
|
|
public $QueryClassName = ''; |
12
|
|
|
public $ResultClassName = ''; |
13
|
|
|
public $migrationsVersions = []; |
14
|
|
|
|
15
|
|
|
public function init($param = 'default') { |
16
|
|
|
if (!$param || $param === 'default') { |
17
|
|
|
$param = isset($this->config['default']) ? $this->config['default'] : 'local'; |
18
|
|
|
} |
19
|
|
|
if ($param === 'injiStorage') { |
20
|
|
|
$db = [ |
21
|
|
|
'driver' => 'InjiStorage' |
22
|
|
|
]; |
23
|
|
|
} else if (!is_array($param)) { |
24
|
|
|
if (!($dbOption = Db\Options::sharedStorage()->where($param, 'connect_alias')->get(['array' => true]))) { |
|
|
|
|
25
|
|
|
return false; |
26
|
|
|
} |
27
|
|
|
$db = $dbOption; |
28
|
|
|
} else { |
29
|
|
|
$db = $param; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
$className = 'Inji\Db\\' . $db['driver']; |
33
|
|
|
$this->connection = new $className(); |
34
|
|
|
if (method_exists($className, 'init')) { |
35
|
|
|
$this->connection->init($db); |
36
|
|
|
} |
37
|
|
|
$this->connection->dbInstance = $this; |
38
|
|
|
$this->connect = $this->connection->connect; |
39
|
|
|
$this->dbConfig = $db; |
40
|
|
|
|
41
|
|
|
$this->className = 'Inji\Db\\' . $this->dbConfig['driver']; |
42
|
|
|
$this->QueryClassName = 'Inji\Db\\' . $this->dbConfig['driver'] . '\\Query'; |
43
|
|
|
$this->ResultClassName = 'Inji\Db\\' . $this->dbConfig['driver'] . '\\Result'; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function loadMigrationsVersion($code) { |
47
|
|
|
if (!$this->connect) { |
48
|
|
|
return false; |
49
|
|
|
} |
50
|
|
|
$version = Db\Migration::getList(['where' => ['code', $code], 'order' => ['date_create', 'desc'], 'limit' => 1, 'key' => false]); |
51
|
|
|
if ($version) { |
|
|
|
|
52
|
|
|
$this->migrationsVersions[$code] = $version[0]->version; |
53
|
|
|
} |
54
|
|
|
return true; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function compareMigrations($code, $migrations) { |
58
|
|
|
if (!isset($this->migrationsVersions[$code])) { |
59
|
|
|
$this->loadMigrationsVersion($code); |
60
|
|
|
} |
61
|
|
|
if (!isset($this->migrationsVersions[$code]) || !isset($migrations[$this->migrationsVersions[$code]])) { |
62
|
|
|
return $migrations; |
63
|
|
|
} |
64
|
|
|
$startVersion = $this->migrationsVersions[$code]; |
65
|
|
|
end($migrations); |
66
|
|
|
if ($startVersion == key($migrations)) { |
67
|
|
|
return []; |
68
|
|
|
} |
69
|
|
|
$pos = 0; |
70
|
|
|
foreach ($migrations as $migrationVersion => $migration) { |
71
|
|
|
$pos++; |
72
|
|
|
if ($startVersion == $migrationVersion) { |
73
|
|
|
return array_slice($migrations, $pos, null, true); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
} |
77
|
|
|
return []; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function makeMigration($code, $version, $migration) { |
81
|
|
|
if (!isset($migration['up'])) { |
82
|
|
|
return false; |
83
|
|
|
} |
84
|
|
|
if (is_callable($migration['up'])) { |
85
|
|
|
$migration['up'](); |
86
|
|
|
} |
87
|
|
|
$this->migrationsVersions[$code] = $version; |
88
|
|
|
$migrationVersion = new Db\Migration(['code' => $code, 'version' => $version]); |
89
|
|
|
$migrationVersion->save(); |
90
|
|
|
return true; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function __call($name, $params) { |
94
|
|
|
if (!is_object($this->connection)) { |
95
|
|
|
return false; |
96
|
|
|
} |
97
|
|
|
if (method_exists($this->className, $name)) { |
98
|
|
|
return call_user_func_array(array($this->connection, $name), $params); |
99
|
|
|
} |
100
|
|
|
if (method_exists($this->QueryClassName, $name)) { |
101
|
|
|
if (!is_object($this->curQuery)) { |
102
|
|
|
$this->curQuery = new $this->QueryClassName($this->connection); |
103
|
|
|
} |
104
|
|
|
return call_user_func_array(array($this->curQuery, $name), $params); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return false; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function newQuery() { |
111
|
|
|
if ($this->QueryClassName) { |
112
|
|
|
return new $this->QueryClassName($this->connection); |
113
|
|
|
} |
114
|
|
|
return false; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
View Code Duplication |
public function __get($name) { |
118
|
|
|
if (isset($this->connection->$name)) { |
119
|
|
|
return $this->connection->$name; |
120
|
|
|
} |
121
|
|
|
if (!is_object($this->curQuery)) { |
122
|
|
|
$this->curQuery = $this->newQuery(); |
123
|
|
|
} |
124
|
|
|
if (isset($this->curQuery->$name)) { |
125
|
|
|
return $this->curQuery->$name; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
129
|
|
View Code Duplication |
public function __set($name, $value) { |
130
|
|
|
if (isset($this->connection->$name)) { |
131
|
|
|
return $this->connection->$name = $value; |
132
|
|
|
} |
133
|
|
|
if (!is_object($this->curQuery)) { |
134
|
|
|
$this->curQuery = $this->newQuery(); |
135
|
|
|
} |
136
|
|
|
if (isset($this->curQuery->$name)) { |
137
|
|
|
return $this->curQuery->$name = $value; |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
} |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: