Skip to content

Commit bc11219

Browse files
committed
UrlImmutable: authority is pregenerated (microoptimization)
1 parent d208720 commit bc11219

File tree

1 file changed

+24
-13
lines changed

1 file changed

+24
-13
lines changed

src/Http/UrlImmutable.php

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ class UrlImmutable implements \JsonSerializable
6666
/** @var string */
6767
private $fragment = '';
6868

69+
/** @var string */
70+
private $authority = '';
71+
6972

7073
/**
7174
* @param string|self|Url $url
@@ -80,9 +83,7 @@ public function __construct($url)
8083
throw new Nette\InvalidArgumentException;
8184
}
8285

83-
if ($this->host && substr($this->path, 0, 1) !== '/') {
84-
$this->path = '/' . $this->path;
85-
}
86+
$this->build();
8687
}
8788

8889

@@ -173,15 +174,7 @@ public function getAbsoluteUrl(): string
173174
*/
174175
public function getAuthority(): string
175176
{
176-
return $this->host === ''
177-
? ''
178-
: ($this->user !== ''
179-
? rawurlencode($this->user) . ($this->password === '' ? '' : ':' . rawurlencode($this->password)) . '@'
180-
: '')
181-
. $this->host
182-
. ($this->port && (!isset(Url::$defaultPorts[$this->scheme]) || $this->port !== Url::$defaultPorts[$this->scheme])
183-
? ':' . $this->port
184-
: '');
177+
return $this->authority;
185178
}
186179

187180

@@ -191,7 +184,7 @@ public function getAuthority(): string
191184
public function getHostUrl(): string
192185
{
193186
return ($this->scheme ? $this->scheme . ':' : '')
194-
. (($authority = $this->getAuthority()) ? '//' . $authority : '');
187+
. ($this->authority ? '//' . $this->authority : '');
195188
}
196189

197190

@@ -221,4 +214,22 @@ final public function export(): array
221214
{
222215
return [$this->scheme, $this->user, $this->password, $this->host, $this->port, $this->path, $this->query, $this->fragment];
223216
}
217+
218+
219+
protected function build(): void
220+
{
221+
if ($this->host && substr($this->path, 0, 1) !== '/') {
222+
$this->path = '/' . $this->path;
223+
}
224+
225+
$this->authority = $this->host === ''
226+
? ''
227+
: ($this->user !== ''
228+
? rawurlencode($this->user) . ($this->password === '' ? '' : ':' . rawurlencode($this->password)) . '@'
229+
: '')
230+
. $this->host
231+
. ($this->port && (!isset(Url::$defaultPorts[$this->scheme]) || $this->port !== Url::$defaultPorts[$this->scheme])
232+
? ':' . $this->port
233+
: '');
234+
}
224235
}

0 commit comments

Comments
 (0)