1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Gacela\Framework\Config\GacelaFileConfig\Factory; |
6
|
|
|
|
7
|
|
|
use Gacela\Framework\Bootstrap\GacelaConfig; |
8
|
|
|
use Gacela\Framework\Bootstrap\SetupGacela; |
9
|
|
|
use Gacela\Framework\Bootstrap\SetupGacelaInterface; |
10
|
|
|
use Gacela\Framework\Config\FileIoInterface; |
11
|
|
|
use Gacela\Framework\Config\GacelaConfigBuilder\BindingsBuilder; |
12
|
|
|
use Gacela\Framework\Config\GacelaConfigBuilder\ConfigBuilder; |
13
|
|
|
use Gacela\Framework\Config\GacelaConfigBuilder\SuffixTypesBuilder; |
14
|
|
|
use Gacela\Framework\Config\GacelaConfigFileFactoryInterface; |
15
|
|
|
use Gacela\Framework\Config\GacelaFileConfig\GacelaConfigFile; |
16
|
|
|
use Gacela\Framework\Config\GacelaFileConfig\GacelaConfigFileInterface; |
17
|
|
|
use RuntimeException; |
18
|
|
|
|
19
|
|
|
use function is_callable; |
20
|
|
|
|
21
|
|
|
final class GacelaConfigUsingGacelaPhpFileFactory implements GacelaConfigFileFactoryInterface |
22
|
|
|
{ |
23
|
23 |
|
public function __construct( |
24
|
|
|
private string $gacelaPhpPath, |
25
|
|
|
private SetupGacelaInterface $bootstrapSetup, |
26
|
|
|
private FileIoInterface $fileIo, |
27
|
|
|
) { |
28
|
23 |
|
} |
29
|
|
|
|
30
|
23 |
|
public function createGacelaFileConfig(): GacelaConfigFileInterface |
31
|
|
|
{ |
32
|
23 |
|
$projectGacelaConfig = $this->createGacelaConfig(); |
33
|
22 |
|
$projectSetupGacela = SetupGacela::fromGacelaConfig($projectGacelaConfig); |
34
|
|
|
|
35
|
22 |
|
$this->bootstrapSetup->combine($projectSetupGacela); |
36
|
|
|
|
37
|
22 |
|
$configBuilder = $this->createConfigBuilder($projectSetupGacela); |
38
|
22 |
|
$bindingsBuilder = $this->createBindingsBuilder($projectSetupGacela); |
39
|
22 |
|
$suffixTypesBuilder = $this->createSuffixTypesBuilder($projectSetupGacela); |
40
|
|
|
|
41
|
22 |
|
return (new GacelaConfigFile()) |
42
|
22 |
|
->setConfigItems($configBuilder->build()) |
43
|
22 |
|
->setBindings($bindingsBuilder->build()) |
44
|
22 |
|
->setSuffixTypes($suffixTypesBuilder->build()); |
45
|
|
|
} |
46
|
|
|
|
47
|
23 |
|
private function createGacelaConfig(): GacelaConfig |
48
|
|
|
{ |
49
|
23 |
|
$gacelaConfig = new GacelaConfig($this->bootstrapSetup->externalServices()); |
50
|
|
|
|
51
|
|
|
/** @var callable(GacelaConfig):void|null $configFn */ |
52
|
23 |
|
$configFn = $this->fileIo->include($this->gacelaPhpPath); |
53
|
|
|
|
54
|
23 |
|
if (!is_callable($configFn)) { |
55
|
1 |
|
throw new RuntimeException('`gacela.php` file should return a `callable(GacelaConfig)`'); |
56
|
|
|
} |
57
|
|
|
|
58
|
22 |
|
$configFn($gacelaConfig); |
59
|
|
|
|
60
|
22 |
|
return $gacelaConfig; |
61
|
|
|
} |
62
|
|
|
|
63
|
22 |
|
private function createConfigBuilder(SetupGacelaInterface $setupGacela): ConfigBuilder |
64
|
|
|
{ |
65
|
22 |
|
return $setupGacela->buildConfig(new ConfigBuilder()); |
66
|
|
|
} |
67
|
|
|
|
68
|
22 |
|
private function createBindingsBuilder(SetupGacelaInterface $setupGacela): BindingsBuilder |
69
|
|
|
{ |
70
|
22 |
|
return $setupGacela->buildBindings(new BindingsBuilder(), $this->bootstrapSetup->externalServices()); |
71
|
|
|
} |
72
|
|
|
|
73
|
22 |
|
private function createSuffixTypesBuilder(SetupGacelaInterface $setupGacela): SuffixTypesBuilder |
74
|
|
|
{ |
75
|
22 |
|
return $setupGacela->buildSuffixTypes(new SuffixTypesBuilder()); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|