delboy1978uk /
bone-server
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Bone\Server; |
||
| 4 | |||
| 5 | use Bone\Server\Traits\HasAttributesTrait; |
||
| 6 | |||
| 7 | class Environment |
||
| 8 | { |
||
| 9 | use HasAttributesTrait; |
||
| 10 | |||
| 11 | /** |
||
| 12 | * Environment constructor. |
||
| 13 | * @param array $serverGlobals |
||
| 14 | */ |
||
| 15 | 7 | public function __construct(array $serverGlobals) |
|
| 16 | { |
||
| 17 | 7 | $this->setAttributes($serverGlobals); |
|
| 18 | 7 | } |
|
| 19 | |||
| 20 | /** |
||
| 21 | * @param string $configFolder |
||
| 22 | * @param string $applicationEnvironment |
||
| 23 | * @return array |
||
| 24 | */ |
||
| 25 | 4 | public function fetchConfig(string $configFolder, string $applicationEnvironment): array |
|
| 26 | { |
||
| 27 | 4 | $config = $this->loadConfig($configFolder); |
|
| 28 | |||
| 29 | 4 | if (!empty($applicationEnvironment)) { |
|
| 30 | 3 | $config = $this->loadEnvironmentConfig($configFolder, $applicationEnvironment, $config); |
|
| 31 | } |
||
| 32 | |||
| 33 | 4 | return $config; |
|
| 34 | } |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @param string $configFolder |
||
| 38 | * @return array |
||
| 39 | */ |
||
| 40 | 4 | private function loadConfig(string $configFolder): array |
|
| 41 | { |
||
| 42 | 4 | $config = []; |
|
| 43 | 4 | $config = $this->globLoadConfig($configFolder, $config); |
|
| 44 | |||
| 45 | 4 | return $config; |
|
| 46 | } |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @param string $configFolder |
||
| 50 | * @param string $applicationEnvironment |
||
| 51 | * @param array $config |
||
| 52 | * @return array |
||
| 53 | */ |
||
| 54 | 3 | private function loadEnvironmentConfig(string $configFolder, string $applicationEnvironment, array $config): array |
|
| 55 | { |
||
| 56 | 3 | $path = $configFolder . '/' . $applicationEnvironment; |
|
| 57 | 3 | $config = $this->globLoadConfig($path, $config); |
|
| 58 | |||
| 59 | 3 | return $config; |
|
| 60 | } |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @param $path |
||
| 64 | * @param array $config |
||
| 65 | * @return array |
||
| 66 | */ |
||
| 67 | 4 | private function globLoadConfig($path, array $config) |
|
| 68 | { |
||
| 69 | 4 | if (file_exists($path)) { |
|
| 70 | 4 | $files = glob($path . '/*.php'); |
|
| 71 | 4 | foreach ($files as $file) { |
|
| 72 | 4 | $config = $this->loadInConfig($config, $file); |
|
| 73 | } |
||
| 74 | } |
||
| 75 | |||
| 76 | 4 | return $config; |
|
| 77 | } |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @param array $config |
||
| 81 | * @param string $file |
||
| 82 | * @return array |
||
| 83 | */ |
||
| 84 | 4 | private function loadInConfig(array $config, string $file): array |
|
| 85 | { |
||
| 86 | 4 | $moreConfig = include $file; |
|
| 87 | 4 | if (is_array($moreConfig)) { |
|
| 88 | 4 | $config = array_merge($config, $moreConfig); |
|
| 89 | } |
||
| 90 | |||
| 91 | 4 | return $config; |
|
| 92 | } |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @return string |
||
| 96 | */ |
||
| 97 | 1 | public function getApplicationEnv(): string |
|
| 98 | { |
||
| 99 | 1 | return $this->getAttribute('APPLICATION_ENV'); |
|
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 100 | } |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @return string |
||
| 104 | */ |
||
| 105 | 1 | public function getPhpIniDir(): string |
|
| 106 | { |
||
| 107 | 1 | return $this->getAttribute('PHP_INI_DIR'); |
|
|
0 ignored issues
–
show
|
|||
| 108 | } |
||
| 109 | |||
| 110 | /** |
||
| 111 | * @return string |
||
| 112 | */ |
||
| 113 | 1 | public function getPwd(): string |
|
| 114 | { |
||
| 115 | 1 | return $this->getAttribute('PWD'); |
|
|
0 ignored issues
–
show
|
|||
| 116 | } |
||
| 117 | |||
| 118 | /** |
||
| 119 | * @return string |
||
| 120 | */ |
||
| 121 | 1 | public function getUser(): string |
|
| 122 | { |
||
| 123 | 1 | return $this->getAttribute('USER'); |
|
|
0 ignored issues
–
show
|
|||
| 124 | } |
||
| 125 | |||
| 126 | /** |
||
| 127 | * @return string |
||
| 128 | */ |
||
| 129 | 1 | public function getRequestUri(): string |
|
| 130 | { |
||
| 131 | 1 | return $this->getAttribute('REQUEST_URI'); |
|
|
0 ignored issues
–
show
|
|||
| 132 | } |
||
| 133 | |||
| 134 | /** |
||
| 135 | * @return string |
||
| 136 | */ |
||
| 137 | 1 | public function getQueryString(): string |
|
| 138 | { |
||
| 139 | 1 | return $this->getAttribute('QUERY_STRING') ?: ''; |
|
| 140 | } |
||
| 141 | |||
| 142 | /** |
||
| 143 | * @return string |
||
| 144 | */ |
||
| 145 | 1 | public function getRequestMethod(): string |
|
| 146 | { |
||
| 147 | 1 | return $this->getAttribute('REQUEST_METHOD'); |
|
|
0 ignored issues
–
show
|
|||
| 148 | } |
||
| 149 | |||
| 150 | /** |
||
| 151 | * @return string |
||
| 152 | */ |
||
| 153 | 1 | public function getScriptFilename(): string |
|
| 154 | { |
||
| 155 | 1 | return $this->getAttribute('SCRIPT_FILENAME'); |
|
|
0 ignored issues
–
show
|
|||
| 156 | } |
||
| 157 | |||
| 158 | /** |
||
| 159 | * @return string |
||
| 160 | */ |
||
| 161 | 1 | public function getServerAdmin(): string |
|
| 162 | { |
||
| 163 | 1 | return $this->getAttribute('SERVER_ADMIN'); |
|
|
0 ignored issues
–
show
|
|||
| 164 | } |
||
| 165 | |||
| 166 | /** |
||
| 167 | * @return string |
||
| 168 | */ |
||
| 169 | 1 | public function getRequestScheme(): string |
|
| 170 | { |
||
| 171 | 1 | return $this->getAttribute('REQUEST_SCHEME', 'http'); |
|
|
0 ignored issues
–
show
|
|||
| 172 | } |
||
| 173 | |||
| 174 | /** |
||
| 175 | * @return string |
||
| 176 | */ |
||
| 177 | 1 | public function getDocumentRoot(): string |
|
| 178 | { |
||
| 179 | 1 | return $this->getAttribute('DOCUMENT_ROOT'); |
|
|
0 ignored issues
–
show
|
|||
| 180 | } |
||
| 181 | |||
| 182 | /** |
||
| 183 | * @return string |
||
| 184 | */ |
||
| 185 | 1 | public function getRemoteAddress(): string |
|
| 186 | { |
||
| 187 | 1 | return $this->getAttribute('REMOTE_ADDR'); |
|
|
0 ignored issues
–
show
|
|||
| 188 | } |
||
| 189 | |||
| 190 | /** |
||
| 191 | * @return string |
||
| 192 | */ |
||
| 193 | 1 | public function getServerPort(): string |
|
| 194 | { |
||
| 195 | 1 | return $this->getAttribute('SERVER_PORT'); |
|
|
0 ignored issues
–
show
|
|||
| 196 | } |
||
| 197 | |||
| 198 | /** |
||
| 199 | * @return string |
||
| 200 | */ |
||
| 201 | 1 | public function getServerName(): string |
|
| 202 | { |
||
| 203 | 1 | return $this->getAttribute('SERVER_NAME'); |
|
|
0 ignored issues
–
show
|
|||
| 204 | } |
||
| 205 | |||
| 206 | /** |
||
| 207 | * @return string |
||
| 208 | */ |
||
| 209 | 1 | public function getHttpHost(): string |
|
| 210 | { |
||
| 211 | 1 | return $this->getAttribute('HTTP_HOST'); |
|
|
0 ignored issues
–
show
|
|||
| 212 | } |
||
| 213 | |||
| 214 | /** |
||
| 215 | * @return string |
||
| 216 | */ |
||
| 217 | 1 | public function getSiteURL() : string |
|
| 218 | { |
||
| 219 | 1 | return $this->getRequestScheme() . '://' . $this->getHttpHost(); |
|
| 220 | } |
||
| 221 | } |