|
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\AppConfigBuilder; |
|
12
|
|
|
use Gacela\Framework\Config\GacelaConfigBuilder\BindingsBuilder; |
|
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
|
|
|
public function __construct( |
|
24
|
|
|
private readonly string $gacelaPhpPath, |
|
25
|
|
|
private readonly SetupGacelaInterface $bootstrapSetup, |
|
26
|
|
|
private readonly FileIoInterface $fileIo, |
|
27
|
|
|
) { |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
public function createGacelaFileConfig(): GacelaConfigFileInterface |
|
31
|
|
|
{ |
|
32
|
|
|
$projectGacelaConfig = $this->createGacelaConfig(); |
|
33
|
|
|
$projectSetupGacela = SetupGacela::fromGacelaConfig($projectGacelaConfig); |
|
34
|
|
|
|
|
35
|
|
|
$this->bootstrapSetup->merge($projectSetupGacela); |
|
36
|
|
|
|
|
37
|
|
|
$configBuilder = $this->createConfigBuilder($projectSetupGacela); |
|
38
|
|
|
$bindingsBuilder = $this->createBindingsBuilder($projectSetupGacela); |
|
39
|
|
|
$suffixTypesBuilder = $this->createSuffixTypesBuilder($projectSetupGacela); |
|
40
|
|
|
|
|
41
|
|
|
return (new GacelaConfigFile()) |
|
42
|
|
|
->setConfigItems($configBuilder->build()) |
|
43
|
|
|
->setBindings($bindingsBuilder->build()) |
|
44
|
|
|
->setSuffixTypes($suffixTypesBuilder->build()); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
private function createGacelaConfig(): GacelaConfig |
|
48
|
|
|
{ |
|
49
|
|
|
$gacelaConfig = new GacelaConfig($this->bootstrapSetup->externalServices()); |
|
50
|
|
|
|
|
51
|
|
|
/** @var callable(GacelaConfig):void|null $configFn */ |
|
52
|
|
|
$configFn = $this->fileIo->include($this->gacelaPhpPath); |
|
53
|
|
|
|
|
54
|
|
|
if (!is_callable($configFn)) { |
|
55
|
|
|
throw new RuntimeException('`gacela.php` file should return a `callable(GacelaConfig)`'); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
$configFn($gacelaConfig); |
|
59
|
|
|
|
|
60
|
|
|
return $gacelaConfig; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
private function createConfigBuilder(SetupGacelaInterface $setupGacela): AppConfigBuilder |
|
64
|
|
|
{ |
|
65
|
|
|
return $setupGacela->buildAppConfig(new AppConfigBuilder()); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
private function createBindingsBuilder(SetupGacelaInterface $setupGacela): BindingsBuilder |
|
69
|
|
|
{ |
|
70
|
|
|
return $setupGacela->buildBindings(new BindingsBuilder(), $this->bootstrapSetup->externalServices()); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
private function createSuffixTypesBuilder(SetupGacelaInterface $setupGacela): SuffixTypesBuilder |
|
74
|
|
|
{ |
|
75
|
|
|
return $setupGacela->buildSuffixTypes(new SuffixTypesBuilder()); |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|