Skip to content

Commit 596fa54

Browse files
authored
Merge pull request #6784 from kenjis/refactor-around-URI
refactor: around URI
2 parents 11680d6 + 9bae6d1 commit 596fa54

File tree

13 files changed

+142
-46
lines changed

13 files changed

+142
-46
lines changed

system/CodeIgniter.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1081,7 +1081,13 @@ public function storePreviousURL($uri)
10811081
}
10821082

10831083
if (isset($_SESSION)) {
1084-
$_SESSION['_ci_previous_url'] = URI::createURIString($uri->getScheme(), $uri->getAuthority(), $uri->getPath(), $uri->getQuery(), $uri->getFragment());
1084+
$_SESSION['_ci_previous_url'] = URI::createURIString(
1085+
$uri->getScheme(),
1086+
$uri->getAuthority(),
1087+
$uri->getPath(),
1088+
$uri->getQuery(),
1089+
$uri->getFragment()
1090+
);
10851091
}
10861092
}
10871093

system/Common.php

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
function app_timezone(): string
4949
{
5050
/** @var App $config */
51-
$config = config(App::class);
51+
$config = config('App');
5252

5353
return $config->appTimezone;
5454
}
@@ -471,14 +471,15 @@ function force_https(int $duration = 31_536_000, ?RequestInterface $request = nu
471471
if ($request === null) {
472472
$request = Services::request(null, true);
473473
}
474-
if ($response === null) {
475-
$response = Services::response(null, true);
476-
}
477474

478475
if (! $request instanceof IncomingRequest) {
479476
return;
480477
}
481478

479+
if ($response === null) {
480+
$response = Services::response(null, true);
481+
}
482+
482483
if ((ENVIRONMENT !== 'testing' && (is_cli() || $request->isSecure())) || (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'test')) {
483484
return; // @codeCoverageIgnore
484485
}
@@ -489,17 +490,19 @@ function force_https(int $duration = 31_536_000, ?RequestInterface $request = nu
489490
Services::session(null, true)->regenerate(); // @codeCoverageIgnore
490491
}
491492

492-
$baseURL = config(App::class)->baseURL;
493+
$baseURL = config('App')->baseURL;
493494

494495
if (strpos($baseURL, 'https://') === 0) {
495-
$baseURL = substr($baseURL, strlen('https://'));
496+
$authority = substr($baseURL, strlen('https://'));
496497
} elseif (strpos($baseURL, 'http://') === 0) {
497-
$baseURL = substr($baseURL, strlen('http://'));
498+
$authority = substr($baseURL, strlen('http://'));
499+
} else {
500+
$authority = $baseURL;
498501
}
499502

500503
$uri = URI::createURIString(
501504
'https',
502-
$baseURL,
505+
$authority,
503506
$request->getUri()->getPath(), // Absolute URIs should use a "/" for an empty path
504507
$request->getUri()->getQuery(),
505508
$request->getUri()->getFragment()
@@ -1020,7 +1023,7 @@ function single_service(string $name, ...$params)
10201023
*/
10211024
function slash_item(string $item): ?string
10221025
{
1023-
$config = config(App::class);
1026+
$config = config('App');
10241027

10251028
if (! property_exists($config, $item)) {
10261029
return null;

system/Debug/Toolbar/Collectors/Config.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
namespace CodeIgniter\Debug\Toolbar\Collectors;
1313

1414
use CodeIgniter\CodeIgniter;
15-
use Config\App;
1615
use Config\Services;
1716

1817
/**
@@ -25,7 +24,7 @@ class Config
2524
*/
2625
public static function display(): array
2726
{
28-
$config = config(App::class);
27+
$config = config('App');
2928

3029
return [
3130
'ciVersion' => CodeIgniter::CI_VERSION,

system/HTTP/CURLRequest.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,13 @@ protected function prepareURL(string $url): string
310310
$uri = $this->baseURI->resolveRelativeURI($url);
311311

312312
// Create the string instead of casting to prevent baseURL muddling
313-
return URI::createURIString($uri->getScheme(), $uri->getAuthority(), $uri->getPath(), $uri->getQuery(), $uri->getFragment());
313+
return URI::createURIString(
314+
$uri->getScheme(),
315+
$uri->getAuthority(),
316+
$uri->getPath(),
317+
$uri->getQuery(),
318+
$uri->getFragment()
319+
);
314320
}
315321

316322
/**

system/HTTP/IncomingRequest.php

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,7 @@ class IncomingRequest extends Request
145145
* Constructor
146146
*
147147
* @param App $config
148-
* @param URI $uri
149148
* @param string|null $body
150-
* @param UserAgent $userAgent
151149
*/
152150
public function __construct($config, ?URI $uri = null, $body = 'php://input', ?UserAgent $userAgent = null)
153151
{
@@ -252,7 +250,10 @@ protected function parseRequestURI(): string
252250
$uri = $parts['path'] ?? '';
253251

254252
// Strip the SCRIPT_NAME path from the URI
255-
if ($uri !== '' && isset($_SERVER['SCRIPT_NAME'][0]) && pathinfo($_SERVER['SCRIPT_NAME'], PATHINFO_EXTENSION) === 'php') {
253+
if (
254+
$uri !== '' && isset($_SERVER['SCRIPT_NAME'][0])
255+
&& pathinfo($_SERVER['SCRIPT_NAME'], PATHINFO_EXTENSION) === 'php'
256+
) {
256257
// Compare each segment, dropping them until there is no match
257258
$segments = $keep = explode('/', $uri);
258259

@@ -357,7 +358,8 @@ public function isCLI(): bool
357358
*/
358359
public function isAJAX(): bool
359360
{
360-
return $this->hasHeader('X-Requested-With') && strtolower($this->header('X-Requested-With')->getValue()) === 'xmlhttprequest';
361+
return $this->hasHeader('X-Requested-With')
362+
&& strtolower($this->header('X-Requested-With')->getValue()) === 'xmlhttprequest';
361363
}
362364

363365
/**
@@ -379,12 +381,13 @@ public function isSecure(): bool
379381

380382
/**
381383
* Sets the relative path and updates the URI object.
384+
*
382385
* Note: Since current_url() accesses the shared request
383386
* instance, this can be used to change the "current URL"
384387
* for testing.
385388
*
386-
* @param string $path URI path relative to SCRIPT_NAME
387-
* @param App $config Optional alternate config to use
389+
* @param string $path URI path relative to SCRIPT_NAME
390+
* @param App|null $config Optional alternate config to use
388391
*
389392
* @return $this
390393
*/
@@ -484,7 +487,10 @@ public function getDefaultLocale(): string
484487
*/
485488
public function getVar($index = null, $filter = null, $flags = null)
486489
{
487-
if (strpos($this->getHeaderLine('Content-Type'), 'application/json') !== false && $this->body !== null) {
490+
if (
491+
strpos($this->getHeaderLine('Content-Type'), 'application/json') !== false
492+
&& $this->body !== null
493+
) {
488494
if ($index === null) {
489495
return $this->getJSON();
490496
}
@@ -621,7 +627,9 @@ public function getPostGet($index = null, $filter = null, $flags = null)
621627
// Use $_POST directly here, since filter_has_var only
622628
// checks the initial POST data, not anything that might
623629
// have been added since.
624-
return isset($_POST[$index]) ? $this->getPost($index, $filter, $flags) : (isset($_GET[$index]) ? $this->getGet($index, $filter, $flags) : $this->getPost($index, $filter, $flags));
630+
return isset($_POST[$index])
631+
? $this->getPost($index, $filter, $flags)
632+
: (isset($_GET[$index]) ? $this->getGet($index, $filter, $flags) : $this->getPost($index, $filter, $flags));
625633
}
626634

627635
/**
@@ -641,7 +649,9 @@ public function getGetPost($index = null, $filter = null, $flags = null)
641649
// Use $_GET directly here, since filter_has_var only
642650
// checks the initial GET data, not anything that might
643651
// have been added since.
644-
return isset($_GET[$index]) ? $this->getGet($index, $filter, $flags) : (isset($_POST[$index]) ? $this->getPost($index, $filter, $flags) : $this->getGet($index, $filter, $flags));
652+
return isset($_GET[$index])
653+
? $this->getGet($index, $filter, $flags)
654+
: (isset($_POST[$index]) ? $this->getPost($index, $filter, $flags) : $this->getGet($index, $filter, $flags));
645655
}
646656

647657
/**

system/HTTP/URI.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,13 @@ class URI
144144
* @param string $query
145145
* @param string $fragment
146146
*/
147-
public static function createURIString(?string $scheme = null, ?string $authority = null, ?string $path = null, ?string $query = null, ?string $fragment = null): string
148-
{
147+
public static function createURIString(
148+
?string $scheme = null,
149+
?string $authority = null,
150+
?string $path = null,
151+
?string $query = null,
152+
?string $fragment = null
153+
): string {
149154
$uri = '';
150155
if (! empty($scheme)) {
151156
$uri .= $scheme . '://';
@@ -156,7 +161,9 @@ public static function createURIString(?string $scheme = null, ?string $authorit
156161
}
157162

158163
if (isset($path) && $path !== '') {
159-
$uri .= substr($uri, -1, 1) !== '/' ? '/' . ltrim($path, '/') : ltrim($path, '/');
164+
$uri .= substr($uri, -1, 1) !== '/'
165+
? '/' . ltrim($path, '/')
166+
: ltrim($path, '/');
160167
}
161168

162169
if ($query) {

system/Helpers/cookie_helper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ function get_cookie($index, bool $xssClean = false, ?string $prefix = '')
7272
$cookie = config('Cookie');
7373

7474
// @TODO Remove Config\App fallback when deprecated `App` members are removed.
75-
$prefix = $cookie instanceof Cookie ? $cookie->prefix : config(App::class)->cookiePrefix;
75+
$prefix = $cookie instanceof Cookie ? $cookie->prefix : config('App')->cookiePrefix;
7676
}
7777

7878
$request = Services::request();

system/Helpers/form_helper.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
*/
1111

1212
use CodeIgniter\Validation\Exceptions\ValidationException;
13-
use Config\App;
1413
use Config\Services;
1514

1615
// CodeIgniter Form Helpers
@@ -51,7 +50,7 @@ function form_open(string $action = '', $attributes = [], array $hidden = []): s
5150
$attributes .= ' method="post"';
5251
}
5352
if (stripos($attributes, 'accept-charset=') === false) {
54-
$config = config(App::class);
53+
$config = config('App');
5554
$attributes .= ' accept-charset="' . strtolower($config->charset) . '"';
5655
}
5756

system/Helpers/url_helper.php

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,13 @@ function _get_uri(string $relativePath = '', ?App $config = null): URI
3939
// If a full URI was passed then convert it
4040
if (is_int(strpos($relativePath, '://'))) {
4141
$full = new URI($relativePath);
42-
$relativePath = URI::createURIString(null, null, $full->getPath(), $full->getQuery(), $full->getFragment());
42+
$relativePath = URI::createURIString(
43+
null,
44+
null,
45+
$full->getPath(),
46+
$full->getQuery(),
47+
$full->getFragment()
48+
);
4349
}
4450

4551
$relativePath = URI::removeDotSegments($relativePath);
@@ -86,7 +92,13 @@ function site_url($relativePath = '', ?string $scheme = null, ?App $config = nul
8692

8793
$uri = _get_uri($relativePath, $config);
8894

89-
return URI::createURIString($scheme ?? $uri->getScheme(), $uri->getAuthority(), $uri->getPath(), $uri->getQuery(), $uri->getFragment());
95+
return URI::createURIString(
96+
$scheme ?? $uri->getScheme(),
97+
$uri->getAuthority(),
98+
$uri->getPath(),
99+
$uri->getQuery(),
100+
$uri->getFragment()
101+
);
90102
}
91103
}
92104

@@ -184,7 +196,7 @@ function uri_string(bool $relative = false): string
184196
function index_page(?App $altConfig = null): string
185197
{
186198
// use alternate config if provided, else default one
187-
$config = $altConfig ?? config(App::class);
199+
$config = $altConfig ?? config('App');
188200

189201
return $config->indexPage;
190202
}
@@ -204,7 +216,7 @@ function index_page(?App $altConfig = null): string
204216
function anchor($uri = '', string $title = '', $attributes = '', ?App $altConfig = null): string
205217
{
206218
// use alternate config if provided, else default one
207-
$config = $altConfig ?? config(App::class);
219+
$config = $altConfig ?? config('App');
208220

209221
$siteUrl = is_array($uri) ? site_url($uri, null, $config) : (preg_match('#^(\w+:)?//#i', $uri) ? $uri : site_url($uri, null, $config));
210222
// eliminate trailing slash
@@ -237,7 +249,7 @@ function anchor($uri = '', string $title = '', $attributes = '', ?App $altConfig
237249
function anchor_popup($uri = '', string $title = '', $attributes = false, ?App $altConfig = null): string
238250
{
239251
// use alternate config if provided, else default one
240-
$config = $altConfig ?? config(App::class);
252+
$config = $altConfig ?? config('App');
241253

242254
$siteUrl = preg_match('#^(\w+:)?//#i', $uri) ? $uri : site_url($uri, null, $config);
243255
$siteUrl = rtrim($siteUrl, '/');

0 commit comments

Comments
 (0)