|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Gacela\Framework\Config; |
|
6
|
|
|
|
|
7
|
|
|
use Gacela\Framework\AbstractConfigGacela; |
|
8
|
|
|
use Gacela\Framework\Config\GacelaFileConfig\GacelaConfigFile; |
|
9
|
|
|
use RuntimeException; |
|
10
|
|
|
use function is_array; |
|
11
|
|
|
use function is_callable; |
|
12
|
|
|
|
|
13
|
|
|
final class GacelaConfigFileFactory implements GacelaConfigFileFactoryInterface |
|
14
|
|
|
{ |
|
15
|
|
|
private string $applicationRootDir; |
|
16
|
|
|
private array $globalServices; |
|
17
|
|
|
private string $gacelaPhpConfigFilename; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @param array<string, mixed> $globalServices |
|
21
|
|
|
*/ |
|
22
|
17 |
|
public function __construct( |
|
23
|
|
|
string $applicationRootDir, |
|
24
|
|
|
array $globalServices, |
|
25
|
|
|
string $gacelaPhpConfigFilename |
|
26
|
|
|
) { |
|
27
|
17 |
|
$this->applicationRootDir = $applicationRootDir; |
|
28
|
17 |
|
$this->globalServices = $globalServices; |
|
29
|
17 |
|
$this->gacelaPhpConfigFilename = $gacelaPhpConfigFilename; |
|
30
|
17 |
|
} |
|
31
|
|
|
|
|
32
|
17 |
|
public function createGacelaFileConfig(): GacelaConfigFile |
|
33
|
|
|
{ |
|
34
|
17 |
|
$gacelaPhpPath = $this->applicationRootDir . '/' . $this->gacelaPhpConfigFilename; |
|
35
|
|
|
|
|
36
|
17 |
|
if (!is_file($gacelaPhpPath)) { |
|
37
|
12 |
|
return GacelaConfigFile::withDefaults(); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** @var array|callable|mixed $configGacela */ |
|
41
|
5 |
|
$configGacela = include $gacelaPhpPath; |
|
42
|
|
|
|
|
43
|
5 |
|
if (is_callable($configGacela)) { |
|
44
|
|
|
/** @var AbstractConfigGacela $configGacelaClass */ |
|
45
|
4 |
|
$configGacelaClass = $configGacela($this->globalServices); |
|
46
|
4 |
|
if (!is_subclass_of($configGacelaClass, AbstractConfigGacela::class)) { |
|
47
|
|
|
throw new RuntimeException('Your anonymous class must extends AbstractConfigGacela'); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** @psalm-suppress ArgumentTypeCoercion */ |
|
51
|
4 |
|
return GacelaConfigFile::fromArray([ |
|
52
|
4 |
|
'config' => $configGacelaClass->config(), |
|
53
|
4 |
|
'mapping-interfaces' => $configGacelaClass->mappingInterfaces(), |
|
54
|
|
|
]); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
1 |
|
if (is_array($configGacela)) { |
|
58
|
1 |
|
trigger_error('Use a function that returns an anonymous class that extends AbstractConfigGacela. Check the documentation for more info. Array is deprecated.', E_USER_DEPRECATED); |
|
59
|
|
|
/** @psalm-suppress MixedArgumentTypeCoercion */ |
|
60
|
|
|
return GacelaConfigFile::fromArray($configGacela); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
throw new RuntimeException('Create a function that returns an anonymous class that extends AbstractConfigGacela'); |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|