Skip to content

Commit f204ff7

Browse files
michalsnpaulbalandanneznaika0
authored
feat: complete Superglobals implementation (codeigniter4#9858)
Co-authored-by: John Paul E. Balandan, CPA <paulbalandan@gmail.com> Co-authored-by: neznaika0 <ozornick.ks@gmail.com>
1 parent e88e03a commit f204ff7

74 files changed

Lines changed: 2205 additions & 2149 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

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: 7 additions & 3 deletions
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.
@@ -746,13 +746,17 @@ public static function siteurifactory(
746746
public static function superglobals(
747747
?array $server = null,
748748
?array $get = null,
749+
?array $post = null,
750+
?array $cookie = null,
751+
?array $files = null,
752+
?array $request = null,
749753
bool $getShared = true,
750754
) {
751755
if ($getShared) {
752-
return static::getSharedInstance('superglobals', $server, $get);
756+
return static::getSharedInstance('superglobals', $server, $get, $post, $cookie, $files, $request);
753757
}
754758

755-
return new Superglobals($server, $get);
759+
return new Superglobals($server, $get, $post, $cookie, $files, $request);
756760
}
757761

758762
/**

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/Files/FileCollection.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,11 +150,13 @@ protected function populateFiles()
150150

151151
$this->files = [];
152152

153-
if ($_FILES === []) {
153+
$files = service('superglobals')->getFilesArray();
154+
155+
if ($files === []) {
154156
return;
155157
}
156158

157-
$files = $this->fixFilesArray($_FILES);
159+
$files = $this->fixFilesArray($files);
158160

159161
foreach ($files as $name => $file) {
160162
$this->files[$name] = $this->createFileObject($file);

system/HTTP/IncomingRequest.php

Lines changed: 7 additions & 5 deletions
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 = service('superglobals')->server('HTTPS');
270+
271+
if ($https !== null && strtolower($https) !== 'off') {
270272
return true;
271273
}
272274

@@ -599,9 +601,9 @@ public function getPostGet($index = null, $filter = null, $flags = null)
599601
// Use $_POST directly here, since filter_has_var only
600602
// checks the initial POST data, not anything that might
601603
// have been added since.
602-
return isset($_POST[$index])
604+
return service('superglobals')->post($index) !== null
603605
? $this->getPost($index, $filter, $flags)
604-
: (isset($_GET[$index]) ? $this->getGet($index, $filter, $flags) : $this->getPost($index, $filter, $flags));
606+
: (service('superglobals')->get($index) !== null ? $this->getGet($index, $filter, $flags) : $this->getPost($index, $filter, $flags));
605607
}
606608

607609
/**
@@ -622,9 +624,9 @@ public function getGetPost($index = null, $filter = null, $flags = null)
622624
// Use $_GET directly here, since filter_has_var only
623625
// checks the initial GET data, not anything that might
624626
// have been added since.
625-
return isset($_GET[$index])
627+
return service('superglobals')->get($index) !== null
626628
? $this->getGet($index, $filter, $flags)
627-
: (isset($_POST[$index]) ? $this->getPost($index, $filter, $flags) : $this->getGet($index, $filter, $flags));
629+
: (service('superglobals')->post($index) !== null ? $this->getPost($index, $filter, $flags) : $this->getGet($index, $filter, $flags));
628630
}
629631

630632
/**

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', (string) 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/RedirectResponse.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ public function withInput()
9393
{
9494
$session = service('session');
9595
$session->setFlashdata('_ci_old_input', [
96-
'get' => $_GET ?? [], // @phpstan-ignore nullCoalesce.variable
97-
'post' => $_POST ?? [], // @phpstan-ignore nullCoalesce.variable
96+
'get' => service('superglobals')->getGetArray(),
97+
'post' => service('superglobals')->getPostArray(),
9898
]);
9999

100100
$this->withErrors();

system/HTTP/RequestTrait.php

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ trait RequestTrait
4747
* Stores values we've retrieved from PHP globals.
4848
*
4949
* @var array{get?: array, post?: array, request?: array, cookie?: array, server?: array}
50+
*
51+
* @deprecated 4.7.0 Use the Superglobals service instead
5052
*/
5153
protected $globals = [];
5254

@@ -231,8 +233,12 @@ public function getEnv($index = null, $filter = null, $flags = null)
231233
*/
232234
public function setGlobal(string $name, $value)
233235
{
236+
// Keep BC with $globals array
234237
$this->globals[$name] = $value;
235238

239+
// Also update Superglobals via service
240+
service('superglobals')->setGlobalArray($name, $value);
241+
236242
return $this;
237243
}
238244

@@ -342,35 +348,16 @@ public function fetchGlobal(string $name, $index = null, ?int $filter = null, $f
342348
* @param 'cookie'|'get'|'post'|'request'|'server' $name Superglobal name (lowercase)
343349
*
344350
* @return void
351+
*
352+
* @deprecated 4.7.0 No longer needs to be called explicitly. Used internally to maintain BC with $globals.
345353
*/
346354
protected function populateGlobals(string $name)
347355
{
348356
if (! isset($this->globals[$name])) {
349357
$this->globals[$name] = [];
350358
}
351359

352-
// Don't populate ENV as it might contain
353-
// sensitive data that we don't want to get logged.
354-
switch ($name) {
355-
case 'get':
356-
$this->globals['get'] = $_GET;
357-
break;
358-
359-
case 'post':
360-
$this->globals['post'] = $_POST;
361-
break;
362-
363-
case 'request':
364-
$this->globals['request'] = $_REQUEST;
365-
break;
366-
367-
case 'cookie':
368-
$this->globals['cookie'] = $_COOKIE;
369-
break;
370-
371-
case 'server':
372-
$this->globals['server'] = $_SERVER;
373-
break;
374-
}
360+
// Get data from Superglobals service instead of direct access
361+
$this->globals[$name] = service('superglobals')->getGlobalArray($name);
375362
}
376363
}

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 {

0 commit comments

Comments
 (0)