forked from silexphp/Silex-Providers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDoctrineServiceProvider.php
129 lines (103 loc) · 3.83 KB
/
DoctrineServiceProvider.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?php
/*
* This file is part of the Silex framework.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Silex\Provider;
use Pimple\Container;
use Pimple\ServiceProviderInterface;
use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\Configuration;
use Doctrine\Common\EventManager;
use Symfony\Bridge\Doctrine\Logger\DbalLogger;
/**
* Doctrine DBAL Provider.
*
* @author Fabien Potencier <[email protected]>
*/
class DoctrineServiceProvider implements ServiceProviderInterface
{
public function register(Container $app)
{
$app['db.default_options'] = [
'driver' => 'pdo_mysql',
'dbname' => null,
'host' => 'localhost',
'user' => 'root',
'password' => null,
];
$app['dbs.options.initializer'] = $app->protect(function () use ($app) {
static $initialized = false;
if ($initialized) {
return;
}
$initialized = true;
if (!isset($app['dbs.options'])) {
$app['dbs.options'] = ['default' => isset($app['db.options']) ? $app['db.options'] : []];
}
$tmp = $app['dbs.options'];
foreach ($tmp as $name => &$options) {
$options = array_replace($app['db.default_options'], $options);
if (!isset($app['dbs.default'])) {
$app['dbs.default'] = $name;
}
}
$app['dbs.options'] = $tmp;
});
$app['dbs'] = function ($app) {
$app['dbs.options.initializer']();
$dbs = new Container();
foreach ($app['dbs.options'] as $name => $options) {
if ($app['dbs.default'] === $name) {
// we use shortcuts here in case the default has been overridden
$config = $app['db.config'];
$manager = $app['db.event_manager'];
} else {
$config = $app['dbs.config'][$name];
$manager = $app['dbs.event_manager'][$name];
}
$dbs[$name] = function ($dbs) use ($options, $config, $manager) {
return DriverManager::getConnection($options, $config, $manager);
};
}
return $dbs;
};
$app['dbs.config'] = function ($app) {
$app['dbs.options.initializer']();
$configs = new Container();
$addLogger = isset($app['logger']) && null !== $app['logger'] && class_exists('Symfony\Bridge\Doctrine\Logger\DbalLogger');
foreach ($app['dbs.options'] as $name => $options) {
$configs[$name] = new Configuration();
if ($addLogger) {
$configs[$name]->setSQLLogger(new DbalLogger($app['logger'], isset($app['stopwatch']) ? $app['stopwatch'] : null));
}
}
return $configs;
};
$app['dbs.event_manager'] = function ($app) {
$app['dbs.options.initializer']();
$managers = new Container();
foreach ($app['dbs.options'] as $name => $options) {
$managers[$name] = new EventManager();
}
return $managers;
};
// shortcuts for the "first" DB
$app['db'] = function ($app) {
$dbs = $app['dbs'];
return $dbs[$app['dbs.default']];
};
$app['db.config'] = function ($app) {
$dbs = $app['dbs.config'];
return $dbs[$app['dbs.default']];
};
$app['db.event_manager'] = function ($app) {
$dbs = $app['dbs.event_manager'];
return $dbs[$app['dbs.default']];
};
}
}