1
|
|
|
<?php declare(strict_types = 1); |
2
|
|
|
|
3
|
|
|
namespace Facile\MongoDbBundle\DependencyInjection; |
4
|
|
|
|
5
|
|
|
use Facile\MongoDbBundle\Command\DropCollectionCommand; |
6
|
|
|
use Facile\MongoDbBundle\Command\DropDatabaseCommand; |
7
|
|
|
use Facile\MongoDbBundle\Command\LoadFixturesCommand; |
8
|
|
|
use Facile\MongoDbBundle\DataCollector\MongoDbDataCollector; |
9
|
|
|
use Facile\MongoDbBundle\Event\ConnectionEvent; |
10
|
|
|
use Facile\MongoDbBundle\Event\Listener\DataCollectorListener; |
11
|
|
|
use Facile\MongoDbBundle\Event\QueryEvent; |
12
|
|
|
use Facile\MongoDbBundle\Services\ClientRegistry; |
13
|
|
|
use Facile\MongoDbBundle\Services\ConnectionFactory; |
14
|
|
|
use Facile\MongoDbBundle\Services\Explain\ExplainQueryService; |
15
|
|
|
use Facile\MongoDbBundle\Services\Loggers\MongoQueryLogger; |
16
|
|
|
use Facile\MongoDbBundle\Twig\FacileMongoDbBundleExtension; |
17
|
|
|
use MongoDB\Database; |
18
|
|
|
use Symfony\Bundle\WebProfilerBundle\WebProfilerBundle; |
19
|
|
|
use Symfony\Component\DependencyInjection\Alias; |
20
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
21
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
22
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
23
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcher; |
24
|
|
|
use Symfony\Component\HttpKernel\DependencyInjection\Extension; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Class MongoDbBundleExtension. |
28
|
|
|
* @internal |
29
|
|
|
*/ |
30
|
|
|
final class MongoDbBundleExtension extends Extension |
31
|
|
|
{ |
32
|
|
|
/** @var ContainerBuilder */ |
33
|
|
|
private $containerBuilder; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* {@inheritdoc} |
37
|
|
|
*/ |
38
|
6 |
|
public function load(array $configs, ContainerBuilder $container) |
39
|
|
|
{ |
40
|
6 |
|
$this->containerBuilder = $container; |
41
|
6 |
|
$configuration = new Configuration(); |
42
|
6 |
|
$config = $this->processConfiguration($configuration, $configs); |
43
|
|
|
|
44
|
6 |
|
$this->defineEventManager(); |
45
|
6 |
|
$this->defineClientRegistry($config['clients'], $container->getParameter("kernel.environment")); |
46
|
6 |
|
$this->defineConnectionFactory(); |
47
|
6 |
|
$this->defineConnections($config['connections']); |
48
|
6 |
|
$this->defineCommands(); |
49
|
|
|
|
50
|
6 |
|
if ($this->mustCollectData($config)) { |
51
|
3 |
|
$this->defineLoggers(); |
52
|
3 |
|
$this->defineDataCollectorListeners(); |
53
|
3 |
|
$this->attachDataCollectionListenerToEventManager(); |
54
|
3 |
|
$this->defineDataCollector(); |
55
|
3 |
|
$this->attachTwigExtesion(); |
56
|
3 |
|
$this->defineExplainQueryService(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
|
60
|
6 |
|
return $config; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param array $config |
65
|
|
|
* |
66
|
|
|
* @return bool |
67
|
|
|
*/ |
68
|
6 |
|
private function mustCollectData(array $config): bool |
69
|
|
|
{ |
70
|
6 |
|
return in_array($this->containerBuilder->getParameter("kernel.environment"), ["dev"]) |
71
|
6 |
|
&& class_exists(WebProfilerBundle::class) |
72
|
6 |
|
&& $config['data_collection'] === true; |
73
|
|
|
} |
74
|
|
|
|
75
|
3 |
|
private function defineLoggers() |
76
|
|
|
{ |
77
|
3 |
|
$loggerDefinition = new Definition(MongoQueryLogger::class); |
78
|
3 |
|
$loggerDefinition->setPublic(false); |
79
|
|
|
|
80
|
3 |
|
$this->containerBuilder->setDefinition('facile_mongo_db.logger', $loggerDefinition); |
81
|
3 |
|
} |
82
|
|
|
|
83
|
3 |
|
private function defineDataCollector() |
84
|
|
|
{ |
85
|
3 |
|
$dataCollectorDefinition = new Definition(MongoDbDataCollector::class); |
86
|
3 |
|
$dataCollectorDefinition->addMethodCall('setLogger', [new Reference('facile_mongo_db.logger')]); |
87
|
3 |
|
$dataCollectorDefinition->addTag( |
88
|
3 |
|
'data_collector', |
89
|
|
|
[ |
90
|
3 |
|
'template' => 'FacileMongoDbBundle:Collector:mongo.html.twig', |
91
|
|
|
'id' => 'mongodb', |
92
|
|
|
'priority' => 250, |
93
|
|
|
] |
94
|
|
|
); |
95
|
3 |
|
$dataCollectorDefinition->setPublic(false); |
96
|
|
|
|
97
|
3 |
|
$this->containerBuilder->setDefinition('facile_mongo_db.data_collector', $dataCollectorDefinition); |
98
|
3 |
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param array $clientsConfig |
102
|
|
|
* @param string $environment |
103
|
|
|
*/ |
104
|
6 |
|
private function defineClientRegistry(array $clientsConfig, string $environment) |
105
|
|
|
{ |
106
|
6 |
|
$clientRegistryDefinition = new Definition( |
107
|
6 |
|
ClientRegistry::class, |
108
|
|
|
[ |
109
|
6 |
|
new Reference('facile_mongo_db.event_dispatcher'), |
110
|
6 |
|
$environment, |
111
|
|
|
] |
112
|
|
|
); |
113
|
6 |
|
$clientRegistryDefinition->addMethodCall('addClientsConfigurations', [$clientsConfig]); |
114
|
6 |
|
$clientRegistryDefinition->setPublic(true); |
115
|
|
|
|
116
|
6 |
|
$this->containerBuilder->setDefinition('mongo.client_registry', $clientRegistryDefinition); |
117
|
6 |
|
} |
118
|
|
|
|
119
|
6 |
View Code Duplication |
private function defineConnectionFactory() |
|
|
|
|
120
|
|
|
{ |
121
|
6 |
|
$factoryDefinition = new Definition(ConnectionFactory::class, [new Reference('mongo.client_registry')]); |
122
|
6 |
|
$factoryDefinition->setPublic(false); |
123
|
|
|
|
124
|
6 |
|
$this->containerBuilder->setDefinition('mongo.connection_factory', $factoryDefinition); |
125
|
6 |
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param array $connections |
129
|
|
|
*/ |
130
|
6 |
|
private function defineConnections(array $connections) |
131
|
|
|
{ |
132
|
6 |
|
foreach ($connections as $name => $conf) { |
133
|
6 |
|
$connectionDefinition = new Definition( |
134
|
6 |
|
Database::class, |
135
|
|
|
[ |
136
|
6 |
|
$conf['client_name'], |
137
|
6 |
|
$conf['database_name'], |
138
|
|
|
] |
139
|
|
|
); |
140
|
6 |
|
$connectionDefinition->setFactory([new Reference('mongo.connection_factory'), 'createConnection']); |
141
|
6 |
|
$connectionDefinition->setPublic(true); |
142
|
6 |
|
$this->containerBuilder->setDefinition('mongo.connection.'.$name, $connectionDefinition); |
143
|
|
|
} |
144
|
6 |
|
$this->containerBuilder->setAlias('mongo.connection', new Alias('mongo.connection.'.array_keys($connections)[0], true)); |
145
|
6 |
|
} |
146
|
|
|
|
147
|
6 |
|
private function defineEventManager() |
148
|
|
|
{ |
149
|
6 |
|
$eventManagerDefinition = new Definition(EventDispatcher::class); |
150
|
6 |
|
$eventManagerDefinition->setPublic(false); |
151
|
|
|
|
152
|
6 |
|
$this->containerBuilder->setDefinition('facile_mongo_db.event_dispatcher', $eventManagerDefinition); |
153
|
6 |
|
} |
154
|
|
|
|
155
|
3 |
View Code Duplication |
private function defineDataCollectorListeners() |
|
|
|
|
156
|
|
|
{ |
157
|
3 |
|
$dataCollectorListenerDefinition = new Definition( |
158
|
3 |
|
DataCollectorListener::class, |
159
|
|
|
[ |
160
|
3 |
|
new Reference('facile_mongo_db.logger') |
161
|
|
|
] |
162
|
|
|
); |
163
|
3 |
|
$dataCollectorListenerDefinition->setPublic(false); |
164
|
|
|
|
165
|
3 |
|
$this->containerBuilder->setDefinition('facile_mongo_db.data_collector.listener', $dataCollectorListenerDefinition); |
166
|
3 |
|
} |
167
|
|
|
|
168
|
3 |
|
private function attachDataCollectionListenerToEventManager() |
169
|
|
|
{ |
170
|
3 |
|
$eventManagerDefinition = $this->containerBuilder->getDefinition('facile_mongo_db.event_dispatcher'); |
171
|
3 |
|
$eventManagerDefinition->addMethodCall( |
172
|
3 |
|
'addListener', |
173
|
|
|
[ |
174
|
3 |
|
ConnectionEvent::CLIENT_CREATED, |
175
|
3 |
|
[new Reference('facile_mongo_db.data_collector.listener'), 'onConnectionClientCreated'] |
176
|
|
|
] |
177
|
|
|
); |
178
|
3 |
|
$eventManagerDefinition->addMethodCall( |
179
|
3 |
|
'addListener', |
180
|
|
|
[ |
181
|
3 |
|
QueryEvent::QUERY_EXECUTED, |
182
|
3 |
|
[new Reference('facile_mongo_db.data_collector.listener'), 'onQueryExecuted'] |
183
|
|
|
] |
184
|
|
|
); |
185
|
3 |
|
} |
186
|
|
|
|
187
|
3 |
|
private function attachTwigExtesion() |
188
|
|
|
{ |
189
|
3 |
|
$extension = new Definition(FacileMongoDbBundleExtension::class); |
190
|
3 |
|
$extension->setPublic(false); |
191
|
3 |
|
$extension->addTag('twig.extension'); |
192
|
|
|
|
193
|
3 |
|
$this->containerBuilder->setDefinition('facile_mongo_db.twig_extesion', $extension); |
194
|
3 |
|
} |
195
|
|
|
|
196
|
3 |
View Code Duplication |
private function defineExplainQueryService() |
|
|
|
|
197
|
|
|
{ |
198
|
3 |
|
$explainServiceDefinition = new Definition( |
199
|
3 |
|
ExplainQueryService::class, |
200
|
3 |
|
[new Reference('mongo.client_registry')] |
201
|
|
|
); |
202
|
3 |
|
$explainServiceDefinition->setPublic(true); |
203
|
|
|
|
204
|
3 |
|
$this->containerBuilder->setDefinition('mongo.explain_query_service', $explainServiceDefinition); |
205
|
3 |
|
} |
206
|
|
|
|
207
|
6 |
|
private function defineCommands() |
208
|
|
|
{ |
209
|
|
|
$commandClasses = [ |
210
|
6 |
|
DropCollectionCommand::class, |
211
|
|
|
DropDatabaseCommand::class, |
212
|
|
|
LoadFixturesCommand::class, |
213
|
|
|
]; |
214
|
|
|
|
215
|
6 |
|
foreach ($commandClasses as $command) { |
216
|
6 |
|
$this->containerBuilder->setDefinition($command, new Definition($command)) |
217
|
6 |
|
->addTag('console.command'); |
218
|
|
|
} |
219
|
6 |
|
} |
220
|
|
|
} |
221
|
|
|
|
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.