Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
33 / 33 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
DatabaseServiceProvider | |
100.00% |
33 / 33 |
|
100.00% |
2 / 2 |
3 | |
100.00% |
1 / 1 |
register | |
100.00% |
32 / 32 |
|
100.00% |
1 / 1 |
2 | |||
exitOnError | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | declare(strict_types=1); |
4 | |
5 | namespace Engelsystem\Database; |
6 | |
7 | use Carbon\Carbon; |
8 | use Engelsystem\Container\ServiceProvider; |
9 | use Exception; |
10 | use Illuminate\Database\Capsule\Manager as CapsuleManager; |
11 | use Illuminate\Database\Connection as DatabaseConnection; |
12 | use PDOException; |
13 | use Throwable; |
14 | |
15 | class DatabaseServiceProvider extends ServiceProvider |
16 | { |
17 | public function register(): void |
18 | { |
19 | $config = $this->app->get('config'); |
20 | $capsule = $this->app->make(CapsuleManager::class); |
21 | $now = Carbon::now($config->get('timezone')); |
22 | |
23 | $dbConfig = $config->get('database'); |
24 | $capsule->addConnection(array_merge([ |
25 | 'driver' => 'mysql', |
26 | 'host' => '', |
27 | 'database' => '', |
28 | 'username' => '', |
29 | 'password' => '', |
30 | 'charset' => 'utf8mb4', |
31 | 'collation' => 'utf8mb4_unicode_ci', |
32 | 'timezone' => $now->format('P'), |
33 | 'prefix' => '', |
34 | ], $dbConfig)); |
35 | |
36 | $capsule->setAsGlobal(); |
37 | $capsule->bootEloquent(); |
38 | $capsule->getConnection()->useDefaultSchemaGrammar(); |
39 | |
40 | $pdo = null; |
41 | try { |
42 | $pdo = $capsule->getConnection()->getPdo(); |
43 | } catch (PDOException $e) { |
44 | $this->exitOnError($e); |
45 | } |
46 | |
47 | $this->app->instance(CapsuleManager::class, $capsule); |
48 | $this->app->instance(Db::class, $capsule); |
49 | Db::setDbManager($capsule); |
50 | |
51 | $connection = $capsule->getConnection(); |
52 | $this->app->instance(DatabaseConnection::class, $connection); |
53 | |
54 | $database = $this->app->make(Database::class); |
55 | $this->app->instance(Database::class, $database); |
56 | $this->app->instance('db', $database); |
57 | $this->app->instance('db.pdo', $pdo); |
58 | $this->app->instance('db.connection', $connection); |
59 | } |
60 | |
61 | /** |
62 | * |
63 | * @throws Exception |
64 | */ |
65 | protected function exitOnError(Throwable $exception): void |
66 | { |
67 | throw new Exception('Error: Unable to connect to database', 0, $exception); |
68 | } |
69 | } |