|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Teebot\Configuration; |
|
6
|
|
|
|
|
7
|
|
|
use Symfony\Component\Config\{ |
|
8
|
|
|
Definition\Processor, |
|
9
|
|
|
Definition\ConfigurationInterface, |
|
10
|
|
|
Exception\FileLoaderLoadException, |
|
11
|
|
|
FileLocator |
|
12
|
|
|
}; |
|
13
|
|
|
use Symfony\Component\Yaml\Yaml; |
|
14
|
|
|
use Dotenv\{ |
|
15
|
|
|
Dotenv, |
|
16
|
|
|
Exception\InvalidPathException |
|
17
|
|
|
}; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Abstract configuration loader |
|
21
|
|
|
* |
|
22
|
|
|
* @package Teebot\Configuration |
|
23
|
|
|
*/ |
|
24
|
|
|
abstract class AbstractLoader |
|
25
|
|
|
{ |
|
26
|
|
|
protected const FILE_NAME = 'config%s.yml'; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var string $path |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $path; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var string $fileName |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $fileName; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param string $path |
|
40
|
|
|
*/ |
|
41
|
|
|
public function __construct(string $path) |
|
42
|
|
|
{ |
|
43
|
|
|
$this->initEnv($path); |
|
44
|
|
|
|
|
45
|
|
|
$this->path = $path; |
|
46
|
|
|
$this->fileName = $this->getFileName(); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Loads configuration |
|
51
|
|
|
* |
|
52
|
|
|
* @return ContainerInterface |
|
53
|
|
|
*/ |
|
54
|
|
|
public function load(): ContainerInterface |
|
55
|
|
|
{ |
|
56
|
|
|
$configFile = $this->getConfigFile(); |
|
57
|
|
|
$data = Yaml::parse(file_get_contents($configFile)); |
|
58
|
|
|
|
|
59
|
|
|
return $this->loadFromArray($data); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Loads configuration from array |
|
64
|
|
|
* |
|
65
|
|
|
* @param array $configData |
|
66
|
|
|
* |
|
67
|
|
|
* @return ContainerInterface |
|
68
|
|
|
*/ |
|
69
|
|
|
public function loadFromArray(array $configData): ContainerInterface |
|
70
|
|
|
{ |
|
71
|
|
|
$config = $this->processConfig($configData); |
|
72
|
|
|
|
|
73
|
|
|
return $this->initContainer($config); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Returns config file name based on the current environment |
|
78
|
|
|
* |
|
79
|
|
|
* @return string |
|
80
|
|
|
*/ |
|
81
|
|
|
protected function getFileName(): string |
|
82
|
|
|
{ |
|
83
|
|
|
$env = getenv('ENV') ? '_' . getenv('ENV') : ''; |
|
84
|
|
|
|
|
85
|
|
|
return sprintf(static::FILE_NAME, $env); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Initializes and loads the DotEnv, suppress the DotEnv exception to continue loading process |
|
90
|
|
|
* |
|
91
|
|
|
* @param string $path |
|
92
|
|
|
*/ |
|
93
|
|
|
protected function initEnv(string $path) |
|
94
|
|
|
{ |
|
95
|
|
|
try { |
|
96
|
|
|
$dotenv = new Dotenv($path); |
|
97
|
|
|
$dotenv->load(); |
|
98
|
|
|
} catch (InvalidPathException $e) { |
|
|
|
|
|
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Returns path to config file |
|
104
|
|
|
* |
|
105
|
|
|
* @return string |
|
106
|
|
|
* |
|
107
|
|
|
* @throws FileLoaderLoadException |
|
108
|
|
|
*/ |
|
109
|
|
|
protected function getConfigFile(): string |
|
110
|
|
|
{ |
|
111
|
|
|
$locator = new FileLocator($this->path); |
|
112
|
|
|
$configFile = $locator->locate($this->fileName, null, true); |
|
113
|
|
|
|
|
114
|
|
|
if (!is_readable($configFile)) { |
|
|
|
|
|
|
115
|
|
|
throw new FileLoaderLoadException('Config file is not readable!'); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
return $configFile; |
|
|
|
|
|
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Processes the config |
|
123
|
|
|
* |
|
124
|
|
|
* @param array $data |
|
125
|
|
|
* |
|
126
|
|
|
* @return array |
|
127
|
|
|
*/ |
|
128
|
|
|
protected function processConfig(array $data): array |
|
129
|
|
|
{ |
|
130
|
|
|
$processor = new Processor(); |
|
131
|
|
|
$configuration = $this->getConfiguration(); |
|
132
|
|
|
$processedConfig = $processor->processConfiguration($configuration, $data); |
|
133
|
|
|
|
|
134
|
|
|
return $processedConfig; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* @return ConfigurationInterface |
|
139
|
|
|
*/ |
|
140
|
|
|
abstract protected function getConfiguration(): ConfigurationInterface; |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* @param array $config |
|
144
|
|
|
* |
|
145
|
|
|
* @return ContainerInterface |
|
146
|
|
|
*/ |
|
147
|
|
|
abstract protected function initContainer(array $config): ContainerInterface; |
|
148
|
|
|
} |
|
149
|
|
|
|