Skip to content

Commit 1b1440f

Browse files
committed
continue with superglobals
1 parent d60b0ab commit 1b1440f

12 files changed

Lines changed: 41 additions & 38 deletions

File tree

system/Commands/Utilities/Environment.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ final class Environment extends BaseCommand
8686
public function run(array $params)
8787
{
8888
if ($params === []) {
89-
CLI::write(sprintf('Your environment is currently set as %s.', CLI::color($_SERVER['CI_ENVIRONMENT'] ?? ENVIRONMENT, 'green')));
89+
CLI::write(sprintf('Your environment is currently set as %s.', CLI::color(service('superglobals')->server('CI_ENVIRONMENT') ?? ENVIRONMENT, 'green')));
9090
CLI::newLine();
9191

9292
return EXIT_ERROR;
@@ -119,7 +119,8 @@ public function run(array $params)
119119
// force DotEnv to reload the new environment
120120
// however we cannot redefine the ENVIRONMENT constant
121121
putenv('CI_ENVIRONMENT');
122-
unset($_ENV['CI_ENVIRONMENT'], $_SERVER['CI_ENVIRONMENT']);
122+
unset($_ENV['CI_ENVIRONMENT']);
123+
service('superglobals')->unsetServer('CI_ENVIRONMENT');
123124
(new DotEnv((new Paths())->envDirectory ?? ROOTPATH))->load();
124125

125126
CLI::write(sprintf('Environment is successfully changed to "%s".', $env), 'green');
@@ -149,7 +150,7 @@ private function writeNewEnvironmentToEnvFile(string $newEnv): bool
149150
copy($baseEnv, $envFile);
150151
}
151152

152-
$pattern = preg_quote($_SERVER['CI_ENVIRONMENT'] ?? ENVIRONMENT, '/');
153+
$pattern = preg_quote(service('superglobals')->server('CI_ENVIRONMENT') ?? ENVIRONMENT, '/');
153154
$pattern = sprintf('/^[#\s]*CI_ENVIRONMENT[=\s]+%s$/m', $pattern);
154155

155156
return file_put_contents(

system/Commands/Utilities/Routes.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,17 +87,14 @@ public function run(array $params)
8787

8888
// Set HTTP_HOST
8989
if ($host !== null) {
90-
$request = service('request');
91-
$_SERVER = $request->getServer();
92-
$_SERVER['HTTP_HOST'] = $host;
93-
$request->setGlobal('server', $_SERVER);
90+
service('superglobals')->setServer('HTTP_HOST', $host);
9491
}
9592

9693
$collection = service('routes')->loadRoutes();
9794

9895
// Reset HTTP_HOST
9996
if ($host !== null) {
100-
unset($_SERVER['HTTP_HOST']);
97+
service('superglobals')->unsetServer('HTTP_HOST');
10198
}
10299

103100
$methods = Router::HTTP_METHODS;

system/Config/Services.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,7 @@ public static function createRequest(App $config, bool $isCli = false): void
543543
$request = AppServices::incomingrequest($config);
544544

545545
// guess at protocol if needed
546-
$request->setProtocolVersion($_SERVER['SERVER_PROTOCOL'] ?? 'HTTP/1.1');
546+
$request->setProtocolVersion(static::superglobals()->server('SERVER_PROTOCOL') ?? 'HTTP/1.1');
547547
}
548548

549549
// Inject the request object into Services.

system/HTTP/CLIRequest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ public function getOptionString(bool $useLongOpts = false): string
185185
*/
186186
protected function parseCommand()
187187
{
188-
$args = $this->getServer('argv');
188+
$args = $this->getServer('argv') ?? [];
189189
array_shift($args); // Scrap index.php
190190

191191
$optionValue = false;

system/HTTP/DownloadResponse.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,9 +171,9 @@ private function getDownloadFileName(): string
171171
*
172172
* Reference: http://digiblog.de/2011/04/19/android-and-the-download-file-headers/
173173
*/
174-
// @todo: depend super global
175-
if (count($x) !== 1 && isset($_SERVER['HTTP_USER_AGENT'])
176-
&& preg_match('/Android\s(1|2\.[01])/', $_SERVER['HTTP_USER_AGENT'])) {
174+
$userAgent = service('superglobals')->server('HTTP_USER_AGENT');
175+
if (count($x) !== 1 && $userAgent !== null
176+
&& preg_match('/Android\s(1|2\.[01])/', $userAgent)) {
177177
$x[count($x) - 1] = strtoupper($extension);
178178
$filename = implode('.', $x);
179179
}

system/HTTP/IncomingRequest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,9 @@ public function isAJAX(): bool
266266
*/
267267
public function isSecure(): bool
268268
{
269-
if (! empty($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off') {
269+
$https = $this->getSuperglobals()->server('HTTPS');
270+
271+
if ($https !== null && strtolower($https) !== 'off') {
270272
return true;
271273
}
272274

system/HTTP/MessageTrait.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,19 +86,21 @@ public function appendBody($data): self
8686
*/
8787
public function populateHeaders(): void
8888
{
89-
$contentType = $_SERVER['CONTENT_TYPE'] ?? getenv('CONTENT_TYPE');
89+
$contentType = service('superglobals')->server('CONTENT_TYPE') ?? getenv('CONTENT_TYPE');
9090
if (! empty($contentType)) {
9191
$this->setHeader('Content-Type', $contentType);
9292
}
9393
unset($contentType);
9494

95-
foreach (array_keys($_SERVER) as $key) {
95+
$serverArray = service('superglobals')->getServerArray();
96+
97+
foreach (array_keys($serverArray) as $key) {
9698
if (sscanf($key, 'HTTP_%s', $header) === 1) {
9799
// take SOME_HEADER and turn it into Some-Header
98100
$header = str_replace('_', ' ', strtolower($header));
99101
$header = str_replace(' ', '-', ucwords($header));
100102

101-
$this->setHeader($header, $_SERVER[$key]);
103+
$this->setHeader($header, $serverArray[$key]);
102104

103105
// Add us to the header map, so we can find them case-insensitively
104106
$this->headerMap[strtolower($header)] = $header;

system/HTTP/ResponseTrait.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -451,21 +451,26 @@ public function sendBody()
451451
public function redirect(string $uri, string $method = 'auto', ?int $code = null)
452452
{
453453
// IIS environment likely? Use 'refresh' for better compatibility
454+
$superglobals = service('superglobals');
455+
$serverSoftware = $superglobals->server('SERVER_SOFTWARE');
454456
if (
455457
$method === 'auto'
456-
&& isset($_SERVER['SERVER_SOFTWARE'])
457-
&& str_contains($_SERVER['SERVER_SOFTWARE'], 'Microsoft-IIS')
458+
&& $serverSoftware !== null
459+
&& str_contains($serverSoftware, 'Microsoft-IIS')
458460
) {
459461
$method = 'refresh';
460462
} elseif ($method !== 'refresh' && $code === null) {
461463
// override status code for HTTP/1.1 & higher
464+
$serverProtocol = $superglobals->server('SERVER_PROTOCOL');
465+
$requestMethod = $superglobals->server('REQUEST_METHOD');
462466
if (
463-
isset($_SERVER['SERVER_PROTOCOL'], $_SERVER['REQUEST_METHOD'])
467+
$serverProtocol !== null
468+
&& $requestMethod !== null
464469
&& $this->getProtocolVersion() >= 1.1
465470
) {
466-
if ($_SERVER['REQUEST_METHOD'] === Method::GET) {
471+
if ($requestMethod === Method::GET) {
467472
$code = 302;
468-
} elseif (in_array($_SERVER['REQUEST_METHOD'], [Method::POST, Method::PUT, Method::DELETE], true)) {
473+
} elseif (in_array($requestMethod, [Method::POST, Method::PUT, Method::DELETE], true)) {
469474
// reference: https://en.wikipedia.org/wiki/Post/Redirect/Get
470475
$code = 303;
471476
} else {

system/Helpers/form_helper.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -290,12 +290,17 @@ function form_dropdown($data = '', $options = [], $selected = [], $extra = ''):
290290

291291
// If no selected state was submitted we will attempt to set it automatically
292292
if ($selected === []) {
293+
$superglobals = service('superglobals');
293294
if (is_array($data)) {
294-
if (isset($data['name'], $_POST[$data['name']])) {
295-
$selected = [$_POST[$data['name']]];
295+
$postValue = $superglobals->post($data['name'] ?? '');
296+
if (isset($data['name']) && $postValue !== null) {
297+
$selected = [$postValue];
298+
}
299+
} else {
300+
$postValue = $superglobals->post($data);
301+
if ($postValue !== null) {
302+
$selected = [$postValue];
296303
}
297-
} elseif (isset($_POST[$data])) {
298-
$selected = [$_POST[$data]];
299304
}
300305
}
301306

system/Router/Router.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ public function __construct(RouteCollectionInterface $routes, ?Request $request
167167
$this->controller = $this->collection->getDefaultController();
168168
$this->method = $this->collection->getDefaultMethod();
169169

170-
$this->collection->setHTTPVerb($request->getMethod() === '' ? $_SERVER['REQUEST_METHOD'] : $request->getMethod());
170+
$this->collection->setHTTPVerb($request->getMethod() === '' ? service('superglobals')->server('REQUEST_METHOD') : $request->getMethod());
171171

172172
$this->translateURIDashes = $this->collection->shouldTranslateURIDashes();
173173

0 commit comments

Comments
 (0)