Skip to content

Commit d01c783

Browse files
committed
refactor: current URI creation
1 parent 3ad9d4a commit d01c783

10 files changed

Lines changed: 186 additions & 145 deletions

File tree

system/Config/Services.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
use CodeIgniter\HTTP\RequestInterface;
3939
use CodeIgniter\HTTP\Response;
4040
use CodeIgniter\HTTP\ResponseInterface;
41+
use CodeIgniter\HTTP\SiteURIFactory;
4142
use CodeIgniter\HTTP\URI;
4243
use CodeIgniter\HTTP\UserAgent;
4344
use CodeIgniter\Images\Handlers\BaseHandler;
@@ -52,6 +53,7 @@
5253
use CodeIgniter\Session\Handlers\Database\PostgreHandler;
5354
use CodeIgniter\Session\Handlers\DatabaseHandler;
5455
use CodeIgniter\Session\Session;
56+
use CodeIgniter\Superglobals;
5557
use CodeIgniter\Throttle\Throttler;
5658
use CodeIgniter\Typography\Typography;
5759
use CodeIgniter\Validation\Validation;
@@ -744,14 +746,21 @@ public static function toolbar(?ToolbarConfig $config = null, bool $getShared =
744746
*
745747
* @param string $uri
746748
*
747-
* @return URI
749+
* @return URI The current URI if $uri is null.
748750
*/
749751
public static function uri(?string $uri = null, bool $getShared = true)
750752
{
751753
if ($getShared) {
752754
return static::getSharedInstance('uri', $uri);
753755
}
754756

757+
if ($uri === null) {
758+
$appConfig = config(App::class);
759+
$factory = new SiteURIFactory($appConfig, new Superglobals());
760+
761+
return $factory->createFromGlobals();
762+
}
763+
755764
return new URI($uri);
756765
}
757766

system/HTTP/IncomingRequest.php

Lines changed: 11 additions & 48 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

@@ -227,9 +232,9 @@ public function detectLocale($config)
227232
* either provided by the user in the baseURL Config setting, or
228233
* determined from the environment as needed.
229234
*
230-
* @deprecated $protocol and $baseURL are deprecated. No longer used.
231-
*
232235
* @return void
236+
*
237+
* @deprecated No longer used.
233238
*/
234239
protected function detectURI(string $protocol, string $baseURL)
235240
{
@@ -447,7 +452,7 @@ public function isSecure(): bool
447452
}
448453

449454
/**
450-
* Sets the relative path and updates the URI object.
455+
* Sets the URI path relative to baseURL.
451456
*
452457
* Note: Since current_url() accesses the shared request
453458
* instance, this can be used to change the "current URL"
@@ -457,51 +462,13 @@ public function isSecure(): bool
457462
* @param App|null $config Optional alternate config to use
458463
*
459464
* @return $this
465+
*
466+
* @deprecated This method will be private. The parameter $config is deprecated. No longer used.
460467
*/
461468
public function setPath(string $path, ?App $config = null)
462469
{
463470
$this->path = $path;
464471

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

@@ -535,10 +502,6 @@ private function determineHost(App $config, string $baseURL): string
535502
*/
536503
public function getPath(): string
537504
{
538-
if ($this->path === null) {
539-
$this->detectPath($this->config->uriProtocol);
540-
}
541-
542505
return $this->path;
543506
}
544507

system/HTTP/URI.php

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

system/Test/FeatureTestTrait.php

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use CodeIgniter\HTTP\Exceptions\RedirectException;
1616
use CodeIgniter\HTTP\IncomingRequest;
1717
use CodeIgniter\HTTP\Request;
18+
use CodeIgniter\HTTP\SiteURI;
1819
use CodeIgniter\HTTP\URI;
1920
use Config\App;
2021
use Config\Services;
@@ -265,15 +266,23 @@ public function options(string $path, ?array $params = null)
265266
*/
266267
protected function setupRequest(string $method, ?string $path = null): IncomingRequest
267268
{
268-
$path = URI::removeDotSegments($path);
269-
$config = config(App::class);
270-
$request = Services::request($config, false);
269+
$config = config(App::class);
270+
$uri = new SiteURI($config);
271271

272272
// $path may have a query in it
273-
$parts = explode('?', $path);
274-
$_SERVER['QUERY_STRING'] = $parts[1] ?? '';
273+
$path = URI::removeDotSegments($path);
274+
$parts = explode('?', $path);
275+
$path = $parts[0];
276+
$query = $parts[1] ?? '';
277+
278+
$_SERVER['QUERY_STRING'] = $query;
279+
280+
$uri->setPath($path);
281+
282+
Services::injectMock('uri', $uri);
283+
284+
$request = Services::request($config, false);
275285

276-
$request->setPath($parts[0]);
277286
$request->setMethod($method);
278287
$request->setProtocolVersion('1.1');
279288

tests/_support/Config/Services.php

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

1212
namespace Tests\Support\Config;
1313

14+
use CodeIgniter\HTTP\SiteURIFactory;
1415
use CodeIgniter\HTTP\URI;
16+
use CodeIgniter\Superglobals;
17+
use Config\App;
1518
use Config\Services as BaseServices;
1619
use RuntimeException;
1720

@@ -41,6 +44,13 @@ public static function uri(?string $uri = null, bool $getShared = true)
4144
return static::getSharedInstance('uri', $uri);
4245
}
4346

47+
if ($uri === null) {
48+
$appConfig = config(App::class);
49+
$factory = new SiteURIFactory($appConfig, new Superglobals());
50+
51+
return $factory->createFromGlobals();
52+
}
53+
4454
return new URI($uri);
4555
}
4656
}

0 commit comments

Comments
 (0)