Skip to content

Commit f4e16c5

Browse files
committed
UrlImmutable: authority & basePath are pregenerated
1 parent cfd5421 commit f4e16c5

1 file changed

Lines changed: 24 additions & 14 deletions

File tree

src/Http/UrlImmutable.php

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,12 @@ class UrlImmutable implements \JsonSerializable
7676
/** @var string */
7777
private $fragment = '';
7878

79+
/** @var string */
80+
private $authority = '';
81+
82+
/** @var string */
83+
private $basePath = '';
84+
7985

8086
public static function fromUrl(Url $url): self
8187
{
@@ -109,6 +115,19 @@ public static function fromString(string $url): self
109115
private function __construct(array $args)
110116
{
111117
[$this->scheme, $this->user, $this->password, $this->host, $this->port, $this->path, $this->query, $this->fragment] = $args;
118+
119+
$this->authority = $this->host === ''
120+
? ''
121+
: ($this->user !== '' && $this->scheme !== 'http' && $this->scheme !== 'https'
122+
? rawurlencode($this->user) . ($this->password === '' ? '' : ':' . rawurlencode($this->password)) . '@'
123+
: '')
124+
. $this->host
125+
. ($this->port && (!isset(Url::$defaultPorts[$this->scheme]) || $this->port !== Url::$defaultPorts[$this->scheme])
126+
? ':' . $this->port
127+
: '');
128+
129+
$pos = strrpos($this->path, '/');
130+
$this->basePath = $pos === false ? '' : substr($this->path, 0, $pos + 1);
112131
}
113132

114133

@@ -226,15 +245,7 @@ public function getAbsoluteUrl(): string
226245
*/
227246
public function getAuthority(): string
228247
{
229-
return $this->host === ''
230-
? ''
231-
: ($this->user !== '' && $this->scheme !== 'http' && $this->scheme !== 'https'
232-
? rawurlencode($this->user) . ($this->password === '' ? '' : ':' . rawurlencode($this->password)) . '@'
233-
: '')
234-
. $this->host
235-
. ($this->port && (!isset(Url::$defaultPorts[$this->scheme]) || $this->port !== Url::$defaultPorts[$this->scheme])
236-
? ':' . $this->port
237-
: '');
248+
return $this->authority;
238249
}
239250

240251

@@ -244,7 +255,7 @@ public function getAuthority(): string
244255
public function getHostUrl(): string
245256
{
246257
return ($this->scheme ? $this->scheme . ':' : '')
247-
. (($authority = $this->getAuthority()) || $this->scheme ? '//' . $authority : '');
258+
. ($this->authority || $this->scheme ? '//' . $this->authority : '');
248259
}
249260

250261

@@ -253,8 +264,7 @@ public function getHostUrl(): string
253264
*/
254265
public function getBasePath(): string
255266
{
256-
$pos = strrpos($this->path, '/');
257-
return $pos === false ? '' : substr($this->path, 0, $pos + 1);
267+
return $this->basePath;
258268
}
259269

260270

@@ -264,7 +274,7 @@ public function getBasePath(): string
264274
*/
265275
public function getRelativePath()
266276
{
267-
return substr($this->path, strlen($this->getBasePath()));
277+
return substr($this->path, strlen($this->basePath));
268278
}
269279

270280

@@ -273,7 +283,7 @@ public function getRelativePath()
273283
*/
274284
public function getBaseUrl(): string
275285
{
276-
return $this->getHostUrl() . $this->getBasePath();
286+
return $this->getHostUrl() . $this->basePath;
277287
}
278288

279289

0 commit comments

Comments
 (0)