|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Pheature\Community\Symfony\DependencyInjection; |
|
6
|
|
|
|
|
7
|
|
|
use Pheature\Core\Toggle\Read\ChainToggleStrategyFactory; |
|
8
|
|
|
use Pheature\Core\Toggle\Read\SegmentFactory; |
|
9
|
|
|
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
|
10
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
11
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
|
12
|
|
|
use Webmozart\Assert\Assert; |
|
13
|
|
|
|
|
14
|
|
|
final class ToggleStrategyFactoryPass implements CompilerPassInterface |
|
15
|
1 |
|
{ |
|
16
|
|
|
public function process(ContainerBuilder $container): void |
|
17
|
|
|
{ |
|
18
|
1 |
|
/** @var array<array<mixed>> $pheatureFlagsConfig */ |
|
19
|
1 |
|
$pheatureFlagsConfig = $container->getExtensionConfig('pheature_flags'); |
|
20
|
|
|
$mergedConfig = array_merge(...$pheatureFlagsConfig); |
|
21
|
1 |
|
|
|
22
|
1 |
|
$toggleStrategyFactory = $container->register( |
|
23
|
1 |
|
ChainToggleStrategyFactory::class, |
|
24
|
|
|
ChainToggleStrategyFactory::class |
|
25
|
1 |
|
) |
|
26
|
1 |
|
->setAutowired(false) |
|
27
|
1 |
|
->setLazy(true) |
|
28
|
|
|
->addArgument(new Reference(SegmentFactory::class)); |
|
29
|
|
|
|
|
30
|
1 |
|
Assert::keyExists($mergedConfig, 'strategy_types'); |
|
31
|
1 |
|
Assert::isArray($mergedConfig['strategy_types']); |
|
32
|
1 |
|
|
|
33
|
1 |
|
/** @var array<string, string> $strategyDefinition */ |
|
34
|
|
|
foreach ($mergedConfig['strategy_types'] as $strategyDefinition) { |
|
35
|
1 |
|
Assert::keyExists($strategyDefinition, 'type'); |
|
36
|
|
|
Assert::string($strategyDefinition['type']); |
|
37
|
1 |
|
Assert::keyExists($strategyDefinition, 'factory_id'); |
|
38
|
|
|
Assert::string($strategyDefinition['factory_id']); |
|
39
|
|
|
$container->register($strategyDefinition['type'], $strategyDefinition['factory_id']) |
|
40
|
|
|
->setAutowired(false) |
|
41
|
|
|
->setLazy(true); |
|
42
|
|
|
|
|
43
|
|
|
$toggleStrategyFactory->addArgument(new Reference($strategyDefinition['type'])); |
|
44
|
|
|
} |
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
|
|