|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Bone\Server; |
|
4
|
|
|
|
|
5
|
|
|
use Bone\Traits\HasAttributesTrait; |
|
6
|
|
|
|
|
7
|
|
|
class Environment |
|
8
|
|
|
{ |
|
9
|
|
|
use HasAttributesTrait; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Environment constructor. |
|
13
|
|
|
* @param array $serverGlobals |
|
14
|
|
|
*/ |
|
15
|
|
|
public function __construct(array $serverGlobals) |
|
16
|
|
|
{ |
|
17
|
|
|
$this->setAttributes($serverGlobals); |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @param string $configFolder |
|
22
|
|
|
* @param string $applicationEnvironment |
|
23
|
|
|
* @return array |
|
24
|
|
|
*/ |
|
25
|
|
|
public function fetchConfig(string $configFolder, string $applicationEnvironment) : array |
|
26
|
|
|
{ |
|
27
|
|
|
$config = $this->loadLegacyConfig($configFolder); |
|
28
|
|
|
|
|
29
|
|
|
if (!empty($applicationEnvironment)) { |
|
30
|
|
|
// die(var_dump($config, $configFolder, $applicationEnvironment)); |
|
31
|
|
|
$config = $this->loadEnvironmentConfig($configFolder, $applicationEnvironment, $config); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
return $config; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param string $configFolder |
|
39
|
|
|
* @return array |
|
40
|
|
|
*/ |
|
41
|
|
|
private function loadLegacyConfig(string $configFolder) : array |
|
42
|
|
|
{ |
|
43
|
|
|
$config = []; |
|
44
|
|
|
|
|
45
|
|
|
// load the config.php if it exists |
|
46
|
|
|
$path = $configFolder . '/config.php'; |
|
47
|
|
|
if (file_exists($path)) { |
|
48
|
|
|
$config = $this->loadInConfig($config, $path); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
return $config; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @param string $configFolder |
|
56
|
|
|
* @param string $applicationEnvironment |
|
57
|
|
|
* @param array $config |
|
58
|
|
|
* @return array |
|
59
|
|
|
*/ |
|
60
|
|
|
private function loadEnvironmentConfig(string $configFolder, string $applicationEnvironment, array $config) : array |
|
61
|
|
|
{ |
|
62
|
|
|
// check environment config folder exists |
|
63
|
|
|
$path = $configFolder . '/' . $applicationEnvironment; |
|
64
|
|
|
if (file_exists($path)) { |
|
65
|
|
|
$files = glob($path . '/*.php'); |
|
66
|
|
|
foreach ($files as $file) { |
|
67
|
|
|
$config = $this->loadInConfig($config, $file); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
return $config; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @param array $config |
|
76
|
|
|
* @param string $file |
|
77
|
|
|
* @return array |
|
78
|
|
|
*/ |
|
79
|
|
|
private function loadInConfig(array $config, string $file) : array |
|
80
|
|
|
{ |
|
81
|
|
|
$moreConfig = require_once $file; |
|
82
|
|
|
if (is_array($moreConfig)) { |
|
83
|
|
|
$config = array_merge($config, $moreConfig); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
return $config; |
|
87
|
|
|
} |
|
88
|
|
|
} |