1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App; |
4
|
|
|
|
5
|
|
|
use App\Interfaces\IConfig; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* App\Config |
9
|
|
|
* |
10
|
|
|
* is a config manager |
11
|
|
|
* |
12
|
|
|
* @author pierrefromager |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
class Config implements IConfig |
16
|
|
|
{ |
17
|
|
|
|
18
|
|
|
protected $path; |
19
|
|
|
protected $env; |
20
|
|
|
protected $settings; |
21
|
|
|
protected static $instance; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* instanciate |
25
|
|
|
* |
26
|
|
|
* @param string $env |
27
|
|
|
*/ |
28
|
|
|
public function __construct(string $env, string $path) |
29
|
|
|
{ |
30
|
|
|
$this->path = $path; |
31
|
|
|
$this->setEnv($env); |
32
|
|
|
$this->load(); |
33
|
|
|
return $this; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* set config environment |
38
|
|
|
* |
39
|
|
|
* @param string $env |
40
|
|
|
* @return Config |
41
|
|
|
*/ |
42
|
|
|
public function setEnv(string $env = self::ENV_DEV): Config |
43
|
|
|
{ |
44
|
|
|
$this->env = $env; |
45
|
|
|
return $this; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* returns env |
50
|
|
|
* |
51
|
|
|
* @return string |
52
|
|
|
*/ |
53
|
|
|
public function getEnv(): string |
54
|
|
|
{ |
55
|
|
|
return $this->env; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* set config path |
60
|
|
|
* |
61
|
|
|
* @param string $path |
62
|
|
|
* @return Config |
63
|
|
|
*/ |
64
|
|
|
public function setPath(string $path): Config |
65
|
|
|
{ |
66
|
|
|
$this->path = $path; |
67
|
|
|
return $this; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* returns config path |
72
|
|
|
* |
73
|
|
|
* @return string |
74
|
|
|
*/ |
75
|
|
|
public function getPath(): string |
76
|
|
|
{ |
77
|
|
|
return $this->path; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* return config array for a main entry key |
82
|
|
|
* |
83
|
|
|
* @param string $key |
84
|
|
|
* @return array |
85
|
|
|
*/ |
86
|
|
|
public function getSettings(string $key = ''): array |
87
|
|
|
{ |
88
|
|
|
return ($key) ? $this->settings[$key] : $this->settings; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* return true if config main entry for a key exists |
93
|
|
|
* |
94
|
|
|
* @param string $key |
95
|
|
|
* @return boolean |
96
|
|
|
*/ |
97
|
|
|
public function hasEntry(string $key): bool |
98
|
|
|
{ |
99
|
|
|
return isset($this->settings[$key]); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* load config for a given env |
104
|
|
|
* |
105
|
|
|
* @return Config |
106
|
|
|
*/ |
107
|
|
|
public function load(): Config |
108
|
|
|
{ |
109
|
|
|
$filename = realpath($this->getFilename()); |
110
|
|
|
if (false === $this->check($filename)) { |
111
|
|
|
throw new \Exception( |
112
|
|
|
sprintf( |
113
|
|
|
self::CONFIG_ERROR_MISSING . '%s on %s', |
114
|
|
|
$this->env, |
115
|
|
|
$this->path |
116
|
|
|
) |
117
|
|
|
); |
118
|
|
|
} |
119
|
|
|
$this->settings = require $this->getFilename(); |
120
|
|
|
return $this; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* getFilename |
125
|
|
|
* |
126
|
|
|
* @return string |
127
|
|
|
*/ |
128
|
|
|
protected function getFilename(): string |
129
|
|
|
{ |
130
|
|
|
return $this->path . $this->env . '.php'; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* check |
135
|
|
|
* |
136
|
|
|
* @param string $filename |
137
|
|
|
* @return boolean |
138
|
|
|
*/ |
139
|
|
|
protected function check($filename): bool |
140
|
|
|
{ |
141
|
|
|
return (in_array($this->env, $this->getAllowedEnv()) |
142
|
|
|
&& file_exists($filename)); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* getAllowedEnv |
147
|
|
|
* |
148
|
|
|
* @return array |
149
|
|
|
*/ |
150
|
|
|
protected function getAllowedEnv(): array |
151
|
|
|
{ |
152
|
|
|
return [ |
153
|
|
|
self::ENV_DEV, self::ENV_INT, self::ENV_PROD, |
154
|
|
|
self::ENV_TEST, self::ENV_CLI |
155
|
|
|
]; |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|