1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Meraki\Route; |
5
|
|
|
|
6
|
|
|
use Meraki\Route\Collection; |
7
|
|
|
use Meraki\Route\Pattern; |
8
|
|
|
use InvalidArgumentException; |
9
|
|
|
use RuntimeException; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Generates a URL/request-target from a route's pattern. |
13
|
|
|
* |
14
|
|
|
* @author Nathan Bishop <[email protected]> (https://nathanbishop.name) |
15
|
|
|
* @copyright 2019 Nathan Bishop |
16
|
|
|
* @license The MIT license. |
17
|
|
|
*/ |
18
|
|
|
final class UrlGenerator |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @const string [RELATIVE_URI_REFERENCE_REGEX description] |
22
|
|
|
*/ |
23
|
|
|
private const RELATIVE_URI_REFERENCE_REGEX = '~^//([^/?#]+)([^?#]*)(\?([^#]*))?~'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var Collection The route collection. |
27
|
|
|
*/ |
28
|
|
|
private $routes; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var string [$base description] |
32
|
|
|
*/ |
33
|
|
|
private $base; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Constructs a new route path generator. |
37
|
|
|
* |
38
|
|
|
* @param Collection $routes The collection of "named" routes. |
39
|
|
|
*/ |
40
|
|
|
public function __construct(Collection $routes) |
41
|
|
|
{ |
42
|
|
|
$this->routes = $routes; |
43
|
|
|
$this->base = ''; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Set a URL for the path to be appended to. |
48
|
|
|
* |
49
|
|
|
* @param string $relativeUri A relative URI reference (a URI without the scheme component). |
50
|
|
|
* @throws InvalidArgumentException If a relative URI reference was not provided. |
51
|
|
|
*/ |
52
|
|
|
public function setBaseUrl(string $relativeUri): void |
53
|
|
|
{ |
54
|
|
|
if (preg_match(self::RELATIVE_URI_REFERENCE_REGEX, $relativeUri) !== 1) { |
55
|
|
|
throw new InvalidArgumentException(sprintf('Invalid relative-uri reference "%s".', $relativeUri)); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$this->base = $relativeUri; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Retrieve the URL that the path is being resolved against. |
63
|
|
|
* |
64
|
|
|
* @return string Either the URI that was set or an empty string if none was set. |
65
|
|
|
*/ |
66
|
|
|
public function getBaseUrl(): string |
67
|
|
|
{ |
68
|
|
|
return $this->base; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Generates a URL for the given route name. |
73
|
|
|
* |
74
|
|
|
* @param string $name The route name. |
75
|
|
|
* @param mixed[] $parameters Replacements for named placeholders. |
76
|
|
|
* @throws RuntimeException If a route could not be found for the given name. |
77
|
|
|
* @return string The full protocol-less URL. |
78
|
|
|
*/ |
79
|
|
|
public function generate(string $name, array $parameters = []) |
80
|
|
|
{ |
81
|
|
|
foreach ($this->routes as $route) { |
82
|
|
|
if ($route->getName() === $name) { |
83
|
|
|
$requestTarget = $this->interpolate($route->getPattern(), $parameters); |
84
|
|
|
|
85
|
|
|
return $this->resolve($this->base, $requestTarget); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
throw new RuntimeException(sprintf('A route could not be found for "%s".', $name)); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Resolve the request-target against a base URL. |
94
|
|
|
* |
95
|
|
|
* @param string $base A relative URI reference. |
96
|
|
|
* @param string $requestTarget The interpolated request-target. |
97
|
|
|
* @return string A protocol-relative (begins with two slashes) URL. |
98
|
|
|
*/ |
99
|
|
|
protected function resolve(string $base, string $requestTarget): string |
100
|
|
|
{ |
101
|
|
|
if ($base) { |
102
|
|
|
return rtrim($base . $requestTarget, '/'); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return $requestTarget; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Replace placeholders in a template with their contextual values. |
110
|
|
|
* |
111
|
|
|
* @param Pattern $template The template/pattern to use. |
112
|
|
|
* @param mixed[] $parameters Replacements for the named placeholders. |
113
|
|
|
* @return string The pattern with the placeholders filled in. |
114
|
|
|
*/ |
115
|
|
|
protected function interpolate(Pattern $template, array $parameters) |
116
|
|
|
{ |
117
|
|
|
$replacements = []; |
118
|
|
|
|
119
|
|
|
foreach ($parameters as $key => $value) { |
120
|
|
|
$replacements[":{$key}"] = $value; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return strtr((string)$template, $replacements); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|