Merge branch 'develop'
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);
|
||||
}
|
||||
}
|
21
composer.json
Normal file
21
composer.json
Normal file
@ -0,0 +1,21 @@
|
||||
{
|
||||
"name": "provm/database",
|
||||
"description": "Database loader for j4mie/paris",
|
||||
"type": "library",
|
||||
"require": {
|
||||
"j4mie/paris": "^1.5"
|
||||
},
|
||||
"license": "MIT",
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"ProVM\\Database\\": "src/",
|
||||
"ProVM\\Common\\": "common/"
|
||||
}
|
||||
},
|
||||
"authors": [
|
||||
{
|
||||
"name": "Aldarien",
|
||||
"email": "aldarien85@gmail.com"
|
||||
}
|
||||
]
|
||||
}
|
32
src/MySQL.php
Normal file
32
src/MySQL.php
Normal file
@ -0,0 +1,32 @@
|
||||
<?php
|
||||
namespace ProVM\Database;
|
||||
|
||||
use ProVM\Common\Define\Engine;
|
||||
|
||||
class MySQL implements Engine {
|
||||
protected $host;
|
||||
protected $name;
|
||||
public function __construct(string $host, string $name, ?int $port = null) {
|
||||
$host_arr = [
|
||||
'name' => $host
|
||||
];
|
||||
if ($port !== null) {
|
||||
$host_arr['port'] = $port;
|
||||
}
|
||||
$this->host = (object) $host_arr;
|
||||
$this->name = $name;
|
||||
}
|
||||
public function dsn(): string {
|
||||
$dsn = [
|
||||
'host=' . $this->host->name
|
||||
];
|
||||
if (isset($this->host->port)) {
|
||||
$dsn []= 'port=' . $this->host->port;
|
||||
}
|
||||
$dsn []= 'dbname=' . $this->name;
|
||||
return 'mysql:' . implode(';', $dsn);
|
||||
}
|
||||
public function hasLogin(): bool {
|
||||
return true;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user