forked from trompette/php-feature-toggles
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FeatureTogglesExtension.php
129 lines (112 loc) · 5.22 KB
/
FeatureTogglesExtension.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?php
namespace Trompette\FeatureToggles\Bundle;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Trompette\FeatureToggles\Console\ConfigureFeatureCommand;
use Trompette\FeatureToggles\Console\MigrateDBALSchemaCommand;
use Trompette\FeatureToggles\Console\ShowFeatureConfigurationCommand;
use Trompette\FeatureToggles\DBAL\OnOffStrategyConfigurationRepository;
use Trompette\FeatureToggles\DBAL\PercentageStrategyConfigurationRepository;
use Trompette\FeatureToggles\DBAL\WhitelistStrategyConfigurationRepository;
use Trompette\FeatureToggles\FeatureRegistry;
use Trompette\FeatureToggles\OnOffStrategy\OnOff;
use Trompette\FeatureToggles\ORM\SchemaSubscriber;
use Trompette\FeatureToggles\PercentageStrategy\Percentage;
use Trompette\FeatureToggles\ToggleRouter;
use Trompette\FeatureToggles\WhitelistStrategy\Whitelist;
class FeatureTogglesExtension extends Extension
{
public function load(array $configs, ContainerBuilder $container)
{
$config = $this->processConfiguration($this->getConfiguration([], $container), $configs);
$this->defineFeatureRegistry($config['declared_features'], $container);
$this->defineTogglingStrategies($config['doctrine_dbal_connection'], $container);
$this->defineToggleRouter($container);
$this->defineConsoleCommands($container);
$this->defineDoctrineEventSubscriber($container);
}
public function getConfiguration(array $config, ContainerBuilder $container)
{
return new FeatureTogglesConfiguration();
}
private function defineFeatureRegistry(array $declaredFeatures, ContainerBuilder $container)
{
$featureRegistry = $container->register(FeatureRegistry::class, FeatureRegistry::class);
foreach ($declaredFeatures as $name => $declaredFeature) {
$featureRegistry->addMethodCall(
'register',
[$name, $declaredFeature['description'], $declaredFeature['strategy']]
);
}
}
private function defineTogglingStrategies(string $doctrineDBALConnection, ContainerBuilder $container)
{
$configurationRepositories = [
'onoff' => OnOffStrategyConfigurationRepository::class,
'whitelist' => WhitelistStrategyConfigurationRepository::class,
'percentage' => PercentageStrategyConfigurationRepository::class,
];
foreach ($configurationRepositories as $key => $class) {
$container->register($class, $class)->addArgument(new Reference($doctrineDBALConnection));
}
$togglingStrategies = [
'onoff' => OnOff::class,
'whitelist' => Whitelist::class,
'percentage' => Percentage::class,
];
foreach ($togglingStrategies as $key => $class) {
$container->register($class, $class)->addArgument(new Reference($configurationRepositories[$key]));
}
}
private function defineToggleRouter(ContainerBuilder $container)
{
$definition = $container
->register(ToggleRouter::class, ToggleRouter::class)
->setPublic(true)
->addArgument(new Reference(FeatureRegistry::class))
->addArgument([
'onoff' => new Reference(OnOff::class),
'whitelist' => new Reference(Whitelist::class),
'percentage' => new Reference(Percentage::class),
])
;
if ($container->has('logger')) {
$definition
->addMethodCall('setLogger', [new Reference('logger')])
->addTag('monolog.logger', ['channel' => 'feature_toggles'])
;
}
}
private function defineConsoleCommands(ContainerBuilder $container)
{
$container
->register(MigrateDBALSchemaCommand::class, MigrateDBALSchemaCommand::class)
->addArgument(new Reference(OnOffStrategyConfigurationRepository::class))
->addArgument(new Reference(WhitelistStrategyConfigurationRepository::class))
->addArgument(new Reference(PercentageStrategyConfigurationRepository::class))
->addTag('console.command')
;
$container
->register(ShowFeatureConfigurationCommand::class, ShowFeatureConfigurationCommand::class)
->addArgument(new Reference(FeatureRegistry::class))
->addArgument(new Reference(ToggleRouter::class))
->addTag('console.command')
;
$container
->register(ConfigureFeatureCommand::class, ConfigureFeatureCommand::class)
->addArgument(new Reference(ToggleRouter::class))
->addTag('console.command')
;
}
private function defineDoctrineEventSubscriber(ContainerBuilder $container): void
{
$container
->register(SchemaSubscriber::class, SchemaSubscriber::class)
->addArgument(new Reference(OnOffStrategyConfigurationRepository::class))
->addArgument(new Reference(WhitelistStrategyConfigurationRepository::class))
->addArgument(new Reference(PercentageStrategyConfigurationRepository::class))
->addTag('doctrine.event_subscriber')
;
}
}