Skip to content

Commit 60fc61b

Browse files
committed
refactor: extract _get_uri() function
1 parent ff0321f commit 60fc61b

1 file changed

Lines changed: 22 additions & 88 deletions

File tree

system/Helpers/url_helper.php

Lines changed: 22 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,23 @@
2626
*
2727
* @internal Outside the framework this should not be used directly.
2828
*
29-
* @param string $relativePath URI path relative to baseURL. May include queries or fragments
30-
* @param App|null $config Alternative Config to use
31-
* @param bool $useConfig Whether to use the Config baseURL
29+
* @param array|string $relativePath URI string or array of URI segments
30+
* @param App|null $config Alternative Config to use
31+
* @param bool $indexPage Whether to add the $indexPage value
3232
*
3333
* @throws HTTPException For invalid paths.
3434
* @throws InvalidArgumentException For invalid config.
35-
*
36-
* @deprecated No longer used.
3735
*/
38-
function _get_uri(string $relativePath = '', ?App $config = null, bool $useConfig = false): URI
36+
function _get_uri($relativePath = '', ?App $config = null, bool $indexPage = false): URI
3937
{
40-
$config ??= config('App');
38+
$appConfig = null;
39+
if ($config === null) {
40+
$appConfig = config('App');
41+
}
4142

42-
if ($config->baseURL === '') {
43-
throw new InvalidArgumentException('_get_uri() requires a valid baseURL.');
43+
// Convert array of segments to a string
44+
if (is_array($relativePath)) {
45+
$relativePath = implode('/', $relativePath);
4446
}
4547

4648
// If a full URI was passed then convert it
@@ -57,20 +59,21 @@ function _get_uri(string $relativePath = '', ?App $config = null, bool $useConfi
5759

5860
$relativePath = URI::removeDotSegments($relativePath);
5961

60-
// Build the full URL based on $config and $relativePath
6162
$request = Services::request();
6263

63-
/** @var App $config */
64-
if ($useConfig) {
65-
$url = rtrim($config->baseURL, '/ ') . '/';
66-
} else {
64+
if ($config === null) {
6765
$url = $request instanceof CLIRequest
68-
? rtrim($config->baseURL, '/ ') . '/'
66+
? rtrim($appConfig->baseURL, '/ ') . '/'
67+
// Use the current baseURL for multiple domain support
6968
: $request->getUri()->getBaseURL();
69+
70+
$config = $appConfig;
71+
} else {
72+
$url = rtrim($config->baseURL, '/ ') . '/';
7073
}
7174

7275
// Check for an index page
73-
if ($config->indexPage !== '') {
76+
if ($indexPage && $config->indexPage !== '') {
7477
$url .= $config->indexPage;
7578

7679
// Check if we need a separator
@@ -79,16 +82,7 @@ function _get_uri(string $relativePath = '', ?App $config = null, bool $useConfi
7982
}
8083
}
8184

82-
$url .= $relativePath;
83-
84-
$uri = new URI($url);
85-
86-
// Check if the baseURL scheme needs to be coerced into its secure version
87-
if ($config->forceGlobalSecureRequests && $uri->getScheme() === 'http') {
88-
$uri->setScheme('https');
89-
}
90-
91-
return $uri;
85+
return new URI($url . $relativePath);
9286
}
9387
}
9488

@@ -107,50 +101,12 @@ function site_url($relativePath = '', ?string $scheme = null, ?App $config = nul
107101
$appConfig = config('App');
108102
}
109103

110-
// Convert array of segments to a string
111-
if (is_array($relativePath)) {
112-
$relativePath = implode('/', $relativePath);
113-
}
114-
115-
// If a full URI was passed then convert it
116-
if (strpos($relativePath, '://') !== false) {
117-
$full = new URI($relativePath);
118-
$relativePath = URI::createURIString(
119-
null,
120-
null,
121-
$full->getPath(),
122-
$full->getQuery(),
123-
$full->getFragment()
124-
);
125-
}
126-
127-
$relativePath = URI::removeDotSegments($relativePath);
128-
129-
$request = Services::request();
104+
$uri = _get_uri($relativePath, $config, true);
130105

131106
if ($config === null) {
132-
$url = $request instanceof CLIRequest
133-
? rtrim($appConfig->baseURL, '/ ') . '/'
134-
// Use the current baseURL for multiple domain support
135-
: $request->getUri()->getBaseURL();
136-
137107
$config = $appConfig;
138-
} else {
139-
$url = rtrim($config->baseURL, '/ ') . '/';
140108
}
141109

142-
// Check for an index page
143-
if ($config->indexPage !== '') {
144-
$url .= $config->indexPage;
145-
146-
// Check if we need a separator
147-
if ($relativePath !== '' && $relativePath[0] !== '/' && $relativePath[0] !== '?') {
148-
$url .= '/';
149-
}
150-
}
151-
152-
$uri = new URI($url . $relativePath);
153-
154110
// Check if the baseURL scheme needs to be coerced into its secure version
155111
if ($config->forceGlobalSecureRequests && $uri->getScheme() === 'http') {
156112
$uri->setScheme('https');
@@ -176,29 +132,7 @@ function site_url($relativePath = '', ?string $scheme = null, ?App $config = nul
176132
*/
177133
function base_url($relativePath = '', ?string $scheme = null): string
178134
{
179-
// Convert array of segments to a string
180-
if (is_array($relativePath)) {
181-
$relativePath = implode('/', $relativePath);
182-
}
183-
184-
// If a full URI was passed then convert it
185-
if (strpos($relativePath, '://') !== false) {
186-
$full = new URI($relativePath);
187-
$relativePath = URI::createURIString(
188-
null,
189-
null,
190-
$full->getPath(),
191-
$full->getQuery(),
192-
$full->getFragment()
193-
);
194-
}
195-
196-
$relativePath = URI::removeDotSegments($relativePath);
197-
198-
$request = Services::request();
199-
$currentBaseURL = $request->getUri()->getBaseURL();
200-
201-
$uri = new URI($currentBaseURL . $relativePath);
135+
$uri = _get_uri($relativePath);
202136

203137
return URI::createURIString(
204138
$scheme ?? $uri->getScheme(),

0 commit comments

Comments
 (0)