|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Chubbyphp\ServiceProvider; |
|
6
|
|
|
|
|
7
|
|
|
use Chubbyphp\ServiceProvider\Logger\DoctrineMongoDbLogger; |
|
8
|
|
|
use Doctrine\Common\EventManager; |
|
9
|
|
|
use Doctrine\MongoDB\Configuration; |
|
10
|
|
|
use Doctrine\MongoDB\Connection; |
|
11
|
|
|
use Pimple\Container; |
|
12
|
|
|
use Pimple\ServiceProviderInterface; |
|
13
|
|
|
|
|
14
|
|
|
final class DoctrineMongoDbServiceProvider implements ServiceProviderInterface |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @param Container $container |
|
18
|
|
|
*/ |
|
19
|
3 |
|
public function register(Container $container) |
|
20
|
|
|
{ |
|
21
|
3 |
|
$container['mongodb.default_options'] = $this->getMongoDbDefaultOptions(); |
|
22
|
3 |
|
$container['mongodbs.options.initializer'] = $this->getMongoDbsOptionsInitializerServiceDefinition($container); |
|
23
|
3 |
|
$container['mongodbs'] = $this->getMongoDbsServiceDefinition($container); |
|
24
|
3 |
|
$container['mongodbs.config'] = $this->getMongoDbsConfigServiceDefinition($container); |
|
25
|
3 |
|
$container['mongodbs.event_manager'] = $this->getMongoDbsEventManagerServiceDefinition($container); |
|
26
|
3 |
|
$container['mongodb'] = $this->getMongoDbServiceDefinition($container); |
|
27
|
3 |
|
$container['mongodb.config'] = $this->getMongoDbConfigServiceDefinition($container); |
|
28
|
3 |
|
$container['mongodb.event_manager'] = $this->getMongoDbEventManagerServiceDefinition($container); |
|
29
|
3 |
|
$container['mongodb.logger.batch_insert_threshold'] = 10; |
|
30
|
3 |
|
$container['mongodb.logger.prefix'] = 'MongoDB query: '; |
|
31
|
3 |
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @return array |
|
35
|
|
|
*/ |
|
36
|
3 |
|
private function getMongoDbDefaultOptions(): array |
|
37
|
|
|
{ |
|
38
|
|
|
return [ |
|
39
|
3 |
|
'server' => 'mongodb://localhost:27017', |
|
40
|
|
|
'options' => [], |
|
41
|
|
|
/* @link http://www.php.net/manual/en/mongoclient.construct.php */ |
|
42
|
|
|
]; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @param Container $container |
|
47
|
|
|
* |
|
48
|
|
|
* @return \Closure |
|
49
|
|
|
*/ |
|
50
|
|
View Code Duplication |
private function getMongoDbsOptionsInitializerServiceDefinition(Container $container): \Closure |
|
|
|
|
|
|
51
|
|
|
{ |
|
52
|
3 |
|
return $container->protect(function () use ($container) { |
|
53
|
3 |
|
static $initialized = false; |
|
54
|
|
|
|
|
55
|
3 |
|
if ($initialized) { |
|
56
|
3 |
|
return; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
3 |
|
$initialized = true; |
|
60
|
|
|
|
|
61
|
3 |
|
if (!isset($container['mongodbs.options'])) { |
|
62
|
2 |
|
$container['mongodbs.options'] = [ |
|
63
|
2 |
|
'default' => isset($container['mongodb.options']) ? $container['mongodb.options'] : [], |
|
64
|
|
|
]; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
3 |
|
$tmp = $container['mongodbs.options']; |
|
68
|
3 |
|
foreach ($tmp as $name => &$options) { |
|
69
|
3 |
|
$options = array_replace_recursive($container['mongodb.default_options'], $options); |
|
70
|
|
|
|
|
71
|
3 |
|
if (!isset($container['mongodbs.default'])) { |
|
72
|
3 |
|
$container['mongodbs.default'] = $name; |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
3 |
|
$container['mongodbs.options'] = $tmp; |
|
77
|
3 |
|
}); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @param Container $container |
|
82
|
|
|
* |
|
83
|
|
|
* @return \Closure |
|
84
|
|
|
*/ |
|
85
|
|
|
private function getMongoDbsServiceDefinition(Container $container): \Closure |
|
86
|
|
|
{ |
|
87
|
3 |
|
return function () use ($container) { |
|
88
|
3 |
|
$container['mongodbs.options.initializer'](); |
|
89
|
|
|
|
|
90
|
3 |
|
$mongodbs = new Container(); |
|
91
|
3 |
|
foreach ($container['mongodbs.options'] as $name => $options) { |
|
92
|
3 |
|
if ($container['mongodbs.default'] === $name) { |
|
93
|
|
|
// we use shortcuts here in case the default has been overridden |
|
94
|
3 |
|
$config = $container['mongodb.config']; |
|
95
|
3 |
|
$manager = $container['mongodb.event_manager']; |
|
96
|
|
|
} else { |
|
97
|
1 |
|
$config = $container['mongodbs.config'][$name]; |
|
98
|
1 |
|
$manager = $container['mongodbs.event_manager'][$name]; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
3 |
|
$mongodbs[$name] = function () use ($options, $config, $manager) { |
|
102
|
3 |
|
return new Connection($options['server'], $options['options'], $config, $manager); |
|
103
|
3 |
|
}; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
3 |
|
return $mongodbs; |
|
107
|
3 |
|
}; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @param Container $container |
|
112
|
|
|
* |
|
113
|
|
|
* @return \Closure |
|
114
|
|
|
*/ |
|
115
|
|
|
private function getMongoDbsConfigServiceDefinition(Container $container): \Closure |
|
116
|
|
|
{ |
|
117
|
3 |
|
return function () use ($container) { |
|
118
|
3 |
|
$container['mongodbs.options.initializer'](); |
|
119
|
|
|
|
|
120
|
3 |
|
$configs = new Container(); |
|
121
|
|
|
|
|
122
|
3 |
|
$addLogger = isset($container['logger']) && null !== $container['logger']; |
|
123
|
3 |
|
foreach ($container['mongodbs.options'] as $name => $options) { |
|
124
|
3 |
|
$configs[$name] = function () use ($addLogger, $container) { |
|
125
|
3 |
|
$config = new Configuration(); |
|
126
|
3 |
|
if ($addLogger) { |
|
127
|
2 |
|
$logger = new DoctrineMongoDbLogger( |
|
128
|
2 |
|
$container['logger'], |
|
129
|
2 |
|
$container['mongodb.logger.batch_insert_threshold'], |
|
130
|
2 |
|
$container['mongodb.logger.prefix'] |
|
131
|
|
|
); |
|
132
|
2 |
|
$config->setLoggerCallable([$logger, 'logQuery']); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
3 |
|
return $config; |
|
136
|
3 |
|
}; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
3 |
|
return $configs; |
|
140
|
3 |
|
}; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* @param Container $container |
|
145
|
|
|
* |
|
146
|
|
|
* @return \Closure |
|
147
|
|
|
*/ |
|
148
|
|
View Code Duplication |
private function getMongoDbsEventManagerServiceDefinition(Container $container): \Closure |
|
|
|
|
|
|
149
|
|
|
{ |
|
150
|
3 |
|
return function () use ($container) { |
|
151
|
3 |
|
$container['mongodbs.options.initializer'](); |
|
152
|
|
|
|
|
153
|
3 |
|
$managers = new Container(); |
|
154
|
3 |
|
foreach ($container['mongodbs.options'] as $name => $options) { |
|
155
|
3 |
|
$managers[$name] = function () { |
|
156
|
3 |
|
return new EventManager(); |
|
157
|
3 |
|
}; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
3 |
|
return $managers; |
|
161
|
3 |
|
}; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/*** |
|
165
|
|
|
* @param Container $container |
|
166
|
|
|
* @return \Closure |
|
167
|
|
|
*/ |
|
168
|
|
View Code Duplication |
private function getMongoDbServiceDefinition(Container $container): \Closure |
|
|
|
|
|
|
169
|
|
|
{ |
|
170
|
3 |
|
return function () use ($container) { |
|
171
|
2 |
|
$dbs = $container['mongodbs']; |
|
172
|
|
|
|
|
173
|
2 |
|
return $dbs[$container['mongodbs.default']]; |
|
174
|
3 |
|
}; |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/*** |
|
178
|
|
|
* @param Container $container |
|
179
|
|
|
* @return \Closure |
|
180
|
|
|
*/ |
|
181
|
|
View Code Duplication |
private function getMongoDbConfigServiceDefinition(Container $container): \Closure |
|
|
|
|
|
|
182
|
|
|
{ |
|
183
|
3 |
|
return function () use ($container) { |
|
184
|
3 |
|
$dbs = $container['mongodbs.config']; |
|
185
|
|
|
|
|
186
|
3 |
|
return $dbs[$container['mongodbs.default']]; |
|
187
|
3 |
|
}; |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
/*** |
|
191
|
|
|
* @param Container $container |
|
192
|
|
|
* @return \Closure |
|
193
|
|
|
*/ |
|
194
|
|
View Code Duplication |
private function getMongoDbEventManagerServiceDefinition(Container $container): \Closure |
|
|
|
|
|
|
195
|
|
|
{ |
|
196
|
3 |
|
return function () use ($container) { |
|
197
|
3 |
|
$dbs = $container['mongodbs.event_manager']; |
|
198
|
|
|
|
|
199
|
3 |
|
return $dbs[$container['mongodbs.default']]; |
|
200
|
3 |
|
}; |
|
201
|
|
|
} |
|
202
|
|
|
} |
|
203
|
|
|
|
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.