Skip to content

Commit 9937a03

Browse files
committed
refactor: extract class Superglobals and use it
1 parent 37772e9 commit 9937a03

4 files changed

Lines changed: 71 additions & 44 deletions

File tree

system/HTTP/SiteURIFactory.php

Lines changed: 24 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,18 @@
1212
namespace CodeIgniter\HTTP;
1313

1414
use CodeIgniter\HTTP\Exceptions\HTTPException;
15+
use CodeIgniter\Superglobals;
1516
use Config\App;
1617

1718
class SiteURIFactory
1819
{
19-
/**
20-
* @var array Superglobal SERVER array
21-
*/
22-
private array $server;
23-
20+
private Superglobals $superglobals;
2421
private App $appConfig;
2522

26-
/**
27-
* @param array $server Superglobal $_SERVER array
28-
*/
29-
public function __construct(array $server, App $appConfig)
23+
public function __construct(Superglobals $superglobals, App $appConfig)
3024
{
31-
$this->server = $server;
32-
$this->appConfig = $appConfig;
25+
$this->superglobals = $superglobals;
26+
$this->appConfig = $appConfig;
3327
}
3428

3529
/**
@@ -103,7 +97,7 @@ public function detectRoutePath(string $protocol = ''): string
10397

10498
case 'PATH_INFO':
10599
default:
106-
$routePath = $this->server[$protocol] ?? $this->parseRequestURI();
100+
$routePath = $this->superglobals->server($protocol) ?? $this->parseRequestURI();
107101
break;
108102
}
109103

@@ -120,27 +114,30 @@ public function detectRoutePath(string $protocol = ''): string
120114
*/
121115
private function parseRequestURI(): string
122116
{
123-
if (! isset($this->server['REQUEST_URI'], $this->server['SCRIPT_NAME'])) {
117+
if (
118+
$this->superglobals->server('REQUEST_URI') === null
119+
|| $this->superglobals->server('SCRIPT_NAME') === null
120+
) {
124121
return '';
125122
}
126123

127124
// parse_url() returns false if no host is present, but the path or query
128125
// string contains a colon followed by a number. So we attach a dummy
129126
// host since REQUEST_URI does not include the host. This allows us to
130127
// parse out the query string and path.
131-
$parts = parse_url('http://dummy' . $this->server['REQUEST_URI']);
128+
$parts = parse_url('http://dummy' . $this->superglobals->server('REQUEST_URI'));
132129
$query = $parts['query'] ?? '';
133130
$path = $parts['path'] ?? '';
134131

135132
// Strip the SCRIPT_NAME path from the URI
136133
if (
137-
$path !== '' && isset($this->server['SCRIPT_NAME'][0])
138-
&& pathinfo($this->server['SCRIPT_NAME'], PATHINFO_EXTENSION) === 'php'
134+
$path !== '' && isset($this->superglobals->server('SCRIPT_NAME')[0])
135+
&& pathinfo($this->superglobals->server('SCRIPT_NAME'), PATHINFO_EXTENSION) === 'php'
139136
) {
140137
// Compare each segment, dropping them until there is no match
141138
$segments = $keep = explode('/', $path);
142139

143-
foreach (explode('/', $this->server['SCRIPT_NAME']) as $i => $segment) {
140+
foreach (explode('/', $this->superglobals->server('SCRIPT_NAME')) as $i => $segment) {
144141
// If these segments are not the same then we're done
145142
if (! isset($segments[$i]) || $segment !== $segments[$i]) {
146143
break;
@@ -160,30 +157,18 @@ private function parseRequestURI(): string
160157
$path = $parts[0];
161158
$newQuery = $query[1] ?? '';
162159

163-
$this->server['QUERY_STRING'] = $newQuery;
164-
$this->updateServer('QUERY_STRING', $newQuery);
160+
$this->superglobals->setServer('QUERY_STRING', $newQuery);
165161
} else {
166-
$this->server['QUERY_STRING'] = $query;
167-
$this->updateServer('QUERY_STRING', $query);
162+
$this->superglobals->setServer('QUERY_STRING', $query);
168163
}
169164

170165
// Update our global GET for values likely to have been changed
171-
parse_str($this->server['QUERY_STRING'], $get);
172-
$this->updateGetArray($get);
166+
parse_str($this->superglobals->server('QUERY_STRING'), $get);
167+
$this->superglobals->setGetArray($get);
173168

174169
return URI::removeDotSegments($path);
175170
}
176171

177-
private function updateServer(string $key, string $value): void
178-
{
179-
$_SERVER[$key] = $value;
180-
}
181-
182-
private function updateGetArray(array $array): void
183-
{
184-
$_GET = $array;
185-
}
186-
187172
/**
188173
* Will parse QUERY_STRING and automatically detect the URI from it.
189174
*
@@ -193,7 +178,7 @@ private function updateGetArray(array $array): void
193178
*/
194179
private function parseQueryString(): string
195180
{
196-
$query = $this->server['QUERY_STRING'] ?? @getenv('QUERY_STRING');
181+
$query = $this->superglobals->server('QUERY_STRING') ?? @getenv('QUERY_STRING');
197182

198183
if (trim($query, '/') === '') {
199184
return '/';
@@ -204,15 +189,14 @@ private function parseQueryString(): string
204189
$path = $parts[0];
205190
$newQuery = $parts[1] ?? '';
206191

207-
$this->server['QUERY_STRING'] = $newQuery;
208-
$this->updateServer('QUERY_STRING', $newQuery);
192+
$this->superglobals->setServer('QUERY_STRING', $newQuery);
209193
} else {
210194
$path = $query;
211195
}
212196

213197
// Update our global GET for values likely to have been changed
214-
parse_str($this->server['QUERY_STRING'], $get);
215-
$this->updateGetArray($get);
198+
parse_str($this->superglobals->server('QUERY_STRING'), $get);
199+
$this->superglobals->setGetArray($get);
216200

217201
return URI::removeDotSegments($path);
218202
}
@@ -224,7 +208,7 @@ private function parseQueryString(): string
224208
*/
225209
private function createURIFromRoutePath(string $routePath): SiteURI
226210
{
227-
$query = $this->server['QUERY_STRING'] ?? '';
211+
$query = $this->superglobals->server('QUERY_STRING') ?? '';
228212

229213
$relativePath = $query !== '' ? $routePath . '?' . $query : $routePath;
230214

@@ -236,7 +220,7 @@ private function createURIFromRoutePath(string $routePath): SiteURI
236220
*/
237221
private function getHost(): ?string
238222
{
239-
$httpHostPort = $this->server['HTTP_HOST'] ?? null;
223+
$httpHostPort = $this->superglobals->server('HTTP_HOST') ?? null;
240224

241225
if ($httpHostPort !== null) {
242226
[$httpHost] = explode(':', $httpHostPort, 2);

system/Superglobals.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
/**
4+
* This file is part of CodeIgniter 4 framework.
5+
*
6+
* (c) CodeIgniter Foundation <admin@codeigniter.com>
7+
*
8+
* For the full copyright and license information, please view
9+
* the LICENSE file that was distributed with this source code.
10+
*/
11+
12+
namespace CodeIgniter;
13+
14+
/**
15+
* Superglobals manipulation.
16+
*
17+
* @internal
18+
*/
19+
final class Superglobals
20+
{
21+
public function server(string $key): ?string
22+
{
23+
return $_SERVER[$key] ?? null;
24+
}
25+
26+
public function setServer(string $key, string $value): void
27+
{
28+
$_SERVER[$key] = $value;
29+
}
30+
31+
public function setGetArray(array $array): void
32+
{
33+
$_GET = $array;
34+
}
35+
}

tests/system/HTTP/SiteURIFactoryDetectRoutePathTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace CodeIgniter\HTTP;
1313

14+
use CodeIgniter\Superglobals;
1415
use CodeIgniter\Test\CIUnitTestCase;
1516
use Config\App;
1617

@@ -34,7 +35,10 @@ private function createSiteURIFactory(array $server, ?App $appConfig = null): Si
3435
{
3536
$appConfig ??= new App();
3637

37-
return new SiteURIFactory($server, $appConfig);
38+
$_SERVER = $server;
39+
$superglobals = new Superglobals();
40+
41+
return new SiteURIFactory($superglobals, $appConfig);
3842
}
3943

4044
public function testDefault()

tests/system/HTTP/SiteURIFactoryTest.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace CodeIgniter\HTTP;
1313

14+
use CodeIgniter\Superglobals;
1415
use CodeIgniter\Test\CIUnitTestCase;
1516
use Config\App;
1617

@@ -41,7 +42,8 @@ public function testCreateFromGlobals()
4142

4243
$_GET['code'] = 'good';
4344

44-
$factory = new SiteURIFactory($_SERVER, new App());
45+
$superglobals = new Superglobals();
46+
$factory = new SiteURIFactory($superglobals, new App());
4547

4648
$uri = $factory->createFromGlobals();
4749

@@ -66,7 +68,8 @@ public function testCreateFromGlobalsAllowedHost()
6668
$config->baseURL = 'http://example.jp/';
6769
$config->allowedHostnames = ['users.example.jp'];
6870

69-
$factory = new SiteURIFactory($_SERVER, $config);
71+
$superglobals = new Superglobals();
72+
$factory = new SiteURIFactory($superglobals, $config);
7073

7174
$uri = $factory->createFromGlobals();
7275

@@ -78,7 +81,8 @@ public function testCreateFromGlobalsAllowedHost()
7881

7982
public function testCreateFromString()
8083
{
81-
$factory = new SiteURIFactory($_SERVER, new App());
84+
$superglobals = new Superglobals();
85+
$factory = new SiteURIFactory($superglobals, new App());
8286

8387
$uriString = 'http://invalid.example.jp/foo/bar?page=3';
8488
$uri = $factory->createFromString($uriString);

0 commit comments

Comments
 (0)