1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Facile\MongoDbBundle\DependencyInjection; |
4
|
|
|
|
5
|
|
|
use Facile\MongoDbBundle\Models\ConnectionConfiguration; |
6
|
|
|
use Facile\MongoDbBundle\Services\ClientRegistry; |
7
|
|
|
use MongoDB\Database; |
8
|
|
|
use Symfony\Component\Config\FileLocator; |
9
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
10
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
11
|
|
|
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; |
12
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
13
|
|
|
use Symfony\Component\HttpKernel\DependencyInjection\Extension; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class MongoDbBundleExtension. |
17
|
|
|
*/ |
18
|
|
|
class MongoDbBundleExtension extends Extension |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* {@inheritdoc} |
22
|
|
|
*/ |
23
|
2 |
|
public function load(array $configs, ContainerBuilder $container) |
24
|
|
|
{ |
25
|
2 |
|
$configuration = new Configuration(); |
26
|
2 |
|
$config = $this->processConfiguration($configuration, $configs); |
27
|
2 |
|
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
28
|
2 |
|
$loader->load('factory.xml'); |
29
|
|
|
|
30
|
2 |
|
$clientRegistryDefinition = $this->prepareClientRegistryDefinition($config['clients']); |
31
|
2 |
|
$container->setDefinition('mongo.client_registry', $clientRegistryDefinition); |
32
|
|
|
|
33
|
2 |
|
$this->defineConnections($container, $config['connections']); |
34
|
|
|
|
35
|
2 |
|
return $config; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param array $clientsConfig |
40
|
|
|
* |
41
|
|
|
* @return Definition |
42
|
|
|
*/ |
43
|
2 |
|
private function prepareClientRegistryDefinition(array $clientsConfig): Definition |
44
|
|
|
{ |
45
|
2 |
|
$clientRegistryDefinition = new Definition(ClientRegistry::class); |
46
|
2 |
|
foreach ($clientsConfig as $name => $conf) { |
47
|
|
|
$clientRegistryDefinition |
48
|
2 |
|
->addMethodCall( |
49
|
2 |
|
'addClientConfiguration', |
50
|
|
|
[ |
51
|
2 |
|
$name, |
52
|
2 |
|
$conf |
53
|
|
|
] |
54
|
|
|
); |
55
|
|
|
} |
56
|
|
|
|
57
|
2 |
|
return $clientRegistryDefinition; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param ContainerBuilder $container |
62
|
|
|
* @param array $connections |
63
|
|
|
*/ |
64
|
2 |
|
private function defineConnections(ContainerBuilder $container, array $connections) |
65
|
|
|
{ |
66
|
2 |
|
foreach ($connections as $name => $conf){ |
67
|
2 |
|
$connectionDefinition = new Definition( |
68
|
2 |
|
Database::class, |
69
|
|
|
[ |
70
|
2 |
|
$conf['client_name'], |
71
|
2 |
|
$conf['database_name'], |
72
|
|
|
] |
73
|
|
|
); |
74
|
2 |
|
$connectionDefinition->setFactory([new Reference('mongo.connection_factory'), 'createConnection']); |
75
|
2 |
|
$container->setDefinition('mongo.connection.'.$name, $connectionDefinition); |
76
|
|
|
} |
77
|
2 |
|
$container->setAlias('mongo.connection', 'mongo.connection.' . array_keys($connections)[0]); |
78
|
2 |
|
} |
79
|
|
|
} |
80
|
|
|
|