2017-10-31 13:40:13 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Engelsystem\Database;
|
|
|
|
|
2019-08-20 23:00:00 +02:00
|
|
|
use Carbon\Carbon;
|
2017-10-31 13:40:13 +01:00
|
|
|
use Engelsystem\Container\ServiceProvider;
|
|
|
|
use Exception;
|
2018-01-14 01:48:50 +01:00
|
|
|
use Illuminate\Database\Capsule\Manager as CapsuleManager;
|
2018-09-16 00:58:25 +02:00
|
|
|
use Illuminate\Database\Connection as DatabaseConnection;
|
2018-01-15 23:31:19 +01:00
|
|
|
use PDOException;
|
2020-10-17 16:23:31 +02:00
|
|
|
use Throwable;
|
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);
|
2019-10-13 20:34:13 +02:00
|
|
|
$now = Carbon::now($config->get('timezone'));
|
2018-01-14 01:48:50 +01:00
|
|
|
|
|
|
|
$dbConfig = $config->get('database');
|
|
|
|
$capsule->addConnection(array_merge([
|
|
|
|
'driver' => 'mysql',
|
|
|
|
'host' => '',
|
|
|
|
'database' => '',
|
|
|
|
'username' => '',
|
|
|
|
'password' => '',
|
2020-04-07 19:36:48 +02:00
|
|
|
'charset' => 'utf8mb4',
|
|
|
|
'collation' => 'utf8mb4_unicode_ci',
|
2019-10-13 20:34:13 +02:00
|
|
|
'timezone' => $now->format('P'),
|
2018-01-14 01:48:50 +01:00
|
|
|
'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
|
|
|
|
2018-09-15 17:24:59 +02:00
|
|
|
$pdo = null;
|
2018-01-14 01:48:50 +01:00
|
|
|
try {
|
2018-09-15 17:24:59 +02:00
|
|
|
$pdo = $capsule->getConnection()->getPdo();
|
2018-01-15 23:31:19 +01:00
|
|
|
} catch (PDOException $e) {
|
2020-10-17 16:23:31 +02:00
|
|
|
$this->exitOnError($e);
|
2018-01-14 01:48:50 +01:00
|
|
|
}
|
|
|
|
|
2018-09-16 00:58:25 +02:00
|
|
|
$this->app->instance(CapsuleManager::class, $capsule);
|
|
|
|
$this->app->instance(Db::class, $capsule);
|
2018-01-14 01:48:50 +01:00
|
|
|
Db::setDbManager($capsule);
|
2018-09-16 00:58:25 +02:00
|
|
|
|
|
|
|
$connection = $capsule->getConnection();
|
|
|
|
$this->app->instance(DatabaseConnection::class, $connection);
|
|
|
|
|
|
|
|
$database = $this->app->make(Database::class);
|
|
|
|
$this->app->instance(Database::class, $database);
|
|
|
|
$this->app->instance('db', $database);
|
|
|
|
$this->app->instance('db.pdo', $pdo);
|
|
|
|
$this->app->instance('db.connection', $connection);
|
2017-10-31 13:40:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-10-17 16:23:31 +02:00
|
|
|
* @param Throwable $exception
|
|
|
|
*
|
2017-10-31 13:40:13 +01:00
|
|
|
* @throws Exception
|
|
|
|
*/
|
2020-10-17 16:23:31 +02:00
|
|
|
protected function exitOnError(Throwable $exception)
|
2017-10-31 13:40:13 +01:00
|
|
|
{
|
2020-10-17 16:23:31 +02:00
|
|
|
throw new Exception('Error: Unable to connect to database', 0, $exception);
|
2017-10-31 13:40:13 +01:00
|
|
|
}
|
|
|
|
}
|