1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Pheature\Community\Symfony\DependencyInjection; |
6
|
|
|
|
7
|
|
|
use Doctrine\DBAL\Connection; |
8
|
|
|
use Pheature\Core\Toggle\Write\FeatureRepository; |
9
|
|
|
use Pheature\Crud\Psr11\Toggle\FeatureRepositoryFactory; |
10
|
|
|
use Pheature\Crud\Psr11\Toggle\ToggleConfig; |
11
|
|
|
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
12
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
13
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
14
|
|
|
use Webmozart\Assert\Assert; |
15
|
|
|
|
16
|
|
|
final class FeatureRepositoryFactoryPass implements CompilerPassInterface |
17
|
2 |
|
{ |
18
|
|
|
public function process(ContainerBuilder $container): void |
19
|
|
|
{ |
20
|
2 |
|
/** @var array<array<mixed>> $pheatureFlagsConfig */ |
21
|
2 |
|
$pheatureFlagsConfig = $container->getExtensionConfig('pheature_flags'); |
22
|
|
|
$mergedConfig = array_merge(...$pheatureFlagsConfig); |
23
|
2 |
|
|
24
|
2 |
|
$repository = $container->register(FeatureRepository::class, FeatureRepository::class) |
25
|
2 |
|
->setAutowired(false) |
26
|
2 |
|
->setLazy(true) |
27
|
2 |
|
->setFactory([FeatureRepositoryFactory::class, 'create']) |
28
|
|
|
->addArgument(new Reference(ToggleConfig::class)); |
29
|
2 |
|
|
30
|
1 |
|
Assert::keyExists($mergedConfig, 'driver'); |
31
|
|
|
Assert::string($mergedConfig['driver']); |
32
|
1 |
|
Assert::keyExists($mergedConfig, 'driver_options'); |
33
|
|
|
Assert::isArray($mergedConfig['driver_options']); |
34
|
2 |
|
|
35
|
|
|
if ( |
36
|
|
|
ToggleConfig::DRIVER_DBAL === $mergedConfig['driver'] |
37
|
|
|
|| true === in_array(ToggleConfig::DRIVER_DBAL, $mergedConfig['driver_options'], true) |
38
|
|
|
) { |
39
|
|
|
$repository->addArgument(new Reference(Connection::class)); |
40
|
|
|
} else { |
41
|
|
|
$repository->addArgument(null); |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
|