|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the distributed-configuration-bundle package |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright (c) 2016 Guillaume Cavana |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
* |
|
11
|
|
|
* Feel free to edit as you please, and have fun. |
|
12
|
|
|
* |
|
13
|
|
|
* @author Guillaume Cavana <[email protected]> |
|
14
|
|
|
*/ |
|
15
|
|
|
|
|
16
|
|
|
namespace Maikuro\DistributedConfigurationBundle\DependencyInjection; |
|
17
|
|
|
|
|
18
|
|
|
use Symfony\Component\Config\Definition\Processor; |
|
19
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
20
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
|
21
|
|
|
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException; |
|
22
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
|
23
|
|
|
use Symfony\Component\HttpKernel\DependencyInjection\Extension; |
|
24
|
|
|
|
|
25
|
|
|
class MaikuroDistributedConfigurationExtension extends Extension |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* {@inheritdoc} |
|
29
|
|
|
*/ |
|
30
|
|
|
public function load(array $configs, ContainerBuilder $container) |
|
31
|
|
|
{ |
|
32
|
|
|
$processor = new Processor(); |
|
33
|
|
|
$configuration = new Configuration(); |
|
34
|
|
|
$config = $processor->processConfiguration($configuration, $configs); |
|
35
|
|
|
|
|
36
|
|
|
$this->loadStoreHandler($container, $config); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* loadStoreHandler. |
|
41
|
|
|
* |
|
42
|
|
|
* @param ContainerBuilder $container |
|
43
|
|
|
* @param array $config |
|
44
|
|
|
*/ |
|
45
|
|
|
private function loadStoreHandler(ContainerBuilder $container, array $config) |
|
46
|
|
|
{ |
|
47
|
|
|
$storeHandlerDef = new Definition('Maikuro\DistributedConfigurationBundle\Handler\StoreHandler'); |
|
48
|
|
|
$storeDef = $this->loadStore($container, $config['store']); |
|
49
|
|
|
if (null === $storeDef) { |
|
50
|
|
|
throw new InvalidArgumentException('No store found'); |
|
51
|
|
|
} |
|
52
|
|
|
$storeHandlerDef->addArgument($storeDef); |
|
53
|
|
|
//Enabled caching |
|
54
|
|
|
if ($config['cache']['enabled']) { |
|
55
|
|
|
if (null === $config['cache']['service_id']) { |
|
56
|
|
|
throw new InvalidArgumentException( |
|
57
|
|
|
'You need to install DoctrineCacheBundle and configure it to use cache feature' |
|
58
|
|
|
); |
|
59
|
|
|
} |
|
60
|
|
|
$cachingDecoratorDef = new Definition('Webmozart\KeyValueStore\Decorator\CachingDecorator', |
|
61
|
|
|
[$storeDef, new Reference($config['cache']['service_id'])] |
|
62
|
|
|
); |
|
63
|
|
|
$storeHandlerDef->replaceArgument(0, $cachingDecoratorDef); |
|
64
|
|
|
} |
|
65
|
|
|
$container->setDefinition('gundan_distributed_configuration.store_handler', $storeHandlerDef); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
private function loadStore(ContainerBuilder $container, array $config) |
|
69
|
|
|
{ |
|
70
|
|
|
switch ($config['type']) { |
|
71
|
|
|
case 'redis': |
|
72
|
|
|
return $this->loadRedisStore($container, $config); |
|
73
|
|
|
break; |
|
|
|
|
|
|
74
|
|
|
case 'predis': |
|
75
|
|
|
return $this->loadPredisStore($container, $config); |
|
76
|
|
|
break; |
|
|
|
|
|
|
77
|
|
|
case 'dbal': |
|
78
|
|
|
return $this->loadDbalStore($container, $config); |
|
79
|
|
|
break; |
|
|
|
|
|
|
80
|
|
|
case 'json': |
|
81
|
|
|
return $this->loadJsonStore($container, $config); |
|
82
|
|
|
break; |
|
|
|
|
|
|
83
|
|
|
default: |
|
84
|
|
|
return; |
|
85
|
|
|
break; |
|
|
|
|
|
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
private function loadRedisStore(ContainerBuilder $container, array $config) |
|
90
|
|
|
{ |
|
91
|
|
|
$redisStoreDef = new Definition('Webmozart\KeyValueStore\PhpRedisStore'); |
|
92
|
|
|
|
|
93
|
|
|
if (isset($config['store']['connection_id'])) { |
|
94
|
|
|
$redisStoreDef->addArgument(new Reference($config['connection_id'])); |
|
95
|
|
|
} else { |
|
96
|
|
|
$host = $config['host']; |
|
97
|
|
|
$port = $config['port']; |
|
98
|
|
|
$connId = 'gundan_distributed_configuration.redis.connection'; |
|
99
|
|
|
$connDef = new Definition('Redis'); |
|
100
|
|
|
$connParams = array($host, $port); |
|
101
|
|
|
if (isset($config['timeout'])) { |
|
102
|
|
|
$connParams[] = $config['timeout']; |
|
103
|
|
|
} |
|
104
|
|
|
$connMethod = 'connect'; |
|
105
|
|
|
if (isset($config['persistent']) && $config['persistent']) { |
|
106
|
|
|
$connMethod = 'pconnect'; |
|
107
|
|
|
} |
|
108
|
|
|
$connDef->setPublic(false); |
|
109
|
|
|
$connDef->addMethodCall($connMethod, $connParams); |
|
110
|
|
View Code Duplication |
if (isset($config['password'])) { |
|
|
|
|
|
|
111
|
|
|
$password = $config['password']; |
|
112
|
|
|
$connDef->addMethodCall('auth', array($password)); |
|
113
|
|
|
} |
|
114
|
|
View Code Duplication |
if (isset($config['database'])) { |
|
|
|
|
|
|
115
|
|
|
$database = (int) $config['database']; |
|
116
|
|
|
$connDef->addMethodCall('select', array($database)); |
|
117
|
|
|
} |
|
118
|
|
|
$container->setDefinition($connId, $connDef); |
|
119
|
|
|
|
|
120
|
|
|
$redisStoreDef->addArgument(new Reference($connId)); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
return $redisStoreDef; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
private function loadPredisStore(ContainerBuilder $container, array $config) |
|
127
|
|
|
{ |
|
128
|
|
|
$predisStoreDef = new Definition('Webmozart\KeyValueStore\PredisStore'); |
|
129
|
|
|
|
|
130
|
|
|
if (isset($config['client_id'])) { |
|
131
|
|
|
$predisStoreDef->addArgument(new Reference($config['connection_id'])); |
|
132
|
|
|
} else { |
|
133
|
|
|
$parameters = array( |
|
134
|
|
|
'scheme' => $config['scheme'], |
|
135
|
|
|
'host' => $config['host'], |
|
136
|
|
|
'port' => $config['port'], |
|
137
|
|
|
); |
|
138
|
|
|
if ($config['password']) { |
|
139
|
|
|
$parameters['password'] = $config['password']; |
|
140
|
|
|
} |
|
141
|
|
|
if ($config['timeout']) { |
|
142
|
|
|
$parameters['timeout'] = $config['timeout']; |
|
143
|
|
|
} |
|
144
|
|
|
if ($config['database']) { |
|
145
|
|
|
$parameters['database'] = $config['database']; |
|
146
|
|
|
} |
|
147
|
|
|
$options = null; |
|
148
|
|
|
if (isset($config['options'])) { |
|
149
|
|
|
$options = $config['options']; |
|
150
|
|
|
} |
|
151
|
|
|
$clientId = 'gundan_distributed_configuration.predis.client'; |
|
152
|
|
|
$clientDef = new Definition('Predis\Client'); |
|
153
|
|
|
$clientDef->addArgument($parameters); |
|
154
|
|
|
$clientDef->addArgument($options); |
|
155
|
|
|
$clientDef->setPublic(false); |
|
156
|
|
|
$container->setDefinition($clientId, $clientDef); |
|
157
|
|
|
|
|
158
|
|
|
$predisStoreDef->addArgument(new Reference($clientId)); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
return $predisStoreDef; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
private function loadJsonStore(ContainerBuilder $container, array $config) |
|
|
|
|
|
|
165
|
|
|
{ |
|
166
|
|
|
$jsonStoreDef = new Definition('Webmozart\KeyValueStore\JsonFileStore'); |
|
167
|
|
|
$jsonStoreDef->addArgument($config['json']['path']); |
|
168
|
|
|
|
|
169
|
|
|
return $jsonStoreDef; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
private function loadDbalStore(ContainerBuilder $container, array $config) |
|
|
|
|
|
|
173
|
|
|
{ |
|
174
|
|
|
$dbalStoreDef = new Definition('Webmozart\KeyValueStore\DbalStore'); |
|
175
|
|
|
|
|
176
|
|
|
if (!isset($config['connection_id'])) { |
|
177
|
|
|
throw new InvalidArgumentException('No dbal connection configured'); |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
$dbalStoreDef->addArgument(new Reference($config['connection_id']), $config['table_name']); |
|
|
|
|
|
|
181
|
|
|
|
|
182
|
|
|
return $dbalStoreDef; |
|
183
|
|
|
} |
|
184
|
|
|
} |
|
185
|
|
|
|
The break statement is not necessary if it is preceded for example by a return statement:
If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.