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;
|
2018-01-15 23:31:19 +01:00
|
|
|
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();
|
2018-01-16 21:26:59 +01:00
|
|
|
$capsule->getConnection()->useDefaultSchemaGrammar();
|
2018-01-14 01:48:50 +01:00
|
|
|
|
|
|
|
try {
|
|
|
|
$capsule->getConnection()->getPdo();
|
2018-01-15 23:31:19 +01:00
|
|
|
} 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');
|
|
|
|
}
|
|
|
|
}
|