1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Facile\MongoDbBundle\DependencyInjection; |
6
|
|
|
|
7
|
|
|
use Facile\MongoDbBundle\Event\ConnectionEvent; |
8
|
|
|
use Facile\MongoDbBundle\Event\QueryEvent; |
9
|
|
|
use Facile\MongoDbBundle\Services\ClientRegistry; |
10
|
|
|
use Facile\MongoDbBundle\Services\ConnectionFactory; |
11
|
|
|
use MongoDB\Database; |
12
|
|
|
use Symfony\Bundle\WebProfilerBundle\WebProfilerBundle; |
13
|
|
|
use Symfony\Component\Config\FileLocator; |
14
|
|
|
use Symfony\Component\DependencyInjection\Alias; |
15
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
16
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
17
|
|
|
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; |
18
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
19
|
|
|
use Symfony\Component\HttpKernel\DependencyInjection\Extension; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @internal |
23
|
|
|
*/ |
24
|
|
|
final class MongoDbBundleExtension extends Extension |
25
|
|
|
{ |
26
|
|
|
/** @var ContainerBuilder */ |
27
|
|
|
private $containerBuilder; |
28
|
|
|
|
29
|
|
|
public function load(array $configs, ContainerBuilder $container) |
30
|
|
|
{ |
31
|
6 |
|
$this->containerBuilder = $container; |
32
|
|
|
$config = $this->processConfiguration(new Configuration(), $configs); |
33
|
6 |
|
|
34
|
6 |
|
$loader = new XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config')); |
35
|
|
|
$loader->load('services.xml'); |
36
|
6 |
|
|
37
|
6 |
|
$this->defineClientRegistry($config, $container->getParameter('kernel.debug')); |
38
|
|
|
$this->defineConnectionFactory(); |
39
|
6 |
|
$this->defineConnections($config['connections']); |
40
|
6 |
|
|
41
|
6 |
|
if ($this->mustCollectData($config)) { |
42
|
|
|
$loader->load('profiler.xml'); |
43
|
6 |
|
$this->attachDataCollectionListenerToEventManager(); |
44
|
3 |
|
} |
45
|
3 |
|
|
46
|
|
|
return $config; |
47
|
|
|
} |
48
|
6 |
|
|
49
|
|
|
private function mustCollectData(array $config): bool |
50
|
|
|
{ |
51
|
|
|
return true === $this->containerBuilder->getParameter('kernel.debug') |
52
|
|
|
&& class_exists(WebProfilerBundle::class) |
53
|
|
|
&& $config['data_collection'] === true; |
54
|
|
|
} |
55
|
|
|
|
56
|
6 |
|
private function defineClientRegistry(array $config, bool $debug): void |
57
|
|
|
{ |
58
|
6 |
|
$clientsConfig = $config['clients']; |
59
|
6 |
|
$clientRegistryDefinition = new Definition( |
60
|
6 |
|
ClientRegistry::class, |
61
|
|
|
[ |
62
|
|
|
new Reference('facile_mongo_db.event_dispatcher'), |
63
|
|
|
$debug, |
64
|
|
|
$this->defineDriverOptionsFactory($config), |
65
|
|
|
] |
66
|
|
|
); |
67
|
6 |
|
$clientRegistryDefinition->addMethodCall('addClientsConfigurations', [$clientsConfig]); |
68
|
|
|
$clientRegistryDefinition->setPublic(true); |
69
|
6 |
|
|
70
|
6 |
|
$this->containerBuilder->setDefinition('mongo.client_registry', $clientRegistryDefinition); |
71
|
|
|
} |
72
|
6 |
|
|
73
|
6 |
|
private function defineConnectionFactory(): void |
74
|
|
|
{ |
75
|
|
|
$factoryDefinition = new Definition(ConnectionFactory::class, [new Reference('mongo.client_registry')]); |
76
|
6 |
|
$factoryDefinition->setPublic(false); |
77
|
6 |
|
|
78
|
|
|
$this->containerBuilder->setDefinition('mongo.connection_factory', $factoryDefinition); |
79
|
6 |
|
} |
80
|
6 |
|
|
81
|
|
|
private function defineConnections(array $connections) |
82
|
6 |
|
{ |
83
|
|
|
foreach ($connections as $name => $conf) { |
84
|
6 |
|
$connectionDefinition = new Definition( |
85
|
6 |
|
Database::class, |
86
|
|
|
[ |
87
|
6 |
|
$conf['client_name'], |
88
|
6 |
|
$conf['database_name'], |
89
|
|
|
] |
90
|
|
|
); |
91
|
|
|
$connectionDefinition->setFactory([new Reference('mongo.connection_factory'), 'createConnection']); |
92
|
|
|
$connectionDefinition->setPublic(true); |
93
|
6 |
|
$this->containerBuilder->setDefinition('mongo.connection.' . $name, $connectionDefinition); |
94
|
|
|
} |
95
|
6 |
|
$this->containerBuilder->setAlias('mongo.connection', new Alias('mongo.connection.' . array_keys($connections)[0], true)); |
96
|
6 |
|
} |
97
|
6 |
|
|
98
|
|
|
private function attachDataCollectionListenerToEventManager(): void |
99
|
6 |
|
{ |
100
|
6 |
|
$eventManagerDefinition = $this->containerBuilder->getDefinition('facile_mongo_db.event_dispatcher'); |
101
|
|
|
$eventManagerDefinition->addMethodCall( |
102
|
|
|
'addListener', |
103
|
6 |
|
[ |
104
|
6 |
|
ConnectionEvent::CLIENT_CREATED, |
105
|
6 |
|
[new Reference('facile_mongo_db.data_collector.listener'), 'onConnectionClientCreated'], |
106
|
|
|
] |
107
|
6 |
|
); |
108
|
6 |
|
$eventManagerDefinition->addMethodCall( |
109
|
|
|
'addListener', |
110
|
3 |
|
[ |
111
|
|
|
QueryEvent::QUERY_EXECUTED, |
112
|
3 |
|
[new Reference('facile_mongo_db.data_collector.listener'), 'onQueryExecuted'], |
113
|
3 |
|
] |
114
|
3 |
|
); |
115
|
|
|
} |
116
|
3 |
|
|
117
|
3 |
|
private function defineDriverOptionsFactory(array $config) |
118
|
|
|
{ |
119
|
|
|
return isset($config['driverOptions']) ? new Reference($config['driverOptions']) : null; |
120
|
3 |
|
} |
121
|
|
|
} |
122
|
|
|
|