Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e200550

Browse files
committed
IMP UriHelper::getCurrentString() and buildStringFromParts()
1 parent 96c2579 commit e200550

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

‎src/utils/UriHelper.php‎

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88

99
class UriHelper
1010
{
11+
private static $defaultSchemePorts = [
12+
'http' => [80],
13+
'https' => [443],
14+
];
15+
1116
# region Private methods
1217
private static function mixUrlEncodedParams(
1318
string $encoded_string,
@@ -209,9 +214,9 @@ public static function buildStringFromParts(array $uriParts): string
209214
{
210215
$join[] = $scheme = $uriParts['scheme'] ?? 'http';
211216
$join[] = '://';
212-
if (isset($uriParts['user'])) {
217+
if (!empty($uriParts['user'])) {
213218
$join[] = $uriParts['user'];
214-
if (isset($uriParts['pass'])) {
219+
if (!empty($uriParts['pass'])) {
215220
$join[] = ":{$uriParts['pass']}";
216221
}
217222
$join[] = '@';
@@ -220,20 +225,20 @@ public static function buildStringFromParts(array $uriParts): string
220225
if (isset($uriParts['port'])) {
221226
$port = $uriParts['port'];
222227
if (
223-
$scheme == 'http' && $port != 80 ||
224-
$scheme == 'https' && $port != 443
228+
isset(static::$defaultSchemePorts[$scheme]) &&
229+
!in_array($port, static::$defaultSchemePorts[$scheme])
225230
) {
226231
$join[] = ":{$port}";
227232
}
228233
}
229234
$join[] = '/';
230-
if (isset($uriParts['path'])) {
235+
if (!empty($uriParts['path'])) {
231236
$join[] = ltrim($uriParts['path'], '/');
232237
}
233-
if (isset($uriParts['query'])) {
238+
if (!empty($uriParts['query'])) {
234239
$join[] = "?{$uriParts['query']}";
235240
}
236-
if (isset($uriParts['fragment'])) {
241+
if (!empty($uriParts['fragment'])) {
237242
$join[] .= "#{$uriParts['fragment']}";
238243
}
239244
return join('', $join);
@@ -290,14 +295,13 @@ public static function getCurrentString(array $sapiVars = [], bool $cached = fal
290295
}
291296
$uriParts['host'] = $sapiVars['HTTP_HOST'] ?? $sapiVars['SERVER_NAME'] ?? null;
292297
$uriParts['path'] = '/' . ltrim($sapiVars['REQUEST_URI'], '/');
298+
$uriParts['query'] = $_SERVER['QUERY_STRING'] ?? null;
293299
return static::buildStringFromParts($uriParts);
294300
}
295301

296302
public static function getCurrent(UriFactoryInterface $uriFactory): UriInterface
297303
{
298-
return $uriFactory
299-
->createUri(static::getCurrentString())
300-
->withQuery($_SERVER['QUERY_STRING'] ?? '');
304+
return $uriFactory->createUri(static::getCurrentString());
301305
}
302306

303307
private static function mapReplaceString(string $uri, array $replaces, bool $encode_values = true): string

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /