1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Facile\MongoDbBundle\DependencyInjection; |
4
|
|
|
|
5
|
|
|
use Facile\MongoDbBundle\Services\ClientRegistry; |
6
|
|
|
use Facile\MongoDbBundle\Services\Loggers\DataCollectorLoggerInterface; |
7
|
|
|
use MongoDB\Database; |
8
|
|
|
use Symfony\Bundle\WebProfilerBundle\WebProfilerBundle; |
9
|
|
|
use Symfony\Component\Config\FileLocator; |
10
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
11
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
12
|
|
|
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; |
13
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
14
|
|
|
use Symfony\Component\HttpKernel\DependencyInjection\Extension; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class MongoDbBundleExtension. |
18
|
|
|
*/ |
19
|
|
|
class MongoDbBundleExtension extends Extension |
20
|
|
|
{ |
21
|
|
|
/** @var ContainerBuilder */ |
22
|
|
|
private $containerBuilder; |
23
|
|
|
private $env; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* {@inheritdoc} |
27
|
|
|
*/ |
28
|
3 |
|
public function load(array $configs, ContainerBuilder $container) |
29
|
|
|
{ |
30
|
3 |
|
$this->containerBuilder = $container; |
31
|
3 |
|
$configuration = new Configuration(); |
32
|
3 |
|
$config = $this->processConfiguration($configuration, $configs); |
33
|
3 |
|
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
34
|
3 |
|
$loader->load('factory.xml'); |
35
|
|
|
|
36
|
3 |
|
$this->env = $container->getParameter("kernel.environment"); |
37
|
3 |
|
if ($this->env === 'dev' && class_exists(WebProfilerBundle::class)) { |
38
|
2 |
|
$loader->load('web_profiler.xml'); |
39
|
|
|
} |
40
|
|
|
|
41
|
3 |
|
$this->defineLoggers(); |
42
|
3 |
|
$this->defineClientRegistry($config['clients']); |
43
|
3 |
|
$this->defineConnections($config['connections']); |
44
|
|
|
|
45
|
3 |
|
return $config; |
46
|
|
|
} |
47
|
|
|
|
48
|
3 |
|
private function defineLoggers() |
49
|
|
|
{ |
50
|
3 |
|
$loggerDefinition = new Definition( |
51
|
3 |
|
DataCollectorLoggerInterface::class, |
52
|
|
|
[ |
53
|
3 |
|
new Reference('debug.stopwatch'), |
54
|
|
|
] |
55
|
|
|
); |
56
|
3 |
|
$loggerDefinition->setFactory([new Reference('mongo.logger_factory'), 'createLogger']); |
57
|
3 |
|
$this->containerBuilder->setDefinition('facile_mongo_db.logger', $loggerDefinition); |
58
|
3 |
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param array $clientsConfig |
62
|
|
|
* |
63
|
|
|
* @return Definition |
64
|
|
|
*/ |
65
|
3 |
|
private function defineClientRegistry(array $clientsConfig) |
66
|
|
|
{ |
67
|
3 |
|
$clientRegistryDefinition = new Definition( |
68
|
3 |
|
ClientRegistry::class, |
69
|
|
|
[ |
70
|
3 |
|
new Reference('facile_mongo_db.logger'), |
71
|
|
|
] |
72
|
|
|
); |
73
|
3 |
|
foreach ($clientsConfig as $name => $conf) { |
74
|
|
|
$clientRegistryDefinition |
75
|
3 |
|
->addMethodCall( |
76
|
3 |
|
'addClientConfiguration', |
77
|
|
|
[ |
78
|
3 |
|
$name, |
79
|
3 |
|
$conf, |
80
|
|
|
] |
81
|
|
|
); |
82
|
|
|
} |
83
|
|
|
|
84
|
3 |
|
$this->containerBuilder->setDefinition('mongo.client_registry', $clientRegistryDefinition); |
85
|
3 |
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param array $connections |
89
|
|
|
*/ |
90
|
3 |
|
private function defineConnections(array $connections) |
91
|
|
|
{ |
92
|
|
|
|
93
|
3 |
|
foreach ($connections as $name => $conf) { |
94
|
3 |
|
$connectionDefinition = new Definition( |
95
|
3 |
|
Database::class, |
96
|
|
|
[ |
97
|
3 |
|
$conf['client_name'], |
98
|
3 |
|
$conf['database_name'], |
99
|
|
|
] |
100
|
|
|
); |
101
|
3 |
|
$connectionDefinition->setFactory([new Reference('mongo.connection_factory'), 'createConnection']); |
102
|
3 |
|
$this->containerBuilder->setDefinition('mongo.connection.'.$name, $connectionDefinition); |
103
|
|
|
} |
104
|
3 |
|
$this->containerBuilder->setAlias('mongo.connection', 'mongo.connection.'.array_keys($connections)[0]); |
105
|
3 |
|
} |
106
|
|
|
} |
107
|
|
|
|