engelsystem/src/Database/DatabaseServiceProvider.php

51 lines
1.3 KiB
PHP
Raw Normal View History

2017-10-31 13:40:13 +01:00
<?php
namespace Engelsystem\Database;
use Engelsystem\Container\ServiceProvider;
use Exception;
2018-01-14 01:48:50 +01:00
use Illuminate\Database\Capsule\Manager as CapsuleManager;
use PDOException;
2017-10-31 13:40:13 +01:00
class DatabaseServiceProvider extends ServiceProvider
{
public function register()
{
$config = $this->app->get('config');
2018-01-14 01:48:50 +01:00
$capsule = $this->app->make(CapsuleManager::class);
$dbConfig = $config->get('database');
$capsule->addConnection(array_merge([
'driver' => 'mysql',
'host' => '',
'database' => '',
'username' => '',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
], $dbConfig));
$capsule->setAsGlobal();
$capsule->bootEloquent();
$capsule->getConnection()->useDefaultSchemaGrammar();
2018-01-14 01:48:50 +01:00
try {
$capsule->getConnection()->getPdo();
} catch (PDOException $e) {
2018-01-14 01:48:50 +01:00
$this->exitOnError();
}
$this->app->instance('db', $capsule);
Db::setDbManager($capsule);
2017-10-31 13:40:13 +01:00
}
/**
* @throws Exception
*/
protected function exitOnError()
{
throw new Exception('Error: Unable to connect to database');
}
}