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