From edc39bd0fe63845ca3294567f0698604671514e1 Mon Sep 17 00:00:00 2001 From: Aldarien Date: Wed, 13 Oct 2021 22:46:10 -0300 Subject: [PATCH 1/3] Composer --- composer.json | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 composer.json diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..c94854f --- /dev/null +++ b/composer.json @@ -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" + } + ] +} From 68122d14311cb8ff61bcb18b7c293cdef5cbafef Mon Sep 17 00:00:00 2001 From: Aldarien Date: Wed, 13 Oct 2021 22:46:19 -0300 Subject: [PATCH 2/3] Common --- common/Define/Engine.php | 8 ++++++++ common/Service/Database.php | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 common/Define/Engine.php create mode 100644 common/Service/Database.php diff --git a/common/Define/Engine.php b/common/Define/Engine.php new file mode 100644 index 0000000..2369669 --- /dev/null +++ b/common/Define/Engine.php @@ -0,0 +1,8 @@ +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); + } +} \ No newline at end of file From 50bd59498dcf1502b969e4312f542edfbf208205 Mon Sep 17 00:00:00 2001 From: Aldarien Date: Wed, 13 Oct 2021 22:46:32 -0300 Subject: [PATCH 3/3] MySQL Engine --- src/MySQL.php | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 src/MySQL.php diff --git a/src/MySQL.php b/src/MySQL.php new file mode 100644 index 0000000..1cf17a0 --- /dev/null +++ b/src/MySQL.php @@ -0,0 +1,32 @@ + $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; + } +} \ No newline at end of file