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