Skip to content

Commit 6c410f5

Browse files
committed
refactor: base_url() and site_url()
1 parent 85a2908 commit 6c410f5

1 file changed

Lines changed: 82 additions & 18 deletions

File tree

system/Helpers/url_helper.php

Lines changed: 82 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -94,28 +94,65 @@ function _get_uri(string $relativePath = '', ?App $config = null, bool $useConfi
9494
/**
9595
* Returns a site URL as defined by the App config.
9696
*
97-
* @param array|string $relativePath URI string or array of URI segments
98-
* @param string|null $scheme URI scheme. E.g., http, ftp
99-
* @param App|null $config Alternate configuration to use
100-
* @param bool $dontUseConfig Set true if you don't use the Config
101-
* baseURL even when you pass the Config
97+
* @param array|string $relativePath URI string or array of URI segments
98+
* @param string|null $scheme URI scheme. E.g., http, ftp
99+
* @param App|null $config Alternate configuration to use
102100
*/
103-
function site_url(
104-
$relativePath = '',
105-
?string $scheme = null,
106-
?App $config = null,
107-
bool $dontUseConfig = false
108-
): string {
109-
// If $dontUseConfig is false and $config is not passed,
110-
// we use the Config baseURL.
111-
$useConfig = (! $dontUseConfig) && ($config !== null);
101+
function site_url($relativePath = '', ?string $scheme = null, ?App $config = null): string
102+
{
103+
$appConfig = null;
104+
if ($config === null) {
105+
$appConfig = config('App');
106+
}
112107

113108
// Convert array of segments to a string
114109
if (is_array($relativePath)) {
115110
$relativePath = implode('/', $relativePath);
116111
}
117112

118-
$uri = _get_uri($relativePath, $config, $useConfig);
113+
// If a full URI was passed then convert it
114+
if (strpos($relativePath, '://') !== false) {
115+
$full = new URI($relativePath);
116+
$relativePath = URI::createURIString(
117+
null,
118+
null,
119+
$full->getPath(),
120+
$full->getQuery(),
121+
$full->getFragment()
122+
);
123+
}
124+
125+
$relativePath = URI::removeDotSegments($relativePath);
126+
127+
$request = Services::request();
128+
129+
if ($config === null) {
130+
$url = $request instanceof CLIRequest
131+
? rtrim($appConfig->baseURL, '/ ') . '/'
132+
// Use the current baseURL for multiple domain support
133+
: $request->getUri()->getBaseURL();
134+
135+
$config = $appConfig;
136+
} else {
137+
$url = rtrim($config->baseURL, '/ ') . '/';
138+
}
139+
140+
// Check for an index page
141+
if ($config->indexPage !== '') {
142+
$url .= $config->indexPage;
143+
144+
// Check if we need a separator
145+
if ($relativePath !== '' && $relativePath[0] !== '/' && $relativePath[0] !== '?') {
146+
$url .= '/';
147+
}
148+
}
149+
150+
$uri = new URI($url . $relativePath);
151+
152+
// Check if the baseURL scheme needs to be coerced into its secure version
153+
if ($config->forceGlobalSecureRequests && $uri->getScheme() === 'http') {
154+
$uri->setScheme('https');
155+
}
119156

120157
return URI::createURIString(
121158
$scheme ?? $uri->getScheme(),
@@ -137,10 +174,37 @@ function site_url(
137174
*/
138175
function base_url($relativePath = '', ?string $scheme = null): string
139176
{
140-
$config = clone config('App');
141-
$config->indexPage = '';
177+
// Convert array of segments to a string
178+
if (is_array($relativePath)) {
179+
$relativePath = implode('/', $relativePath);
180+
}
181+
182+
// If a full URI was passed then convert it
183+
if (strpos($relativePath, '://') !== false) {
184+
$full = new URI($relativePath);
185+
$relativePath = URI::createURIString(
186+
null,
187+
null,
188+
$full->getPath(),
189+
$full->getQuery(),
190+
$full->getFragment()
191+
);
192+
}
193+
194+
$relativePath = URI::removeDotSegments($relativePath);
195+
196+
$request = Services::request();
197+
$currentBaseURL = $request->getUri()->getBaseURL();
142198

143-
return site_url($relativePath, $scheme, $config, true);
199+
$uri = new URI($currentBaseURL . $relativePath);
200+
201+
return URI::createURIString(
202+
$scheme ?? $uri->getScheme(),
203+
$uri->getAuthority(),
204+
$uri->getPath(),
205+
$uri->getQuery(),
206+
$uri->getFragment()
207+
);
144208
}
145209
}
146210

0 commit comments

Comments
 (0)