Common
This commit is contained in:
8
common/Define/Engine.php
Normal file
8
common/Define/Engine.php
Normal file
@ -0,0 +1,8 @@
|
||||
<?php
|
||||
namespace ProVM\Common\Define;
|
||||
|
||||
interface Engine {
|
||||
public function __construct(string $host, string $name, ?int $port = null);
|
||||
public function dsn(): string;
|
||||
public function hasLogin(): bool;
|
||||
}
|
36
common/Service/Database.php
Normal file
36
common/Service/Database.php
Normal file
@ -0,0 +1,36 @@
|
||||
<?php
|
||||
namespace ProVM\Common\Service;
|
||||
|
||||
use \Model;
|
||||
|
||||
class Database {
|
||||
protected $settings;
|
||||
public function __construct($settings) {
|
||||
$this->settings = $settings;
|
||||
}
|
||||
public function load() {
|
||||
foreach ($this->settings->databases as $name => $settings) {
|
||||
$engine = $this->getEngine($settings);
|
||||
$configs = ['connection_string' => $engine->dsn()];
|
||||
if ($engine->hasLogin()) {
|
||||
$configs['username'] = $settings->user->name;
|
||||
$configs['password'] = $settings->user->password;
|
||||
}
|
||||
Model::configure($configs, null, $name);
|
||||
}
|
||||
if (isset($this->settings->short_names)) {
|
||||
Model::$short_table_names = $this->settings->short_names;
|
||||
}
|
||||
}
|
||||
protected function getEngine($settings): \ProVM\Common\Define\Engine {
|
||||
$name = match($settings->engine) {
|
||||
'mysql' => 'MySQL'
|
||||
};
|
||||
$class = implode("\\", [
|
||||
'ProVM',
|
||||
'Database',
|
||||
$name
|
||||
]);
|
||||
return new $class($settings->host->name, $settings->name, $settings->host->port ?? null);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user