summaryrefslogtreecommitdiff
path: root/src/Database/DatabaseServiceProvider.php
blob: 49fb4af5e96e0dc691f97fa9817455377a17c91d (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
32
33
<?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');
    }
}