injitools /
cms-Inji
| 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 | 1 | public function init($param = 'default') { |
|
| 16 | 1 | if (!$param || $param === 'default') { |
|
| 17 | 1 | $param = isset($this->config['default']) ? $this->config['default'] : 'local'; |
|
| 18 | } |
||
| 19 | 1 | if ($param === 'injiStorage') { |
|
| 20 | $db = [ |
||
| 21 | 1 | 'driver' => 'InjiStorage' |
|
| 22 | ]; |
||
| 23 | 1 | } else if (!is_array($param)) { |
|
| 24 | 1 | if (!($dbOption = Db\Options::sharedStorage()->where('connect_alias', $param)->get(['array' => true]))) { |
|
| 25 | 1 | return false; |
|
| 26 | } |
||
| 27 | $db = $dbOption; |
||
| 28 | } else { |
||
| 29 | $db = $param; |
||
| 30 | } |
||
| 31 | |||
| 32 | 1 | $className = 'Inji\Db\\' . $db['driver']; |
|
| 33 | 1 | $this->connection = new $className(); |
|
| 34 | 1 | if (method_exists($className, 'init')) { |
|
| 35 | $this->connection->init($db); |
||
| 36 | } |
||
| 37 | 1 | $this->connection->dbInstance = $this; |
|
| 38 | 1 | $this->connect = $this->connection->connect; |
|
| 39 | 1 | $this->dbConfig = $db; |
|
| 40 | |||
| 41 | 1 | $this->className = 'Inji\Db\\' . $this->dbConfig['driver']; |
|
| 42 | 1 | $this->QueryClassName = 'Inji\Db\\' . $this->dbConfig['driver'] . '\\Query'; |
|
| 43 | 1 | $this->ResultClassName = 'Inji\Db\\' . $this->dbConfig['driver'] . '\\Result'; |
|
| 44 | 1 | } |
|
| 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) { |
||
|
0 ignored issues
–
show
|
|||
| 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 | /** |
||
| 111 | * @return false|\Inji\Db\DriverQuery |
||
| 112 | */ |
||
| 113 | 4 | public function newQuery() { |
|
| 114 | 4 | if ($this->QueryClassName) { |
|
| 115 | 4 | return new $this->QueryClassName($this->connection); |
|
| 116 | } |
||
| 117 | return false; |
||
| 118 | } |
||
| 119 | |||
| 120 | public function __get($name) { |
||
| 121 | if (isset($this->connection->$name)) { |
||
| 122 | return $this->connection->$name; |
||
| 123 | } |
||
| 124 | if (!is_object($this->curQuery)) { |
||
| 125 | $this->curQuery = $this->newQuery(); |
||
| 126 | } |
||
| 127 | if (isset($this->curQuery->$name)) { |
||
| 128 | return $this->curQuery->$name; |
||
| 129 | } |
||
| 130 | } |
||
| 131 | |||
| 132 | public function __set($name, $value) { |
||
| 133 | if (isset($this->connection->$name)) { |
||
| 134 | return $this->connection->$name = $value; |
||
| 135 | } |
||
| 136 | if (!is_object($this->curQuery)) { |
||
| 137 | $this->curQuery = $this->newQuery(); |
||
| 138 | } |
||
| 139 | if (isset($this->curQuery->$name)) { |
||
| 140 | return $this->curQuery->$name = $value; |
||
| 141 | } |
||
| 142 | } |
||
| 143 | } |
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.