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