summaryrefslogtreecommitdiff
path: root/src/Database/DatabaseServiceProvider.php
blob: 364816cc290a084e11e874ad74858348ce5f60a2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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');
    }
}