Skip to content

Commit fbada57

Browse files
authored
Merge pull request #207 from henrikthesing/increase-performance-of-generator
faster lookups instead of nested loops
2 parents 6914421 + 09e7d62 commit fbada57

File tree

1 file changed

+17
-16
lines changed

1 file changed

+17
-16
lines changed

src/Generator.php

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -187,26 +187,27 @@ protected function buildUrl()
187187
protected function buildTokenReplacements()
188188
{
189189
preg_match_all(self::REGEX, $this->url, $matches, PREG_SET_ORDER);
190+
190191
foreach ($matches as $match) {
191192
$name = $match[1];
192-
foreach ($this->data as $key => $val) {
193-
if ($key === $name) {
194-
$token = isset($match[2]) ? $match[2] : null;
195-
if (isset($this->route->tokens[$name]) && is_string($this->route->tokens[$name])) {
196-
// if $token is null use route token
197-
$token = $token ?: $this->route->tokens[$name];
198-
}
199-
if ($token) {
200-
if (!preg_match('~^' . $token . '$~', (string)$val)) {
201-
throw new \RuntimeException(sprintf(
202-
'Parameter value for [%s] did not match the regex `%s`',
203-
$name,
204-
$token
205-
));
206-
}
193+
194+
if (isset($this->data[$name])) {
195+
$val = $this->data[$name];
196+
$token = isset($match[2]) ? $match[2] : null;
197+
if (isset($this->route->tokens[$name]) && is_string($this->route->tokens[$name])) {
198+
// if $token is null use route token
199+
$token = $token ?: $this->route->tokens[$name];
200+
}
201+
if ($token) {
202+
if (!preg_match('~^' . $token . '$~', (string)$val)) {
203+
throw new \RuntimeException(sprintf(
204+
'Parameter value for [%s] did not match the regex `%s`',
205+
$name,
206+
$token
207+
));
207208
}
208-
$this->repl[$match[0]] = $this->encode($val);
209209
}
210+
$this->repl[$match[0]] = $this->encode($val);
210211
}
211212
}
212213
}

0 commit comments

Comments
 (0)