Skip to content

Commit 17d82ad

Browse files
committed
refactor: current URI creation
1 parent 94b444a commit 17d82ad

File tree

11 files changed

+183
-379
lines changed

11 files changed

+183
-379
lines changed

system/Config/Services.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
use CodeIgniter\HTTP\RequestInterface;
3838
use CodeIgniter\HTTP\Response;
3939
use CodeIgniter\HTTP\ResponseInterface;
40+
use CodeIgniter\HTTP\SiteURIFactory;
4041
use CodeIgniter\HTTP\URI;
4142
use CodeIgniter\HTTP\UserAgent;
4243
use CodeIgniter\Images\Handlers\BaseHandler;
@@ -732,14 +733,21 @@ public static function toolbar(?ToolbarConfig $config = null, bool $getShared =
732733
*
733734
* @param string $uri
734735
*
735-
* @return URI
736+
* @return URI The current URI if $uri is null.
736737
*/
737738
public static function uri(?string $uri = null, bool $getShared = true)
738739
{
739740
if ($getShared) {
740741
return static::getSharedInstance('uri', $uri);
741742
}
742743

744+
$appConfig = config(App::class);
745+
$factory = new SiteURIFactory($_SERVER, $appConfig);
746+
747+
if ($uri === null) {
748+
return $factory->createFromGlobals();
749+
}
750+
743751
return new URI($uri);
744752
}
745753

system/HTTP/IncomingRequest.php

Lines changed: 10 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,12 @@ public function __construct($config, ?URI $uri = null, $body = 'php://input', ?U
175175

176176
parent::__construct($config);
177177

178-
$this->detectURI($config->uriProtocol, $config->baseURL);
178+
if ($uri instanceof SiteURI) {
179+
$this->setPath($uri->getRoutePath());
180+
} else {
181+
$this->setPath($uri->getPath());
182+
}
183+
179184
$this->detectLocale($config);
180185
}
181186

@@ -225,7 +230,7 @@ public function detectLocale($config)
225230
* either provided by the user in the baseURL Config setting, or
226231
* determined from the environment as needed.
227232
*
228-
* @deprecated $protocol and $baseURL are deprecated. No longer used.
233+
* @deprecated No longer used.
229234
*/
230235
protected function detectURI(string $protocol, string $baseURL)
231236
{
@@ -443,7 +448,7 @@ public function isSecure(): bool
443448
}
444449

445450
/**
446-
* Sets the relative path and updates the URI object.
451+
* Sets the URI path relative to baseURL.
447452
*
448453
* Note: Since current_url() accesses the shared request
449454
* instance, this can be used to change the "current URL"
@@ -453,51 +458,13 @@ public function isSecure(): bool
453458
* @param App|null $config Optional alternate config to use
454459
*
455460
* @return $this
461+
*
462+
* @deprecated This method will be private. The parameter $config is deprecated. No longer used.
456463
*/
457464
public function setPath(string $path, ?App $config = null)
458465
{
459466
$this->path = $path;
460467

461-
// @TODO remove this. The path of the URI object should be a full URI path,
462-
// not a URI path relative to baseURL.
463-
$this->uri->setPath($path);
464-
465-
$config ??= $this->config;
466-
467-
// It's possible the user forgot a trailing slash on their
468-
// baseURL, so let's help them out.
469-
$baseURL = ($config->baseURL === '') ? $config->baseURL : rtrim($config->baseURL, '/ ') . '/';
470-
471-
// Based on our baseURL and allowedHostnames provided by the developer
472-
// and HTTP_HOST, set our current domain name, scheme.
473-
if ($baseURL !== '') {
474-
$host = $this->determineHost($config, $baseURL);
475-
476-
// Set URI::$baseURL
477-
$uri = new URI($baseURL);
478-
$currentBaseURL = (string) $uri->setHost($host);
479-
$this->uri->setBaseURL($currentBaseURL);
480-
481-
$this->uri->setScheme(parse_url($baseURL, PHP_URL_SCHEME));
482-
$this->uri->setHost($host);
483-
$this->uri->setPort(parse_url($baseURL, PHP_URL_PORT));
484-
485-
// Ensure we have any query vars
486-
$this->uri->setQuery($_SERVER['QUERY_STRING'] ?? '');
487-
488-
// Check if the scheme needs to be coerced into its secure version
489-
if ($config->forceGlobalSecureRequests && $this->uri->getScheme() === 'http') {
490-
$this->uri->setScheme('https');
491-
}
492-
} elseif (! is_cli()) {
493-
// Do not change exit() to exception; Request is initialized before
494-
// setting the exception handler, so if an exception is raised, an
495-
// error will be displayed even if in the production environment.
496-
// @codeCoverageIgnoreStart
497-
exit('You have an empty or invalid baseURL. The baseURL value must be set in app/Config/App.php, or through the .env file.');
498-
// @codeCoverageIgnoreEnd
499-
}
500-
501468
return $this;
502469
}
503470

@@ -531,10 +498,6 @@ private function determineHost(App $config, string $baseURL): string
531498
*/
532499
public function getPath(): string
533500
{
534-
if ($this->path === null) {
535-
$this->detectPath($this->config->uriProtocol);
536-
}
537-
538501
return $this->path;
539502
}
540503

system/HTTP/URI.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,6 @@ class URI
9292
/**
9393
* URI path.
9494
*
95-
* Note: The constructor of the IncomingRequest class changes the path of
96-
* the URI object held by the IncomingRequest class to a path relative
97-
* to the baseURL. If the baseURL contains subfolders, this value
98-
* will be different from the current URI path.
99-
*
10095
* @var string
10196
*/
10297
protected $path;

system/Test/FeatureTestTrait.php

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use CodeIgniter\Events\Events;
1515
use CodeIgniter\HTTP\IncomingRequest;
1616
use CodeIgniter\HTTP\Request;
17+
use CodeIgniter\HTTP\SiteURI;
1718
use CodeIgniter\HTTP\URI;
1819
use CodeIgniter\Router\Exceptions\RedirectException;
1920
use CodeIgniter\Router\RouteCollection;
@@ -284,15 +285,23 @@ public function options(string $path, ?array $params = null)
284285
*/
285286
protected function setupRequest(string $method, ?string $path = null): IncomingRequest
286287
{
287-
$path = URI::removeDotSegments($path);
288-
$config = config(App::class);
289-
$request = Services::request($config, false);
288+
$config = config(App::class);
289+
$uri = new SiteURI($config);
290290

291291
// $path may have a query in it
292-
$parts = explode('?', $path);
293-
$_SERVER['QUERY_STRING'] = $parts[1] ?? '';
292+
$path = URI::removeDotSegments($path);
293+
$parts = explode('?', $path);
294+
$path = $parts[0];
295+
$query = $parts[1] ?? '';
296+
297+
$_SERVER['QUERY_STRING'] = $query;
298+
299+
$uri->setPath($path);
300+
301+
Services::injectMock('uri', $uri);
302+
303+
$request = Services::request($config, false);
294304

295-
$request->setPath($parts[0]);
296305
$request->setMethod($method);
297306
$request->setProtocolVersion('1.1');
298307

tests/_support/Config/Services.php

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

1212
namespace Tests\Support\Config;
1313

14+
use CodeIgniter\HTTP\SiteURIFactory;
1415
use CodeIgniter\HTTP\URI;
16+
use Config\App;
1517
use Config\Services as BaseServices;
1618
use RuntimeException;
1719

@@ -41,6 +43,13 @@ public static function uri(?string $uri = null, bool $getShared = true)
4143
return static::getSharedInstance('uri', $uri);
4244
}
4345

46+
$appConfig = config(App::class);
47+
$factory = new SiteURIFactory($_SERVER, $appConfig);
48+
49+
if ($uri === null) {
50+
return $factory->createFromGlobals();
51+
}
52+
4453
return new URI($uri);
4554
}
4655
}

tests/system/HTTP/IncomingRequestDetectingTest.php

Lines changed: 0 additions & 195 deletions
This file was deleted.

0 commit comments

Comments
 (0)