1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Gacela\Framework\Config; |
6
|
|
|
|
7
|
|
|
use Gacela\Framework\AbstractFactory; |
8
|
|
|
use Gacela\Framework\Bootstrap\SetupGacelaInterface; |
9
|
|
|
use Gacela\Framework\Config\GacelaFileConfig\Factory\GacelaConfigFromBootstrapFactory; |
10
|
|
|
use Gacela\Framework\Config\GacelaFileConfig\Factory\GacelaConfigUsingGacelaPhpFileFactory; |
11
|
|
|
use Gacela\Framework\Config\GacelaFileConfig\GacelaConfigFileInterface; |
12
|
|
|
use Gacela\Framework\Config\PathNormalizer\AbsolutePathNormalizer; |
13
|
|
|
use Gacela\Framework\Config\PathNormalizer\WithoutSuffixAbsolutePathStrategy; |
14
|
|
|
use Gacela\Framework\Config\PathNormalizer\WithSuffixAbsolutePathStrategy; |
15
|
|
|
|
16
|
|
|
final class ConfigFactory extends AbstractFactory |
17
|
|
|
{ |
18
|
|
|
private const GACELA_PHP_CONFIG_FILENAME = 'gacela'; |
19
|
|
|
private const GACELA_PHP_CONFIG_EXTENSION = '.php'; |
20
|
|
|
|
21
|
|
|
private string $appRootDir; |
22
|
|
|
|
23
|
|
|
private SetupGacelaInterface $setup; |
24
|
|
|
|
25
|
51 |
|
public function __construct(string $appRootDir, SetupGacelaInterface $setup) |
26
|
|
|
{ |
27
|
51 |
|
$this->appRootDir = $appRootDir; |
28
|
51 |
|
$this->setup = $setup; |
29
|
|
|
} |
30
|
|
|
|
31
|
47 |
|
public function createConfigLoader(): ConfigLoader |
32
|
|
|
{ |
33
|
47 |
|
return new ConfigLoader( |
34
|
47 |
|
$this->createGacelaFileConfig(), |
35
|
47 |
|
$this->createPathFinder(), |
36
|
47 |
|
$this->createPathNormalizer(), |
37
|
|
|
); |
38
|
|
|
} |
39
|
|
|
|
40
|
51 |
|
public function createGacelaFileConfig(): GacelaConfigFileInterface |
41
|
|
|
{ |
42
|
51 |
|
$gacelaPhpPath = $this->getGacelaPhpPath(); |
43
|
51 |
|
$fileIo = $this->createFileIo(); |
44
|
|
|
|
45
|
51 |
|
if ($fileIo->existsFile($gacelaPhpPath)) { |
46
|
15 |
|
$factoryFromGacelaPhp = new GacelaConfigUsingGacelaPhpFileFactory($gacelaPhpPath, $this->setup, $fileIo); |
47
|
15 |
|
$gacelaSetupFromGacelaPhp = $factoryFromGacelaPhp->createGacelaFileConfig(); |
48
|
|
|
} |
49
|
|
|
|
50
|
51 |
|
$factoryFromBootstrap = new GacelaConfigFromBootstrapFactory($this->setup); |
51
|
51 |
|
$gacelaSetupFromBootstrap = $factoryFromBootstrap->createGacelaFileConfig(); |
52
|
|
|
|
53
|
51 |
|
if (isset($gacelaSetupFromGacelaPhp) && $gacelaSetupFromGacelaPhp instanceof GacelaConfigFileInterface) { |
54
|
15 |
|
return $gacelaSetupFromBootstrap->combine($gacelaSetupFromGacelaPhp); |
55
|
|
|
} |
56
|
|
|
|
57
|
36 |
|
return $gacelaSetupFromBootstrap; |
58
|
|
|
} |
59
|
|
|
|
60
|
51 |
|
private function createFileIo(): FileIoInterface |
61
|
|
|
{ |
62
|
51 |
|
return new FileIo(); |
63
|
|
|
} |
64
|
|
|
|
65
|
47 |
|
private function createPathFinder(): PathFinderInterface |
66
|
|
|
{ |
67
|
47 |
|
return new PathFinder(); |
68
|
|
|
} |
69
|
|
|
|
70
|
47 |
|
private function createPathNormalizer(): PathNormalizerInterface |
71
|
|
|
{ |
72
|
47 |
|
return new AbsolutePathNormalizer([ |
73
|
47 |
|
AbsolutePathNormalizer::WITHOUT_SUFFIX => new WithoutSuffixAbsolutePathStrategy($this->appRootDir), |
74
|
47 |
|
AbsolutePathNormalizer::WITH_SUFFIX => new WithSuffixAbsolutePathStrategy($this->appRootDir, $this->env()), |
75
|
|
|
]); |
76
|
|
|
} |
77
|
|
|
|
78
|
51 |
|
private function env(): string |
79
|
|
|
{ |
80
|
51 |
|
return getenv('APP_ENV') ?: ''; |
81
|
|
|
} |
82
|
|
|
|
83
|
51 |
|
private function getGacelaPhpPath(): string |
84
|
|
|
{ |
85
|
51 |
|
if ($this->env() === '') { |
86
|
47 |
|
return sprintf( |
87
|
|
|
'%s/%s%s', |
88
|
47 |
|
$this->appRootDir, |
89
|
|
|
self::GACELA_PHP_CONFIG_FILENAME, |
90
|
|
|
self::GACELA_PHP_CONFIG_EXTENSION |
91
|
|
|
); |
92
|
|
|
} |
93
|
|
|
|
94
|
4 |
|
return sprintf( |
95
|
|
|
'%s/%s-%s%s', |
96
|
4 |
|
$this->appRootDir, |
97
|
|
|
self::GACELA_PHP_CONFIG_FILENAME, |
98
|
4 |
|
$this->env(), |
99
|
|
|
self::GACELA_PHP_CONFIG_EXTENSION |
100
|
|
|
); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|