|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* (c) Fabien Potencier <[email protected]> (https://github.com/silexphp/Silex-Providers) |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Chubbyphp\ServiceProvider; |
|
10
|
|
|
|
|
11
|
|
|
use Pimple\Container; |
|
12
|
|
|
use Pimple\ServiceProviderInterface; |
|
13
|
|
|
use Doctrine\DBAL\DriverManager; |
|
14
|
|
|
use Doctrine\DBAL\Configuration; |
|
15
|
|
|
use Doctrine\Common\EventManager; |
|
16
|
|
|
use Symfony\Bridge\Doctrine\Logger\DbalLogger; |
|
17
|
|
|
|
|
18
|
|
|
final class DoctrineDbalServiceProvider implements ServiceProviderInterface |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @param Container $container |
|
22
|
|
|
*/ |
|
23
|
3 |
|
public function register(Container $container) |
|
24
|
|
|
{ |
|
25
|
3 |
|
$container['db.default_options'] = $this->getDbDefaultOptions(); |
|
26
|
3 |
|
$container['dbs.options.initializer'] = $this->getDbsOptionsInitializerServiceDefinition($container); |
|
27
|
3 |
|
$container['dbs'] = $this->getDbsServiceDefinition($container); |
|
28
|
3 |
|
$container['dbs.config'] = $this->getDbsConfigServiceDefinition($container); |
|
29
|
3 |
|
$container['dbs.event_manager'] = $this->getDbsEventManagerServiceDefinition($container); |
|
30
|
3 |
|
$container['db'] = $this->getDbServiceDefinition($container); |
|
31
|
3 |
|
$container['db.config'] = $this->getDbConfigServiceDefinition($container); |
|
32
|
3 |
|
$container['db.event_manager'] = $this->getDbEventManagerServiceDefinition($container); |
|
33
|
3 |
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @return array |
|
37
|
|
|
*/ |
|
38
|
3 |
|
private function getDbDefaultOptions(): array |
|
39
|
|
|
{ |
|
40
|
|
|
return [ |
|
41
|
3 |
|
'driver' => 'pdo_mysql', |
|
42
|
|
|
'dbname' => null, |
|
43
|
|
|
'host' => 'localhost', |
|
44
|
|
|
'user' => 'root', |
|
45
|
|
|
'password' => null, |
|
46
|
|
|
]; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @param Container $container |
|
51
|
|
|
* |
|
52
|
|
|
* @return \Closure |
|
53
|
|
|
*/ |
|
54
|
|
View Code Duplication |
private function getDbsOptionsInitializerServiceDefinition(Container $container): \Closure |
|
|
|
|
|
|
55
|
|
|
{ |
|
56
|
3 |
|
return $container->protect(function () use ($container) { |
|
57
|
3 |
|
static $initialized = false; |
|
58
|
|
|
|
|
59
|
3 |
|
if ($initialized) { |
|
60
|
3 |
|
return; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
3 |
|
$initialized = true; |
|
64
|
|
|
|
|
65
|
3 |
|
if (!isset($container['dbs.options'])) { |
|
66
|
2 |
|
$container['dbs.options'] = [ |
|
67
|
2 |
|
'default' => isset($container['db.options']) ? $container['db.options'] : [], |
|
68
|
|
|
]; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
3 |
|
$tmp = $container['dbs.options']; |
|
72
|
3 |
|
foreach ($tmp as $name => &$options) { |
|
73
|
3 |
|
$options = array_replace($container['db.default_options'], $options); |
|
74
|
|
|
|
|
75
|
3 |
|
if (!isset($container['dbs.default'])) { |
|
76
|
3 |
|
$container['dbs.default'] = $name; |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
3 |
|
$container['dbs.options'] = $tmp; |
|
81
|
3 |
|
}); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @param Container $container |
|
86
|
|
|
* |
|
87
|
|
|
* @return \Closure |
|
88
|
|
|
*/ |
|
89
|
|
|
private function getDbsServiceDefinition(Container $container): \Closure |
|
90
|
|
|
{ |
|
91
|
3 |
|
return function () use ($container) { |
|
92
|
3 |
|
$container['dbs.options.initializer'](); |
|
93
|
|
|
|
|
94
|
3 |
|
$dbs = new Container(); |
|
95
|
3 |
|
foreach ($container['dbs.options'] as $name => $options) { |
|
96
|
3 |
|
if ($container['dbs.default'] === $name) { |
|
97
|
|
|
// we use shortcuts here in case the default has been overridden |
|
98
|
3 |
|
$config = $container['db.config']; |
|
99
|
3 |
|
$manager = $container['db.event_manager']; |
|
100
|
|
|
} else { |
|
101
|
1 |
|
$config = $container['dbs.config'][$name]; |
|
102
|
1 |
|
$manager = $container['dbs.event_manager'][$name]; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
3 |
|
$dbs[$name] = function () use ($options, $config, $manager) { |
|
106
|
3 |
|
return DriverManager::getConnection($options, $config, $manager); |
|
107
|
3 |
|
}; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
3 |
|
return $dbs; |
|
111
|
3 |
|
}; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @param Container $container |
|
116
|
|
|
* |
|
117
|
|
|
* @return \Closure |
|
118
|
|
|
*/ |
|
119
|
|
|
private function getDbsConfigServiceDefinition(Container $container): \Closure |
|
120
|
|
|
{ |
|
121
|
3 |
|
return function () use ($container) { |
|
122
|
3 |
|
$container['dbs.options.initializer'](); |
|
123
|
|
|
|
|
124
|
3 |
|
$addLogger = isset($container['logger']) && null !== $container['logger'] |
|
125
|
3 |
|
&& class_exists('Symfony\Bridge\Doctrine\Logger\DbalLogger'); |
|
126
|
3 |
|
$stopwatch = $container['stopwatch'] ?? null; |
|
127
|
|
|
|
|
128
|
3 |
|
$configs = new Container(); |
|
129
|
3 |
|
foreach ($container['dbs.options'] as $name => $options) { |
|
130
|
3 |
|
$configs[$name] = function () use ($addLogger, $container, $stopwatch) { |
|
131
|
3 |
|
$config = new Configuration(); |
|
132
|
3 |
|
if ($addLogger) { |
|
133
|
2 |
|
$config->setSQLLogger( |
|
134
|
2 |
|
new DbalLogger($container['logger'], $stopwatch) |
|
135
|
|
|
); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
3 |
|
return $config; |
|
139
|
3 |
|
}; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
3 |
|
return $configs; |
|
143
|
3 |
|
}; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* @param Container $container |
|
148
|
|
|
* |
|
149
|
|
|
* @return \Closure |
|
150
|
|
|
*/ |
|
151
|
|
View Code Duplication |
private function getDbsEventManagerServiceDefinition(Container $container): \Closure |
|
|
|
|
|
|
152
|
|
|
{ |
|
153
|
3 |
|
return function () use ($container) { |
|
154
|
3 |
|
$container['dbs.options.initializer'](); |
|
155
|
|
|
|
|
156
|
3 |
|
$managers = new Container(); |
|
157
|
3 |
|
foreach ($container['dbs.options'] as $name => $options) { |
|
158
|
3 |
|
$managers[$name] = function () { |
|
159
|
3 |
|
return new EventManager(); |
|
160
|
3 |
|
}; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
3 |
|
return $managers; |
|
164
|
3 |
|
}; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/*** |
|
168
|
|
|
* @param Container $container |
|
169
|
|
|
* @return \Closure |
|
170
|
|
|
*/ |
|
171
|
|
View Code Duplication |
private function getDbServiceDefinition(Container $container): \Closure |
|
|
|
|
|
|
172
|
|
|
{ |
|
173
|
3 |
|
return function () use ($container) { |
|
174
|
2 |
|
$dbs = $container['dbs']; |
|
175
|
|
|
|
|
176
|
2 |
|
return $dbs[$container['dbs.default']]; |
|
177
|
3 |
|
}; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/*** |
|
181
|
|
|
* @param Container $container |
|
182
|
|
|
* @return \Closure |
|
183
|
|
|
*/ |
|
184
|
|
View Code Duplication |
private function getDbConfigServiceDefinition(Container $container): \Closure |
|
|
|
|
|
|
185
|
|
|
{ |
|
186
|
3 |
|
return function () use ($container) { |
|
187
|
3 |
|
$dbs = $container['dbs.config']; |
|
188
|
|
|
|
|
189
|
3 |
|
return $dbs[$container['dbs.default']]; |
|
190
|
3 |
|
}; |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/*** |
|
194
|
|
|
* @param Container $container |
|
195
|
|
|
* @return \Closure |
|
196
|
|
|
*/ |
|
197
|
|
View Code Duplication |
private function getDbEventManagerServiceDefinition(Container $container): \Closure |
|
|
|
|
|
|
198
|
|
|
{ |
|
199
|
3 |
|
return function () use ($container) { |
|
200
|
3 |
|
$dbs = $container['dbs.event_manager']; |
|
201
|
|
|
|
|
202
|
3 |
|
return $dbs[$container['dbs.default']]; |
|
203
|
3 |
|
}; |
|
204
|
|
|
} |
|
205
|
|
|
} |
|
206
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.