|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Enjoys\Config; |
|
6
|
|
|
|
|
7
|
|
|
use Enjoys\Config\ValueHandler\DefinedConstantsValueHandler; |
|
8
|
|
|
use Enjoys\Config\ValueHandler\EnvValueHandler; |
|
9
|
|
|
use Psr\Log\LoggerInterface; |
|
10
|
|
|
use Psr\Log\NullLogger; |
|
11
|
|
|
|
|
12
|
|
|
abstract class Parse implements ParseInterface |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var array<string, mixed> |
|
16
|
|
|
*/ |
|
17
|
|
|
private array $options = []; |
|
18
|
|
|
|
|
19
|
|
|
private ?string $configSource = null; |
|
20
|
|
|
protected LoggerInterface $logger; |
|
21
|
|
|
|
|
22
|
|
|
private array $valueHandlers = [ |
|
23
|
|
|
EnvValueHandler::class, |
|
24
|
|
|
DefinedConstantsValueHandler::class |
|
25
|
|
|
]; |
|
26
|
|
|
|
|
27
|
|
|
public function __construct() |
|
28
|
|
|
{ |
|
29
|
23 |
|
$this->logger = new NullLogger(); |
|
30
|
|
|
} |
|
31
|
23 |
|
|
|
32
|
|
|
public function setLogger(LoggerInterface $logger) |
|
33
|
|
|
{ |
|
34
|
15 |
|
$this->logger = $logger; |
|
35
|
|
|
} |
|
36
|
15 |
|
|
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
22 |
|
* @param array<string, mixed> $options |
|
40
|
|
|
* @return $this |
|
41
|
22 |
|
* @psalm-suppress MixedAssignment |
|
42
|
|
|
*/ |
|
43
|
|
|
public function setOptions(array $options = []): self |
|
44
|
|
|
{ |
|
45
|
|
|
foreach ($options as $key => $value) { |
|
46
|
|
|
$this->setOption($key, $value); |
|
47
|
23 |
|
} |
|
48
|
|
|
return $this; |
|
49
|
23 |
|
} |
|
50
|
1 |
|
|
|
51
|
1 |
|
/** |
|
52
|
|
|
* @param mixed $value |
|
53
|
|
|
*/ |
|
54
|
22 |
|
public function setOption(string $key, $value): self |
|
55
|
17 |
|
{ |
|
56
|
|
|
$this->options[$key] = $value; |
|
57
|
|
|
return $this; |
|
58
|
5 |
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
22 |
|
* @param mixed $defaults |
|
62
|
|
|
* @return mixed |
|
63
|
|
|
*/ |
|
64
|
22 |
|
public function getOption(string $key, $defaults = null) |
|
65
|
22 |
|
{ |
|
66
|
|
|
if (array_key_exists($key, $this->options)) { |
|
67
|
|
|
return $this->options[$key]; |
|
68
|
22 |
|
} |
|
69
|
|
|
return $defaults; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @return array<string, mixed> |
|
74
|
|
|
*/ |
|
75
|
5 |
|
public function getOptions(): array |
|
76
|
|
|
{ |
|
77
|
5 |
|
return $this->options; |
|
78
|
5 |
|
} |
|
79
|
|
|
|
|
80
|
|
|
|
|
81
|
|
|
public function addConfigSource(string $source): void |
|
82
|
|
|
{ |
|
83
|
|
|
$this->configSource = $source; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @return array|false|null |
|
88
|
|
|
*/ |
|
89
|
|
|
public function parse() |
|
90
|
|
|
{ |
|
91
|
|
|
if (is_null($this->configSource)) { |
|
92
|
|
|
$this->logger->notice('Add data for parsing'); |
|
93
|
|
|
return null; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
if (!is_file($this->configSource)) { |
|
97
|
|
|
return $this->parseString($this->applyValueHandlers($this->configSource)); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
return $this->parseFile($this->configSource); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
private function applyValueHandlers(string $data): string |
|
104
|
|
|
{ |
|
105
|
|
|
/** @var class-string<ValueHandlerInterface> $valueHandler */ |
|
106
|
|
|
foreach ($this->valueHandlers as $valueHandler) { |
|
107
|
|
|
$data = (new $valueHandler())->handle($data); |
|
108
|
|
|
} |
|
109
|
|
|
// var_dump($data); |
|
110
|
|
|
return $data; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* @param string $filename |
|
115
|
|
|
* @return array|null|false |
|
116
|
|
|
*/ |
|
117
|
|
|
private function parseFile(string $filename) |
|
118
|
|
|
{ |
|
119
|
|
|
$data = file_get_contents($filename); |
|
120
|
|
|
return $this->parseString($this->applyValueHandlers($data)); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* @param string $input |
|
125
|
|
|
* @return array|null|false |
|
126
|
|
|
*/ |
|
127
|
|
|
abstract protected function parseString(string $input); |
|
128
|
|
|
|
|
129
|
|
|
|
|
130
|
|
|
} |
|
131
|
|
|
|