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(DataCollectorLoggerInterface::class); |
51
|
3 |
|
$loggerDefinition->setFactory([new Reference('mongo.logger_factory'), 'createLogger']); |
52
|
3 |
|
$this->containerBuilder->setDefinition('facile_mongo_db.logger', $loggerDefinition); |
53
|
3 |
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param array $clientsConfig |
57
|
|
|
* |
58
|
|
|
* @return Definition |
59
|
|
|
*/ |
60
|
3 |
|
private function defineClientRegistry(array $clientsConfig) |
61
|
|
|
{ |
62
|
3 |
|
$clientRegistryDefinition = new Definition( |
63
|
3 |
|
ClientRegistry::class, |
64
|
|
|
[ |
65
|
3 |
|
new Reference('facile_mongo_db.logger'), |
66
|
|
|
] |
67
|
|
|
); |
68
|
3 |
|
foreach ($clientsConfig as $name => $conf) { |
69
|
|
|
$clientRegistryDefinition |
70
|
3 |
|
->addMethodCall( |
71
|
3 |
|
'addClientConfiguration', |
72
|
|
|
[ |
73
|
3 |
|
$name, |
74
|
3 |
|
$conf, |
75
|
|
|
] |
76
|
|
|
); |
77
|
|
|
} |
78
|
|
|
|
79
|
3 |
|
$this->containerBuilder->setDefinition('mongo.client_registry', $clientRegistryDefinition); |
80
|
3 |
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param array $connections |
84
|
|
|
*/ |
85
|
3 |
|
private function defineConnections(array $connections) |
86
|
|
|
{ |
87
|
|
|
|
88
|
3 |
|
foreach ($connections as $name => $conf) { |
89
|
3 |
|
$connectionDefinition = new Definition( |
90
|
3 |
|
Database::class, |
91
|
|
|
[ |
92
|
3 |
|
$conf['client_name'], |
93
|
3 |
|
$conf['database_name'], |
94
|
|
|
] |
95
|
|
|
); |
96
|
3 |
|
$connectionDefinition->setFactory([new Reference('mongo.connection_factory'), 'createConnection']); |
97
|
3 |
|
$this->containerBuilder->setDefinition('mongo.connection.'.$name, $connectionDefinition); |
98
|
|
|
} |
99
|
3 |
|
$this->containerBuilder->setAlias('mongo.connection', 'mongo.connection.'.array_keys($connections)[0]); |
100
|
3 |
|
} |
101
|
|
|
} |
102
|
|
|
|