|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Pheature\Crud\Psr11\Toggle; |
|
6
|
|
|
|
|
7
|
|
|
use Doctrine\DBAL\Connection; |
|
8
|
|
|
use InvalidArgumentException; |
|
9
|
|
|
use Pheature\Core\Toggle\Read\ChainToggleStrategyFactory; |
|
|
|
|
|
|
10
|
|
|
use Pheature\Core\Toggle\Read\FeatureFinder; |
|
11
|
|
|
use Pheature\Dbal\Toggle\Read\DbalFeatureFactory; |
|
12
|
|
|
use Pheature\Dbal\Toggle\Read\DbalFeatureFinder; |
|
13
|
|
|
use Pheature\InMemory\Toggle\InMemoryConfig; |
|
14
|
|
|
use Pheature\InMemory\Toggle\InMemoryFeatureFactory; |
|
15
|
|
|
use Pheature\InMemory\Toggle\InMemoryFeatureFinder; |
|
16
|
|
|
use Psr\Container\ContainerInterface; |
|
17
|
|
|
|
|
18
|
|
|
final class FeatureFinderFactory |
|
19
|
|
|
{ |
|
20
|
2 |
|
public function __invoke(ContainerInterface $container): FeatureFinder |
|
21
|
|
|
{ |
|
22
|
|
|
/** @var ToggleConfig $config */ |
|
23
|
2 |
|
$config = $container->get(ToggleConfig::class); |
|
24
|
|
|
/** @var ChainToggleStrategyFactory $chainToggleStrategyFactory */ |
|
25
|
2 |
|
$chainToggleStrategyFactory = $container->get(ChainToggleStrategyFactory::class); |
|
26
|
|
|
|
|
27
|
2 |
|
if (ToggleConfig::DRIVER_DBAL === $config->driver()) { |
|
28
|
|
|
/** @var ?Connection $connection */ |
|
29
|
1 |
|
$connection = $container->get(Connection::class); |
|
30
|
1 |
|
return self::create($config, $chainToggleStrategyFactory, $connection); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
1 |
|
return self::create($config, $chainToggleStrategyFactory, null); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
4 |
|
public static function create( |
|
37
|
|
|
ToggleConfig $config, |
|
38
|
|
|
ChainToggleStrategyFactory $chainToggleStrategyFactory, |
|
39
|
|
|
?Connection $connection |
|
40
|
|
|
): FeatureFinder { |
|
41
|
|
|
|
|
42
|
4 |
|
$driver = $config->driver(); |
|
43
|
|
|
|
|
44
|
4 |
|
if (ToggleConfig::DRIVER_IN_MEMORY === $driver) { |
|
45
|
2 |
|
return new InMemoryFeatureFinder( |
|
46
|
2 |
|
new InMemoryConfig($config->toggles()), |
|
47
|
2 |
|
new InMemoryFeatureFactory( |
|
48
|
2 |
|
$chainToggleStrategyFactory |
|
49
|
|
|
) |
|
50
|
|
|
); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
2 |
|
if (ToggleConfig::DRIVER_DBAL === $driver) { |
|
54
|
|
|
/** @var Connection $connection */ |
|
55
|
2 |
|
return new DbalFeatureFinder($connection, new DbalFeatureFactory($chainToggleStrategyFactory)); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
throw new InvalidArgumentException('Valid driver required'); |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: