Skip to content

Commit b79a0e2

Browse files
committed
integrate doctrine
1 parent 3398064 commit b79a0e2

File tree

3 files changed

+19
-25
lines changed

3 files changed

+19
-25
lines changed

config/container.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
<?php declare(strict_types=1);
22

33
use App\Foundation\Factory\LoggerFactory;
4+
use Doctrine\DBAL\Configuration as DoctrineConfiguration;
5+
use Doctrine\DBAL\Connection;
6+
use Doctrine\DBAL\DriverManager;
47
use Psr\Container\ContainerInterface;
58
use Selective\BasePath\BasePathMiddleware;
69
use Slim\App;
@@ -97,4 +100,16 @@
97100
return $container->get(Session::class);
98101
};
99102

103+
// Database
104+
$object[Connection::class] = function (ContainerInterface $container) {
105+
$config = new DoctrineConfiguration();
106+
$connectionParams = $container->get('settings')['db'];
107+
108+
return DriverManager::getConnection($connectionParams, $config);
109+
};
110+
111+
$object[PDO::class] = function (ContainerInterface $container) {
112+
return $container->get(Connection::class)->getWrappedConnection();
113+
};
114+
100115
return $objects;

config/db.php

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
<?php declare(strict_types=1);
22

3-
$db = [];
4-
5-
$db['connections'] = [];
6-
7-
$db['connections']['mysql'] = [
3+
$connection = [
84
'driver' => 'pdo_mysql',
95
'host' => 'localhost',
106
'port' => 3306,
@@ -22,12 +18,4 @@
2218
],
2319
];
2420

25-
// Grab connections from environment config
26-
$connections = appEnv('db', []);
27-
28-
// Add connections to array.
29-
foreach ($connections as $name => $config) {
30-
$db['connections'][$name] = $config;
31-
}
32-
33-
return $db;
21+
return appEnv('db', $connection);

env.example.php

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,8 @@
88
// App Environment
99
$_ENV['app.environment'] = 'production';
1010

11-
// Database Connections
12-
$_ENV['db'] = [];
13-
14-
// mysql connection example
15-
$_ENV['db']['mysql'] = [
11+
// Database Connection example
12+
$_ENV['db'] = [
1613
'driver' => 'pdo_mysql',
1714
'host' => 'localhost',
1815
'port' => 3306,
@@ -29,9 +26,3 @@
2926
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci',
3027
],
3128
];
32-
33-
// sqlite connection example
34-
$_ENV['db']['sqlite'] = [
35-
'driver' => 'pdo_sqlite',
36-
'url' => 'sqlite:///' . __DIR__ . '/storage/database/app.sqlite',
37-
];

0 commit comments

Comments
 (0)