1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Facile\MongoDbBundle\DependencyInjection; |
4
|
|
|
|
5
|
|
|
use Facile\MongoDbBundle\Services\ClientRegistry; |
6
|
|
|
use Facile\MongoDbBundle\Services\ClientUriBuilder; |
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
|
|
|
/** @var ContainerBuilder */ |
21
|
|
|
private $containerBuilder; |
22
|
|
|
/** |
23
|
|
|
* {@inheritdoc} |
24
|
|
|
*/ |
25
|
2 |
|
public function load(array $configs, ContainerBuilder $container) |
26
|
|
|
{ |
27
|
2 |
|
$this->containerBuilder = $container; |
28
|
2 |
|
$configuration = new Configuration(); |
29
|
2 |
|
$config = $this->processConfiguration($configuration, $configs); |
30
|
2 |
|
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
31
|
2 |
|
$loader->load('factory.xml'); |
32
|
|
|
|
33
|
2 |
|
$this->defineClientRegistry($config['clients']); |
34
|
2 |
|
$this->defineConnections($config['connections']); |
35
|
|
|
|
36
|
2 |
|
return $config; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param array $clientsConfig |
41
|
|
|
* |
42
|
|
|
* @return Definition |
43
|
|
|
*/ |
44
|
2 |
|
private function defineClientRegistry(array $clientsConfig) |
45
|
|
|
{ |
46
|
2 |
|
$clientRegistryDefinition = new Definition(ClientRegistry::class); |
47
|
2 |
|
foreach ($clientsConfig as $name => $conf) { |
48
|
|
|
$clientRegistryDefinition |
49
|
2 |
|
->addMethodCall( |
50
|
2 |
|
'addClientConfiguration', |
51
|
|
|
[ |
52
|
2 |
|
$name, |
53
|
2 |
|
$conf |
54
|
|
|
] |
55
|
|
|
); |
56
|
|
|
} |
57
|
|
|
|
58
|
2 |
|
$this->containerBuilder->setDefinition('mongo.client_registry', $clientRegistryDefinition); |
59
|
2 |
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param array $connections |
63
|
|
|
*/ |
64
|
2 |
|
private function defineConnections(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 |
|
$this->containerBuilder->setDefinition('mongo.connection.'.$name, $connectionDefinition); |
76
|
|
|
} |
77
|
2 |
|
$this->containerBuilder->setAlias('mongo.connection', 'mongo.connection.' . array_keys($connections)[0]); |
78
|
2 |
|
} |
79
|
|
|
} |
80
|
|
|
|