Skip to content

Commit d60b0ab

Browse files
committed
feat: superglobals
1 parent e2fc524 commit d60b0ab

21 files changed

Lines changed: 906 additions & 306 deletions

system/Config/Services.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -746,13 +746,16 @@ 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 $request = null,
749752
bool $getShared = true,
750753
) {
751754
if ($getShared) {
752-
return static::getSharedInstance('superglobals', $server, $get);
755+
return static::getSharedInstance('superglobals', $server, $get, $post, $cookie, $request);
753756
}
754757

755-
return new Superglobals($server, $get);
758+
return new Superglobals($server, $get, $post, $cookie, $request);
756759
}
757760

758761
/**

system/HTTP/IncomingRequest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -599,9 +599,9 @@ public function getPostGet($index = null, $filter = null, $flags = null)
599599
// Use $_POST directly here, since filter_has_var only
600600
// checks the initial POST data, not anything that might
601601
// have been added since.
602-
return isset($_POST[$index])
602+
return service('superglobals')->post($index) !== null
603603
? $this->getPost($index, $filter, $flags)
604-
: (isset($_GET[$index]) ? $this->getGet($index, $filter, $flags) : $this->getPost($index, $filter, $flags));
604+
: (service('superglobals')->get($index) !== null ? $this->getGet($index, $filter, $flags) : $this->getPost($index, $filter, $flags));
605605
}
606606

607607
/**
@@ -622,9 +622,9 @@ public function getGetPost($index = null, $filter = null, $flags = null)
622622
// Use $_GET directly here, since filter_has_var only
623623
// checks the initial GET data, not anything that might
624624
// have been added since.
625-
return isset($_GET[$index])
625+
return service('superglobals')->get($index) !== null
626626
? $this->getGet($index, $filter, $flags)
627-
: (isset($_POST[$index]) ? $this->getPost($index, $filter, $flags) : $this->getGet($index, $filter, $flags));
627+
: (service('superglobals')->post($index) !== null ? $this->getPost($index, $filter, $flags) : $this->getGet($index, $filter, $flags));
628628
}
629629

630630
/**

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: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
namespace CodeIgniter\HTTP;
1515

1616
use CodeIgniter\Exceptions\ConfigException;
17+
use CodeIgniter\Superglobals;
1718
use CodeIgniter\Validation\FormatRules;
1819
use Config\App;
1920

@@ -43,13 +44,34 @@ trait RequestTrait
4344
*/
4445
protected $ipAddress = '';
4546

47+
/**
48+
* Superglobals access wrapper.
49+
*
50+
* @var Superglobals|null
51+
*/
52+
protected $superglobals;
53+
4654
/**
4755
* Stores values we've retrieved from PHP globals.
4856
*
4957
* @var array{get?: array, post?: array, request?: array, cookie?: array, server?: array}
58+
*
59+
* @deprecated 4.7.0 Use $superglobals instead
5060
*/
5161
protected $globals = [];
5262

63+
/**
64+
* Get the Superglobals service instance.
65+
*/
66+
protected function getSuperglobals(): Superglobals
67+
{
68+
if ($this->superglobals === null) {
69+
$this->superglobals = service('superglobals');
70+
}
71+
72+
return $this->superglobals;
73+
}
74+
5375
/**
5476
* Gets the user's IP address.
5577
*
@@ -231,8 +253,12 @@ public function getEnv($index = null, $filter = null, $flags = null)
231253
*/
232254
public function setGlobal(string $name, $value)
233255
{
256+
// Keep BC with $globals array
234257
$this->globals[$name] = $value;
235258

259+
// Also update Superglobals via service
260+
$this->getSuperglobals()->setGlobalArray($name, $value);
261+
236262
return $this;
237263
}
238264

@@ -342,35 +368,16 @@ public function fetchGlobal(string $name, $index = null, ?int $filter = null, $f
342368
* @param 'cookie'|'get'|'post'|'request'|'server' $name Superglobal name (lowercase)
343369
*
344370
* @return void
371+
*
372+
* @deprecated 4.7.0 No longer needs to be called explicitly. Used internally to maintain BC with $globals.
345373
*/
346374
protected function populateGlobals(string $name)
347375
{
348376
if (! isset($this->globals[$name])) {
349377
$this->globals[$name] = [];
350378
}
351379

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-
}
380+
// Get data from Superglobals service instead of direct access
381+
$this->globals[$name] = $this->getSuperglobals()->getGlobalArray($name);
375382
}
376383
}

system/Log/Logger.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -310,8 +310,8 @@ protected function interpolate($message, array $context = [])
310310
$replace['{' . $key . '}'] = $val;
311311
}
312312

313-
$replace['{post_vars}'] = '$_POST: ' . print_r($_POST, true);
314-
$replace['{get_vars}'] = '$_GET: ' . print_r($_GET, true);
313+
$replace['{post_vars}'] = '$_POST: ' . print_r(service('superglobals')->getPostArray(), true);
314+
$replace['{get_vars}'] = '$_GET: ' . print_r(service('superglobals')->getGetArray(), true);
315315
$replace['{env}'] = ENVIRONMENT;
316316

317317
// Allow us to log the file/line that we are logging from

system/Pager/Pager.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ public function getPageURI(?int $page = null, string $group = 'default', bool $r
279279
}
280280

281281
if ($this->only !== null) {
282-
$query = array_intersect_key($_GET, array_flip($this->only));
282+
$query = array_intersect_key(service('superglobals')->getGetArray(), array_flip($this->only));
283283

284284
if (! $segment) {
285285
$query[$this->groups[$group]['pageSelector']] = $page;
@@ -411,8 +411,9 @@ protected function ensureGroup(string $group, ?int $perPage = null)
411411

412412
$this->calculateCurrentPage($group);
413413

414-
if ($_GET !== []) {
415-
$this->groups[$group]['uri'] = $this->groups[$group]['uri']->setQueryArray($_GET);
414+
$get = service('superglobals')->getGetArray();
415+
if ($get !== []) {
416+
$this->groups[$group]['uri'] = $this->groups[$group]['uri']->setQueryArray($get);
416417
}
417418
}
418419

@@ -433,7 +434,7 @@ protected function calculateCurrentPage(string $group)
433434
} else {
434435
$pageSelector = $this->groups[$group]['pageSelector'];
435436

436-
$page = (int) ($_GET[$pageSelector] ?? 1);
437+
$page = (int) (service('superglobals')->get($pageSelector) ?? 1);
437438

438439
$this->groups[$group]['currentPage'] = $page < 1 ? 1 : $page;
439440
}

system/Security/Security.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -281,10 +281,11 @@ private function removeTokenInRequest(RequestInterface $request): void
281281
{
282282
assert($request instanceof Request);
283283

284-
if (isset($_POST[$this->config->tokenName])) {
284+
$superglobals = service('superglobals');
285+
if ($superglobals->post($this->config->tokenName) !== null) {
285286
// We kill this since we're done and we don't want to pollute the POST array.
286-
unset($_POST[$this->config->tokenName]);
287-
$request->setGlobal('post', $_POST);
287+
$superglobals->unsetPost($this->config->tokenName);
288+
$request->setGlobal('post', $superglobals->getPostArray());
288289
} else {
289290
$body = $request->getBody() ?? '';
290291
$json = json_decode($body);

0 commit comments

Comments
 (0)