-
Notifications
You must be signed in to change notification settings - Fork 2
/
FeatureTogglesExtension.php
134 lines (117 loc) · 5.76 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
130
131
132
133
134
<?php
namespace Trompette\FeatureToggles\Bundle;
use Doctrine\ORM\Tools\ToolEvents;
use Symfony\Component\DependencyInjection\Alias;
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\FeatureDefinition;
use Trompette\FeatureToggles\FeatureRegistry;
use Trompette\FeatureToggles\OnOffStrategy\OnOff;
use Trompette\FeatureToggles\ORM\SchemaConfigurationListener;
use Trompette\FeatureToggles\PercentageStrategy\Percentage;
use Trompette\FeatureToggles\ToggleRouter;
use Trompette\FeatureToggles\ToggleRouterInterface;
use Trompette\FeatureToggles\WhitelistStrategy\Whitelist;
final class FeatureTogglesExtension extends Extension
{
public function load(array $configs, ContainerBuilder $container): void
{
$config = $this->processConfiguration(new FeatureTogglesConfiguration(), $configs);
$this->defineFeatureRegistry($config['declared_features'], $container);
$this->defineTogglingStrategies($config['doctrine_dbal_connection'], $container);
$this->defineToggleRouter($container);
$this->defineConsoleCommands($container);
$this->defineDoctrineEventListener($container);
}
/**
* @param array<string, array{description: string, strategy: string}> $declaredFeatures
*/
private function defineFeatureRegistry(array $declaredFeatures, ContainerBuilder $container): void
{
$featureRegistry = $container->register(FeatureRegistry::class, FeatureRegistry::class);
foreach ($declaredFeatures as $name => ['description' => $description, 'strategy' => $strategy]) {
$id = sprintf('declared_features.%s', $name);
$container->register($id, FeatureDefinition::class)->setArguments([$name, $description, $strategy]);
$featureRegistry->addMethodCall('register', [new Reference($id)]);
}
}
private function defineTogglingStrategies(string $doctrineDBALConnection, ContainerBuilder $container): void
{
$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): void
{
$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'])
;
}
$container->addAliases([ToggleRouterInterface::class => new Alias(ToggleRouter::class, true)]);
}
private function defineConsoleCommands(ContainerBuilder $container): void
{
$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 defineDoctrineEventListener(ContainerBuilder $container): void
{
if (class_exists(ToolEvents::class)) {
$container
->register(SchemaConfigurationListener::class, SchemaConfigurationListener::class)
->addArgument(new Reference(OnOffStrategyConfigurationRepository::class))
->addArgument(new Reference(WhitelistStrategyConfigurationRepository::class))
->addArgument(new Reference(PercentageStrategyConfigurationRepository::class))
->addTag('doctrine.event_listener', ['event' => ToolEvents::postGenerateSchema])
;
}
}
}