|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace MetroMarkets\FFBundle\DependencyInjection; |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
use MetroMarkets\FFBundle\FeatureFlagService; |
|
7
|
|
|
use MetroMarkets\FFBundle\Providers\ConfigCatProvider; |
|
8
|
|
|
use MetroMarkets\FFBundle\Providers\FalseProvider; |
|
9
|
|
|
use MetroMarkets\FFBundle\Providers\RandomProvider; |
|
10
|
|
|
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException; |
|
11
|
|
|
use Symfony\Component\Config\FileLocator; |
|
12
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
13
|
|
|
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; |
|
14
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
|
15
|
|
|
use Symfony\Component\HttpKernel\DependencyInjection\Extension; |
|
16
|
|
|
|
|
17
|
|
|
|
|
18
|
|
|
class MetroMarketsFFExtension extends Extension |
|
19
|
|
|
{ |
|
20
|
|
|
public function getAlias() |
|
21
|
|
|
{ |
|
22
|
|
|
return 'metro_markets_ff'; |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
public function load(array $configs, ContainerBuilder $container) |
|
26
|
|
|
{ |
|
27
|
|
|
$loader = new YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config')); |
|
28
|
|
|
$loader->load('services.yml'); |
|
29
|
|
|
|
|
30
|
|
|
$configuration = $this->getConfiguration($configs, $container); |
|
31
|
|
|
$config = $this->processConfiguration($configuration, $configs); |
|
|
|
|
|
|
32
|
|
|
|
|
33
|
|
|
$adapter = $config['provider']; |
|
34
|
|
|
|
|
35
|
|
|
switch ($adapter) { |
|
36
|
|
|
case 'configcat': |
|
37
|
|
|
$this->configureConfigCat($container, $config); |
|
38
|
|
|
break; |
|
39
|
|
|
case 'random': |
|
40
|
|
|
$this->configureRandom($container); |
|
41
|
|
|
break; |
|
42
|
|
|
//logger is the default |
|
43
|
|
|
default: |
|
44
|
|
|
$serviceDefinition = $container->getDefinition(FeatureFlagService::class); |
|
45
|
|
|
$serviceDefinition->setArgument(0, new Reference(FalseProvider::class)); |
|
46
|
|
|
|
|
47
|
|
|
break; |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
private function configureConfigCat(ContainerBuilder $container, array $config) |
|
52
|
|
|
{ |
|
53
|
|
|
$adapterDefinition = $container->getDefinition(ConfigCatProvider::class); |
|
54
|
|
|
|
|
55
|
|
|
if (!isset($config['configcat'])) { |
|
56
|
|
|
throw new InvalidConfigurationException('Configcat config must be set when using provider:configcat'); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
if (!isset($config['configcat']['sdk_key'])) { |
|
60
|
|
|
throw new InvalidConfigurationException('Configcat sdk_key must be set'); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
$adapterDefinition->setArgument(0, $config['configcat']['sdk_key']); |
|
64
|
|
|
|
|
65
|
|
|
$serviceDefinition = $container->getDefinition(FeatureFlagService::class); |
|
66
|
|
|
$serviceDefinition->setArgument(0, new Reference(ConfigCatProvider::class)); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
private function configureRandom(ContainerBuilder $container) |
|
70
|
|
|
{ |
|
71
|
|
|
$serviceDefinition = $container->getDefinition(FeatureFlagService::class); |
|
72
|
|
|
$serviceDefinition->setArgument(0, new Reference(RandomProvider::class)); |
|
73
|
|
|
} |
|
74
|
|
|
} |