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