1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Chubbyphp\DoctrineDbServiceProvider\ServiceFactory; |
6
|
|
|
|
7
|
|
|
use Chubbyphp\Container\Container; |
8
|
|
|
use Chubbyphp\Container\ContainerInterface; |
9
|
|
|
use Chubbyphp\DoctrineDbServiceProvider\Logger\DoctrineDbalLogger; |
10
|
|
|
use Chubbyphp\DoctrineDbServiceProvider\Registry\Psr\DoctrineDbalConnectionRegistry; |
11
|
|
|
use Doctrine\Common\Cache\ApcuCache; |
12
|
|
|
use Doctrine\Common\Cache\ArrayCache; |
13
|
|
|
use Doctrine\Common\Cache\Cache; |
14
|
|
|
use Doctrine\Common\EventManager; |
15
|
|
|
use Doctrine\DBAL\Configuration; |
16
|
|
|
use Doctrine\DBAL\DriverManager; |
17
|
|
|
use Doctrine\DBAL\Types\Type; |
18
|
|
|
|
19
|
|
|
final class DoctrineDbalServiceFactory |
20
|
|
|
{ |
21
|
|
|
public function __invoke(): array |
22
|
|
|
{ |
23
|
|
|
return [ |
24
|
|
|
'doctrine.dbal.connection_registry' => $this->getDbConnectionRegistryDefintion(), |
25
|
|
|
'doctrine.dbal.db' => $this->getDbDefinition(), |
26
|
|
|
'doctrine.dbal.db.cache_factory.apcu' => $this->getDbApcuCacheFactoryDefinition(), |
27
|
|
|
'doctrine.dbal.db.cache_factory.array' => $this->getDbArrayCacheFactoryDefinition(), |
28
|
|
|
'doctrine.dbal.db.config' => $this->getDbConfigDefinition(), |
29
|
|
|
'doctrine.dbal.db.default_options' => $this->getDbDefaultOptions(), |
30
|
|
|
'doctrine.dbal.db.event_manager' => $this->getDbEventManagerDefinition(), |
31
|
|
|
'doctrine.dbal.dbs' => $this->getDbsDefinition(), |
32
|
|
|
'doctrine.dbal.dbs.config' => $this->getDbsConfigDefinition(), |
33
|
|
|
'doctrine.dbal.dbs.event_manager' => $this->getDbsEventManagerDefinition(), |
34
|
|
|
'doctrine.dbal.dbs.name' => $this->getDbsNameDefinition(), |
35
|
|
|
'doctrine.dbal.dbs.options.initializer' => $this->getDbsOptionsInitializerDefinition(), |
36
|
|
|
'doctrine.dbal.types' => $this->getTypesDefinition(), |
37
|
|
|
]; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
private function getDbConnectionRegistryDefintion(): \Closure |
41
|
|
|
{ |
42
|
|
|
return static function (ContainerInterface $container) { |
43
|
|
|
return new DoctrineDbalConnectionRegistry($container); |
44
|
|
|
}; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
private function getDbDefinition(): \Closure |
48
|
|
|
{ |
49
|
|
|
return static function (ContainerInterface $container) { |
50
|
|
|
/** @var Container $dbs */ |
51
|
|
|
$dbs = $container->get('doctrine.dbal.dbs'); |
52
|
|
|
|
53
|
|
|
return $dbs->get($container->get('doctrine.dbal.dbs.default')); |
54
|
|
|
}; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
private function getDbApcuCacheFactoryDefinition(): \Closure |
58
|
|
|
{ |
59
|
|
|
return static function () { |
60
|
|
|
return static function () { |
61
|
|
|
return new ApcuCache(); |
62
|
|
|
}; |
63
|
|
|
}; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
private function getDbArrayCacheFactoryDefinition(): \Closure |
67
|
|
|
{ |
68
|
|
|
return static function () { |
69
|
|
|
return static function () { |
70
|
|
|
return new ArrayCache(); |
71
|
|
|
}; |
72
|
|
|
}; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
private function getDbConfigDefinition(): \Closure |
76
|
|
|
{ |
77
|
|
|
return static function (ContainerInterface $container) { |
78
|
|
|
/** @var Container $dbsConfigs */ |
79
|
|
|
$dbsConfigs = $container->get('doctrine.dbal.dbs.config'); |
80
|
|
|
|
81
|
|
|
return $dbsConfigs->get($container->get('doctrine.dbal.dbs.default')); |
82
|
|
|
}; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
private function getDbDefaultOptions(): \Closure |
86
|
|
|
{ |
87
|
|
|
return static function () { |
88
|
|
|
return [ |
89
|
|
|
'configuration' => [ |
90
|
|
|
'auto_commit' => true, |
91
|
|
|
'cache.result' => ['type' => 'array'], |
92
|
|
|
'filter_schema_assets_expression' => null, // @deprecated |
93
|
|
|
'schema_assets_filter' => null, |
94
|
|
|
], |
95
|
|
|
'connection' => [ |
96
|
|
|
'charset' => 'utf8mb4', |
97
|
|
|
'dbname' => null, |
98
|
|
|
'driver' => 'pdo_mysql', |
99
|
|
|
'host' => 'localhost', |
100
|
|
|
'password' => null, |
101
|
|
|
'path' => null, |
102
|
|
|
'port' => 3306, |
103
|
|
|
'user' => 'root', |
104
|
|
|
], |
105
|
|
|
]; |
106
|
|
|
}; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
private function getDbEventManagerDefinition(): \Closure |
110
|
|
|
{ |
111
|
|
|
return static function (ContainerInterface $container) { |
112
|
|
|
/** @var Container $dbEvents */ |
113
|
|
|
$dbEvents = $container->get('doctrine.dbal.dbs.event_manager'); |
114
|
|
|
|
115
|
|
|
return $dbEvents->get($container->get('doctrine.dbal.dbs.default')); |
116
|
|
|
}; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
private function getDbsDefinition(): \Closure |
120
|
|
|
{ |
121
|
|
|
return static function (ContainerInterface $container) { |
122
|
|
|
$container->get('doctrine.dbal.dbs.options.initializer')(); |
123
|
|
|
|
124
|
|
|
$dbs = new Container(); |
125
|
|
|
foreach ($container->get('doctrine.dbal.dbs.options') as $name => $options) { |
126
|
|
|
if ($container->get('doctrine.dbal.dbs.default') === $name) { |
127
|
|
|
$config = $container->get('doctrine.dbal.db.config'); |
128
|
|
|
$manager = $container->get('doctrine.dbal.db.event_manager'); |
129
|
|
|
} else { |
130
|
|
|
$config = $container->get('doctrine.dbal.dbs.config')->get($name); |
131
|
|
|
$manager = $container->get('doctrine.dbal.dbs.event_manager')->get($name); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
$dbs->factory($name, static function () use ($options, $config, $manager) { |
135
|
|
|
return DriverManager::getConnection($options['connection'], $config, $manager); |
136
|
|
|
}); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
return $dbs; |
140
|
|
|
}; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
private function getDbsConfigDefinition(): \Closure |
144
|
|
|
{ |
145
|
|
|
return function (ContainerInterface $container) { |
146
|
|
|
$container->get('doctrine.dbal.dbs.options.initializer')(); |
147
|
|
|
|
148
|
|
|
$logger = $container->has('logger') ? $container->get('logger') : null; |
149
|
|
|
|
150
|
|
|
$configs = new Container(); |
151
|
|
|
foreach ($container->get('doctrine.dbal.dbs.options') as $name => $options) { |
152
|
|
|
$configs->factory($name, function () use ($logger, $container, $options) { |
153
|
|
|
$configOptions = $options['configuration']; |
154
|
|
|
|
155
|
|
|
$config = new Configuration(); |
156
|
|
|
|
157
|
|
|
if (null !== $logger) { |
158
|
|
|
$config->setSQLLogger(new DoctrineDbalLogger($logger)); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
$config->setResultCacheImpl($this->getCache($container, $configOptions['cache.result'])); |
162
|
|
|
|
163
|
|
|
if (null !== $configOptions['filter_schema_assets_expression']) { |
164
|
|
|
// @deprecated |
165
|
|
|
$config->setFilterSchemaAssetsExpression($configOptions['filter_schema_assets_expression']); |
|
|
|
|
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
if (null !== $configOptions['schema_assets_filter']) { |
169
|
|
|
$config->setSchemaAssetsFilter($configOptions['schema_assets_filter']); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
$config->setAutoCommit($configOptions['auto_commit']); |
173
|
|
|
|
174
|
|
|
return $config; |
175
|
|
|
}); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
return $configs; |
179
|
|
|
}; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
private function getCache(ContainerInterface $container, array $cacheDefinition): Cache |
183
|
|
|
{ |
184
|
|
|
$cacheType = $cacheDefinition['type']; |
185
|
|
|
$cacheOptions = $cacheDefinition['options'] ?? []; |
186
|
|
|
|
187
|
|
|
$cacheFactory = $container->get(sprintf('doctrine.dbal.db.cache_factory.%s', $cacheType)); |
188
|
|
|
|
189
|
|
|
return $cacheFactory($cacheOptions); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
private function getDbsEventManagerDefinition(): \Closure |
193
|
|
|
{ |
194
|
|
|
return static function (ContainerInterface $container) { |
195
|
|
|
$container->get('doctrine.dbal.dbs.options.initializer')(); |
196
|
|
|
|
197
|
|
|
$managers = new Container(); |
198
|
|
|
foreach ($container->get('doctrine.dbal.dbs.name') as $name) { |
199
|
|
|
$managers->factory((string) $name, static function () { |
200
|
|
|
return new EventManager(); |
201
|
|
|
}); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
return $managers; |
205
|
|
|
}; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
private function getDbsNameDefinition(): \Closure |
209
|
|
|
{ |
210
|
|
|
return static function (ContainerInterface $container) { |
211
|
|
|
$container->get('doctrine.dbal.dbs.options.initializer')(); |
212
|
|
|
|
213
|
|
|
return array_keys($container->get('doctrine.dbal.dbs.options')); |
214
|
|
|
}; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
private function getDbsOptionsInitializerDefinition(): \Closure |
218
|
|
|
{ |
219
|
|
|
return static function (ContainerInterface $container) { |
220
|
|
|
return static function () use ($container): void { |
221
|
|
|
static $initialized = false; |
222
|
|
|
|
223
|
|
|
if ($initialized) { |
224
|
|
|
return; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
$initialized = true; |
228
|
|
|
|
229
|
|
|
foreach ($container->get('doctrine.dbal.types') as $typeName => $typeClass) { |
230
|
|
|
if (Type::hasType($typeName)) { |
231
|
|
|
Type::overrideType($typeName, $typeClass); |
232
|
|
|
} else { |
233
|
|
|
Type::addType($typeName, $typeClass); |
234
|
|
|
} |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
if (!$container->has('doctrine.dbal.dbs.options')) { |
238
|
|
|
$container->factory( |
239
|
|
|
'doctrine.dbal.dbs.options', |
240
|
|
|
static function (ContainerInterface $container) { |
241
|
|
|
return [ |
242
|
|
|
'default' => $container->has('doctrine.dbal.db.options') |
243
|
|
|
? $container->get('doctrine.dbal.db.options') |
244
|
|
|
: [], |
245
|
|
|
]; |
246
|
|
|
} |
247
|
|
|
); |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
$tmp = $container->get('doctrine.dbal.dbs.options'); |
251
|
|
|
foreach ($tmp as $name => &$options) { |
252
|
|
|
$options = array_replace_recursive( |
253
|
|
|
$container->get('doctrine.dbal.db.default_options'), |
254
|
|
|
$options |
255
|
|
|
); |
256
|
|
|
|
257
|
|
|
if (!$container->has('doctrine.dbal.dbs.default')) { |
258
|
|
|
$container->factory('doctrine.dbal.dbs.default', static function () use ($name) { |
259
|
|
|
return $name; |
260
|
|
|
}); |
261
|
|
|
} |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
$container->factory('doctrine.dbal.dbs.options', static function () use ($tmp) { |
265
|
|
|
return $tmp; |
266
|
|
|
}); |
267
|
|
|
}; |
268
|
|
|
}; |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
private function getTypesDefinition(): \Closure |
272
|
|
|
{ |
273
|
|
|
return static function () { |
274
|
|
|
return []; |
275
|
|
|
}; |
276
|
|
|
} |
277
|
|
|
} |
278
|
|
|
|
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.