v0.1.0
This commit is contained in:
30
provm/common/Service/Database.php
Normal file
30
provm/common/Service/Database.php
Normal file
@ -0,0 +1,30 @@
|
||||
<?php
|
||||
namespace ProVM\Common\Service;
|
||||
|
||||
use \ORM;
|
||||
use \Model;
|
||||
use ProVM\Common\Service\Database\MySQL;
|
||||
|
||||
class Database {
|
||||
protected $settings;
|
||||
public function __construct($settings) {
|
||||
$this->settings = $settings;
|
||||
}
|
||||
public function load() {
|
||||
foreach ($this->settings->dbs as $name => $data) {
|
||||
switch (strtolower($data->engine)) {
|
||||
case 'mysql':
|
||||
$obj = new MySQL($data);
|
||||
break;
|
||||
}
|
||||
ORM::configure($obj->dsn(), null, $name);
|
||||
if ($obj->hasUser()) {
|
||||
ORM::configure('username', $data->user->name, $name);
|
||||
ORM::configure('password', $data->user->password, $name);
|
||||
}
|
||||
}
|
||||
if (isset($this->settings->short_names)) {
|
||||
Model::$short_table_names = $this->settings->short_names;
|
||||
}
|
||||
}
|
||||
}
|
25
provm/common/Service/Database/DSN.php
Normal file
25
provm/common/Service/Database/DSN.php
Normal file
@ -0,0 +1,25 @@
|
||||
<?php
|
||||
namespace ProVM\Common\Service\Database;
|
||||
|
||||
class DSN {
|
||||
public $engine;
|
||||
public function __construct(string $engine) {
|
||||
$this->engine = $engine;
|
||||
}
|
||||
public $pairs;
|
||||
public function addPair($name, $value) {
|
||||
if ($this->pairs === null) {
|
||||
$this->pairs = [];
|
||||
}
|
||||
$this->pairs []= [$name, $value];
|
||||
return $this;
|
||||
}
|
||||
public function __toString() {
|
||||
return implode(':', [
|
||||
$this->engine,
|
||||
implode(';', array_map(function($item) {
|
||||
return implode('=', $item);
|
||||
}, $this->pairs))
|
||||
]);
|
||||
}
|
||||
}
|
21
provm/common/Service/Database/MySQL.php
Normal file
21
provm/common/Service/Database/MySQL.php
Normal file
@ -0,0 +1,21 @@
|
||||
<?php
|
||||
namespace ProVM\Common\Service\Database;
|
||||
|
||||
class MySQL {
|
||||
protected $settings;
|
||||
public function __construct($settings) {
|
||||
$this->settings = $settings;
|
||||
}
|
||||
public function dsn(): string {
|
||||
$dsn = (new DSN($this->settings->engine))
|
||||
->addPair('host', $this->settings->host->name);
|
||||
if (isset($this->settings->host->port)) {
|
||||
$dsn->addPair('port', $this->settings->host->port);
|
||||
}
|
||||
$dsn->addPair('dbname', $this->settings->name);
|
||||
return '' . $dsn;
|
||||
}
|
||||
public function hasUser(): bool {
|
||||
return true;
|
||||
}
|
||||
}
|
19
provm/common/Service/Migrator.php
Normal file
19
provm/common/Service/Migrator.php
Normal file
@ -0,0 +1,19 @@
|
||||
<?php
|
||||
namespace ProVM\Common\Service;
|
||||
|
||||
class Migrator {
|
||||
protected $schema_filename;
|
||||
protected $migrations_folder;
|
||||
protected $seeds_folder;
|
||||
public function __construct(string $schema_filename, string $migrations_folder, string $seeds_folder) {
|
||||
$this->schema_filename = $schema_filename;
|
||||
$this->migrations_folder = $migrations_folder;
|
||||
$this->seeds_folder = $seeds_folder;
|
||||
}
|
||||
protected $schema;
|
||||
public function schema() {
|
||||
if ($this->schema === null) {
|
||||
$file = new \File($this->schema_filename);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user