diff options
Diffstat (limited to 'src/Database/DatabaseServiceProvider.php')
-rw-r--r-- | src/Database/DatabaseServiceProvider.php | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/src/Database/DatabaseServiceProvider.php b/src/Database/DatabaseServiceProvider.php new file mode 100644 index 00000000..364816cc --- /dev/null +++ b/src/Database/DatabaseServiceProvider.php @@ -0,0 +1,31 @@ +<?php + +namespace Engelsystem\Database; + +use Engelsystem\Container\ServiceProvider; +use Exception; +use PDO; + +class DatabaseServiceProvider extends ServiceProvider +{ + public function register() + { + $config = $this->app->get('config'); + Db::connect( + 'mysql:host=' . $config->get('database')['host'] . ';dbname=' . $config->get('database')['db'] . ';charset=utf8', + $config->get('database')['user'], + $config->get('database')['pw'] + ) || $this->exitOnError(); + + Db::getPdo()->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); + Db::getPdo()->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); + } + + /** + * @throws Exception + */ + protected function exitOnError() + { + throw new Exception('Error: Unable to connect to database'); + } +} |