-
-
Notifications
You must be signed in to change notification settings - Fork 106
/
Copy pathLinkGenerator.php
298 lines (247 loc) · 9.81 KB
/
LinkGenerator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
<?php
/**
* This file is part of the Nette Framework (https://nette.org)
* Copyright (c) 2004 David Grudl (https://davidgrudl.com)
*/
declare(strict_types=1);
namespace Nette\Application;
use Nette\Http\UrlScript;
use Nette\Routing\Router;
use Nette\Utils\Reflection;
/**
* Link generator.
*/
final class LinkGenerator
{
/** @internal */
public ?Request $lastRequest = null;
public function __construct(
private readonly Router $router,
private readonly UrlScript $refUrl,
private readonly ?IPresenterFactory $presenterFactory = null,
) {
}
/**
* Generates URL to presenter.
* @param string $destination in format "[//] [[[module:]presenter:]action | signal! | this | @alias] [#fragment]"
* @throws UI\InvalidLinkException
*/
public function link(
string $destination,
array $args = [],
?UI\Component $component = null,
?string $mode = null,
): ?string
{
$parts = self::parseDestination($destination);
$args = $parts['args'] ?? $args;
$request = $this->createRequest($component, $parts['path'] . ($parts['signal'] ? '!' : ''), $args, $mode ?? 'link');
$relative = $mode === 'link' && !$parts['absolute'] && !$component?->getPresenter()->absoluteUrls;
return $mode === 'forward' || $mode === 'test'
? null
: $this->requestToUrl($request, $relative) . $parts['fragment'];
}
/**
* @param string $destination in format "[[[module:]presenter:]action | signal! | this | @alias]"
* @param string $mode forward|redirect|link
* @throws UI\InvalidLinkException
* @internal
*/
public function createRequest(
?UI\Component $component,
string $destination,
array $args,
string $mode,
): Request
{
// note: createRequest supposes that saveState(), run() & tryCall() behaviour is final
$this->lastRequest = null;
$refPresenter = $component?->getPresenter();
$path = $destination;
if (($component && !$component instanceof UI\Presenter) || str_ends_with($destination, '!')) {
[$cname, $signal] = Helpers::splitName(rtrim($destination, '!'));
if ($cname !== '') {
$component = $component->getComponent(strtr($cname, ':', '-'));
}
if ($signal === '') {
throw new UI\InvalidLinkException('Signal must be non-empty string.');
}
$path = 'this';
}
if ($path[0] === '@') {
if (!$this->presenterFactory instanceof PresenterFactory) {
throw new \LogicException('Link aliasing requires PresenterFactory service.');
}
$path = ':' . $this->presenterFactory->getAlias(substr($path, 1));
}
$current = false;
[$presenter, $action] = Helpers::splitName($path);
if ($presenter === '') {
if (!$refPresenter) {
throw new \LogicException("Presenter must be specified in '$destination'.");
}
$action = $path === 'this' ? $refPresenter->getAction() : $action;
$presenter = $refPresenter->getName();
$presenterClass = $refPresenter::class;
} else {
if ($presenter[0] === ':') { // absolute
$presenter = substr($presenter, 1);
if (!$presenter) {
throw new UI\InvalidLinkException("Missing presenter name in '$destination'.");
}
} elseif ($refPresenter) { // relative
[$module, , $sep] = Helpers::splitName($refPresenter->getName());
$presenter = $module . $sep . $presenter;
}
try {
$presenterClass = $this->presenterFactory?->getPresenterClass($presenter);
} catch (InvalidPresenterException $e) {
throw new UI\InvalidLinkException($e->getMessage(), 0, $e);
}
}
// PROCESS SIGNAL ARGUMENTS
if (isset($signal)) { // $component must be StatePersistent
$reflection = new UI\ComponentReflection($component::class);
if ($signal === 'this') { // means "no signal"
$signal = '';
if (array_key_exists(0, $args)) {
throw new UI\InvalidLinkException("Unable to pass parameters to 'this!' signal.");
}
} elseif (!str_contains($signal, UI\Component::NameSeparator)) {
// counterpart of signalReceived() & tryCall()
$method = $reflection->getSignalMethod($signal);
if (!$method) {
throw new UI\InvalidLinkException("Unknown signal '$signal', missing handler {$reflection->getName()}::{$component::formatSignalMethod($signal)}()");
} elseif ($this->isDeprecated($refPresenter, $method)) {
trigger_error("Link to deprecated signal '$signal'" . ($component === $refPresenter ? '' : ' in ' . $component::class) . " from '{$refPresenter->getName()}:{$refPresenter->getAction()}'.", E_USER_DEPRECATED);
}
// convert indexed parameters to named
UI\ParameterConverter::toParameters($method, $args, [], $missing);
}
// counterpart of StatePersistent
if ($args && array_intersect_key($args, $reflection->getPersistentParams())) {
$component->saveState($args);
}
if ($args && $component !== $refPresenter) {
$prefix = $component->getUniqueId() . UI\Component::NameSeparator;
foreach ($args as $key => $val) {
unset($args[$key]);
$args[$prefix . $key] = $val;
}
}
}
// PROCESS ARGUMENTS
if (is_subclass_of($presenterClass, UI\Presenter::class)) {
if ($action === '') {
$action = UI\Presenter::DefaultAction;
}
$current = $refPresenter && ($action === '*' || strcasecmp($action, $refPresenter->getAction()) === 0) && $presenterClass === $refPresenter::class;
$reflection = new UI\ComponentReflection($presenterClass);
if ($this->isDeprecated($refPresenter, $reflection)) {
trigger_error("Link to deprecated presenter '$presenter' from '{$refPresenter->getName()}:{$refPresenter->getAction()}'.", E_USER_DEPRECATED);
}
// counterpart of run() & tryCall()
if ($method = $reflection->getActionRenderMethod($action)) {
if ($this->isDeprecated($refPresenter, $method)) {
trigger_error("Link to deprecated action '$presenter:$action' from '{$refPresenter->getName()}:{$refPresenter->getAction()}'.", E_USER_DEPRECATED);
}
UI\ParameterConverter::toParameters($method, $args, $path === 'this' ? $refPresenter->getParameters() : [], $missing);
} elseif (array_key_exists(0, $args)) {
throw new UI\InvalidLinkException("Unable to pass parameters to action '$presenter:$action', missing corresponding method $presenterClass::{$presenterClass::formatRenderMethod($action)}().");
}
// counterpart of StatePersistent
if ($refPresenter) {
if (empty($signal) && $args && array_intersect_key($args, $reflection->getPersistentParams())) {
$refPresenter->saveStatePartial($args, $reflection);
}
$globalState = $refPresenter->getGlobalState($path === 'this' ? null : $presenterClass);
if ($current && $args) {
$tmp = $globalState + $refPresenter->getParameters();
foreach ($args as $key => $val) {
if (http_build_query([$val]) !== (isset($tmp[$key]) ? http_build_query([$tmp[$key]]) : '')) {
$current = false;
break;
}
}
}
$args += $globalState;
}
}
if ($mode !== 'test' && !empty($missing)) {
foreach ($missing as $rp) {
if (!array_key_exists($rp->getName(), $args)) {
throw new UI\InvalidLinkException("Missing parameter \${$rp->getName()} required by " . Reflection::toString($rp->getDeclaringFunction()));
}
}
}
// ADD ACTION & SIGNAL & FLASH
if ($action) {
$args[UI\Presenter::ActionKey] = $action;
}
if (!empty($signal)) {
$args[UI\Presenter::SignalKey] = $component->getParameterId($signal);
$current = $current && $args[UI\Presenter::SignalKey] === $refPresenter->getParameter(UI\Presenter::SignalKey);
}
if (($mode === 'redirect' || $mode === 'forward') && $refPresenter->hasFlashSession()) {
$flashKey = $refPresenter->getParameter(UI\Presenter::FlashKey);
$args[UI\Presenter::FlashKey] = is_string($flashKey) && $flashKey !== '' ? $flashKey : null;
}
return $this->lastRequest = new Request($presenter, Request::FORWARD, $args, flags: ['current' => $current]);
}
/**
* Parse destination in format "[//] [[[module:]presenter:]action | signal! | this | @alias] [?query] [#fragment]"
* @throws UI\InvalidLinkException
* @return array{absolute: bool, path: string, signal: bool, args: ?array, fragment: string}
* @internal
*/
public static function parseDestination(string $destination): array
{
if (!preg_match('~^ (?<absolute>//)?+ (?<path>[^!?#]++) (?<signal>!)?+ (?<query>\?[^#]*)?+ (?<fragment>\#.*)?+ $~x', $destination, $matches)) {
throw new UI\InvalidLinkException("Invalid destination '$destination'.");
}
if (!empty($matches['query'])) {
trigger_error("Link format is obsolete, use arguments instead of query string in '$destination'.", E_USER_DEPRECATED);
parse_str(substr($matches['query'], 1), $args);
}
return [
'absolute' => (bool) $matches['absolute'],
'path' => $matches['path'],
'signal' => !empty($matches['signal']),
'args' => $args ?? null,
'fragment' => $matches['fragment'] ?? '',
];
}
/**
* Converts Request to URL.
*/
public function requestToUrl(Request $request, ?bool $relative = false): string
{
$url = $this->router->constructUrl($request->toArray(), $this->refUrl);
if ($url === null) {
$params = $request->getParameters();
unset($params[UI\Presenter::ActionKey], $params[UI\Presenter::PresenterKey]);
$params = urldecode(http_build_query($params, '', ', '));
throw new UI\InvalidLinkException("No route for {$request->getPresenterName()}:{$request->getParameter('action')}($params)");
}
if ($relative) {
$hostUrl = $this->refUrl->getHostUrl() . '/';
if (strncmp($url, $hostUrl, strlen($hostUrl)) === 0) {
$url = substr($url, strlen($hostUrl) - 1);
}
}
return $url;
}
public function withReferenceUrl(string $url): static
{
return new self(
$this->router,
new UrlScript($url),
$this->presenterFactory,
);
}
private function isDeprecated(?UI\Presenter $presenter, \ReflectionClass|\ReflectionMethod $reflection): bool
{
return $presenter?->invalidLinkMode
&& (UI\ComponentReflection::parseAnnotation($reflection, 'deprecated') || $reflection->getAttributes(Attributes\Deprecated::class));
}
}