diff options
author | Igor Scheller <igor.scheller@igorshp.de> | 2017-10-31 13:40:13 +0100 |
---|---|---|
committer | Igor Scheller <igor.scheller@igorshp.de> | 2017-10-31 13:40:13 +0100 |
commit | 60fd72cd1a1e4e53b9af87e00a8c27687c6b5385 (patch) | |
tree | bcf250d05a49a848644af44f09fb897467e3ecb0 /src/Database | |
parent | fb05a38a963784716c105e7f214615ad13d2f272 (diff) |
Added service providers
Diffstat (limited to 'src/Database')
-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'); + } +} |