1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of phiremock-codeception-extension. |
5
|
|
|
* |
6
|
|
|
* phiremock-codeception-extension is free software: you can redistribute it and/or modify |
7
|
|
|
* it under the terms of the GNU Lesser General Public License as published by |
8
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
9
|
|
|
* (at your option) any later version. |
10
|
|
|
* |
11
|
|
|
* phiremock-codeception-extension is distributed in the hope that it will be useful, |
12
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14
|
|
|
* GNU General Public License for more details. |
15
|
|
|
* |
16
|
|
|
* You should have received a copy of the GNU General Public License |
17
|
|
|
* along with phiremock-codeception-extension. If not, see <http://www.gnu.org/licenses/>. |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
namespace Mcustiel\Phiremock\Server\Utils\Config; |
21
|
|
|
|
22
|
|
|
use DomainException; |
23
|
|
|
use Exception; |
24
|
|
|
use Mcustiel\Phiremock\Server\Cli\Options\ExpectationsDirectory; |
25
|
|
|
use Mcustiel\Phiremock\Server\Factory\Factory; |
26
|
|
|
use Mcustiel\Phiremock\Server\Utils\HomePathService; |
27
|
|
|
|
28
|
|
|
class ConfigBuilder |
29
|
|
|
{ |
30
|
|
|
private const DEFAULT_IP = '0.0.0.0'; |
31
|
|
|
private const DEFAULT_PORT = 8086; |
32
|
|
|
|
33
|
|
|
/** @var array */ |
34
|
|
|
private static $defaultConfig; |
35
|
|
|
|
36
|
|
|
/** @var Directory|null */ |
37
|
|
|
private $configPath; |
38
|
|
|
|
39
|
|
|
/** @throws Exception */ |
40
|
|
|
public function __construct(?Directory $configPath) |
41
|
|
|
{ |
42
|
|
|
if (self::$defaultConfig === null) { |
43
|
|
|
self::$defaultConfig = [ |
44
|
|
|
Config::PORT => self::DEFAULT_PORT, |
45
|
|
|
Config::IP => self::DEFAULT_IP, |
46
|
|
|
Config::EXPECTATIONS_DIR => self::getDefaultExpectationsDir()->asString(), |
47
|
|
|
Config::DEBUG => false, |
48
|
|
|
Config::FACTORY_CLASS => Factory::class, |
49
|
|
|
]; |
50
|
|
|
} |
51
|
|
|
$this->configPath = $configPath; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** @throws Exception */ |
55
|
|
|
public function build(array $cliConfig): Config |
56
|
|
|
{ |
57
|
|
|
$config = self::$defaultConfig; |
58
|
|
|
|
59
|
|
|
$fileConfiguration = $this->getConfigurationFromConfigFile(); |
60
|
|
|
$extraKeys = array_diff_key($fileConfiguration, self::$defaultConfig); |
61
|
|
|
if (!empty($extraKeys)) { |
62
|
|
|
throw new DomainException('Extra keys in configuration file: ' . implode(',', $extraKeys)); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return new Config(array_replace($config, $fileConfiguration, $cliConfig)); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** @throws Exception */ |
69
|
|
|
public static function getDefaultExpectationsDir(): ExpectationsDirectory |
70
|
|
|
{ |
71
|
|
|
return new ExpectationsDirectory( |
72
|
|
|
HomePathService::getHomePath()->getFullSubpathAsString( |
73
|
|
|
'.phiremock' . \DIRECTORY_SEPARATOR . 'expectations' |
74
|
|
|
) |
75
|
|
|
); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** @throws Exception */ |
79
|
|
|
protected function getConfigurationFromConfigFile(): array |
80
|
|
|
{ |
81
|
|
|
if ($this->configPath) { |
82
|
|
|
$configFiles = ['.phiremock', '.phiremock.dist']; |
83
|
|
|
foreach ($configFiles as $configFileName) { |
84
|
|
|
$configFilePath = $this->configPath->getFullSubpathAsString($configFileName); |
85
|
|
|
if (is_file($configFilePath)) { |
86
|
|
|
return require $configFilePath; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
throw new Exception('No config file found in: ' . $this->configPath->asString()); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return $this->searchFileAndGetConfig(); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
protected function searchFileAndGetConfig(): array |
96
|
|
|
{ |
97
|
|
|
$configFiles = [ |
98
|
|
|
__DIR__ . '/../../../../../../.phiremock', |
99
|
|
|
__DIR__ . '/../../../../../../.phiremock.dist', |
100
|
|
|
__DIR__ . '/../../../.phiremock', |
101
|
|
|
__DIR__ . '/../../../.phiremock.dist', |
102
|
|
|
getcwd() . '/.phiremock', |
103
|
|
|
getcwd() . '/.phiremock.dist', |
104
|
|
|
HomePathService::getHomePath()->getFullSubpathAsString( |
105
|
|
|
'.phiremock' . \DIRECTORY_SEPARATOR . 'config' |
106
|
|
|
), |
107
|
|
|
'.phiremock', |
108
|
|
|
'.phiremock.dist', |
109
|
|
|
]; |
110
|
|
|
foreach ($configFiles as $configFilePath) { |
111
|
|
|
if (is_file($configFilePath)) { |
112
|
|
|
return require $configFilePath; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return []; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|