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 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" + } + ] +} 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