Skip to content

Commit ed4be55

Browse files
committed
IRouter::constructUrl() accepts and match() returns array instead of Nette\Application\Request (BC break)
1 parent a0474ca commit ed4be55

File tree

74 files changed

+509
-397
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

74 files changed

+509
-397
lines changed

src/Application/Application.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,20 @@ public function run(): void
104104

105105
public function createInitialRequest(): Request
106106
{
107-
$request = $this->router->match($this->httpRequest);
108-
if (!$request) {
107+
$params = $this->router->match($this->httpRequest);
108+
$presenter = $params[UI\Presenter::PRESENTER_KEY] ?? null;
109+
if ($params === null || $presenter === null) {
109110
throw new BadRequestException('No route for HTTP request.');
110111
}
111-
return $request;
112+
unset($params[UI\Presenter::PRESENTER_KEY]);
113+
return new Request(
114+
$presenter,
115+
$this->httpRequest->getMethod(),
116+
$params,
117+
$this->httpRequest->getPost(),
118+
$this->httpRequest->getFiles(),
119+
[Request::SECURED => $this->httpRequest->isSecured()]
120+
);
112121
}
113122

114123

src/Application/IRouter.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ interface IRouter
2121
public const ONE_WAY = 0b0001;
2222

2323
/**
24-
* Maps HTTP request to a Request object.
24+
* Maps HTTP request to an array.
2525
*/
26-
function match(Nette\Http\IRequest $httpRequest): ?Request;
26+
function match(Nette\Http\IRequest $httpRequest): ?array;
2727

2828
/**
29-
* Constructs absolute URL from Request object.
29+
* Constructs absolute URL from array.
3030
*/
31-
function constructUrl(Request $appRequest, Nette\Http\Url $refUrl): ?string;
31+
function constructUrl(array $params, Nette\Http\Url $refUrl): ?string;
3232
}

src/Application/LinkGenerator.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,11 @@ public function link(string $dest, array $params = []): string
7777
if ($action !== '') {
7878
$params[UI\Presenter::ACTION_KEY] = $action;
7979
}
80+
$params[UI\Presenter::PRESENTER_KEY] = $presenter;
8081

81-
$url = $this->router->constructUrl(new Request($presenter, null, $params), $this->refUrl);
82+
$url = $this->router->constructUrl($params, $this->refUrl);
8283
if ($url === null) {
83-
unset($params[UI\Presenter::ACTION_KEY]);
84+
unset($params[UI\Presenter::ACTION_KEY], $params[UI\Presenter::PRESENTER_KEY]);
8485
$params = urldecode(http_build_query($params, '', ', '));
8586
throw new UI\InvalidLinkException("No route for $dest($params)");
8687
}

src/Application/MicroPresenter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public function run(Application\Request $request): Application\IResponse
5959

6060
if ($this->httpRequest && $this->router && !$this->httpRequest->isAjax() && ($request->isMethod('get') || $request->isMethod('head'))) {
6161
$refUrl = clone $this->httpRequest->getUrl();
62-
$url = $this->router->constructUrl($request, $refUrl->setPath($refUrl->getScriptPath()));
62+
$url = $this->router->constructUrl($request->toArray(), $refUrl->setPath($refUrl->getScriptPath()));
6363
if ($url !== null && !$this->httpRequest->getUrl()->isEqual($url)) {
6464
return new Responses\RedirectResponse($url, Http\IResponse::S301_MOVED_PERMANENTLY);
6565
}

src/Application/Request.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,4 +211,12 @@ public function hasFlag(string $flag): bool
211211
{
212212
return !empty($this->flags[$flag]);
213213
}
214+
215+
216+
public function toArray(): array
217+
{
218+
$params = $this->params;
219+
$params['presenter'] = $this->name;
220+
return $params;
221+
}
214222
}

src/Application/Routers/CliRouter.php

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ public function __construct(array $defaults = [])
3333

3434

3535
/**
36-
* Maps command line arguments to a Request object.
36+
* Maps command line arguments to an array.
3737
*/
38-
public function match(Nette\Http\IRequest $httpRequest): ?Application\Request
38+
public function match(Nette\Http\IRequest $httpRequest): ?array
3939
{
4040
if (empty($_SERVER['argv']) || !is_array($_SERVER['argv'])) {
4141
return null;
@@ -82,19 +82,16 @@ public function match(Nette\Http\IRequest $httpRequest): ?Application\Request
8282
$params[self::PRESENTER_KEY] = $presenter;
8383
$presenter = $module;
8484
}
85+
$params['presenter'] = $presenter;
8586

86-
return new Application\Request(
87-
$presenter,
88-
'CLI',
89-
$params
90-
);
87+
return $params;
9188
}
9289

9390

9491
/**
9592
* This router is only unidirectional.
9693
*/
97-
public function constructUrl(Application\Request $appRequest, Nette\Http\Url $refUrl): ?string
94+
public function constructUrl(array $params, Nette\Http\Url $refUrl): ?string
9895
{
9996
return null;
10097
}

src/Application/Routers/Route.php

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
/**
1818
* The bidirectional route is responsible for mapping
19-
* HTTP request to a Request object for dispatch and vice-versa.
19+
* HTTP request to an array for dispatch and vice-versa.
2020
*/
2121
class Route implements Application\IRouter
2222
{
@@ -146,9 +146,9 @@ public function __construct(string $mask, $metadata = [], int $flags = 0)
146146

147147

148148
/**
149-
* Maps HTTP request to a Request object.
149+
* Maps HTTP request to an array.
150150
*/
151-
public function match(Nette\Http\IRequest $httpRequest): ?Application\Request
151+
public function match(Nette\Http\IRequest $httpRequest): ?array
152152
{
153153
// combine with precedence: mask (params in URL-path), fixity, query, (post,) defaults
154154

@@ -249,39 +249,27 @@ public function match(Nette\Http\IRequest $httpRequest): ?Application\Request
249249
} elseif (!is_string($params[self::PRESENTER_KEY])) {
250250
return null;
251251
}
252-
$presenter = $params[self::PRESENTER_KEY];
253-
unset($params[self::PRESENTER_KEY]);
254252

255-
if (isset($this->metadata[self::MODULE_KEY])) {
256-
$presenter = (isset($params[self::MODULE_KEY]) ? $params[self::MODULE_KEY] . ':' : '') . $presenter;
257-
unset($params[self::MODULE_KEY]);
253+
if (isset($this->metadata[self::MODULE_KEY], $params[self::MODULE_KEY])) {
254+
$params[self::PRESENTER_KEY] = $params[self::MODULE_KEY] . ':' . $params[self::PRESENTER_KEY];
258255
}
256+
unset($params[self::MODULE_KEY]);
259257

260-
return new Application\Request(
261-
$presenter,
262-
$httpRequest->getMethod(),
263-
$params,
264-
$httpRequest->getPost(),
265-
$httpRequest->getFiles(),
266-
[Application\Request::SECURED => $httpRequest->isSecured()]
267-
);
258+
return $params;
268259
}
269260

270261

271262
/**
272-
* Constructs absolute URL from Request object.
263+
* Constructs absolute URL from array.
273264
*/
274-
public function constructUrl(Application\Request $appRequest, Nette\Http\Url $refUrl): ?string
265+
public function constructUrl(array $params, Nette\Http\Url $refUrl): ?string
275266
{
276267
if ($this->flags & self::ONE_WAY) {
277268
return null;
278269
}
279270

280-
$params = $appRequest->getParameters();
281271
$metadata = $this->metadata;
282-
283-
$presenter = $appRequest->getPresenterName();
284-
$params[self::PRESENTER_KEY] = $presenter;
272+
$presenter = $params[self::PRESENTER_KEY];
285273

286274
if (isset($metadata[self::MODULE_KEY])) { // try split into module and [submodule:]presenter parts
287275
$module = $metadata[self::MODULE_KEY];

src/Application/Routers/RouteList.php

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
*/
1818
class RouteList extends Nette\Utils\ArrayList implements Nette\Application\IRouter
1919
{
20+
private const PRESENTER_KEY = 'presenter';
21+
2022
/** @var array */
2123
private $cachedRoutes;
2224

@@ -31,49 +33,48 @@ public function __construct(string $module = null)
3133

3234

3335
/**
34-
* Maps HTTP request to a Request object.
36+
* Maps HTTP request to an array.
3537
*/
36-
public function match(Nette\Http\IRequest $httpRequest): ?Nette\Application\Request
38+
public function match(Nette\Http\IRequest $httpRequest): ?array
3739
{
3840
foreach ($this as $route) {
39-
$appRequest = $route->match($httpRequest);
40-
if ($appRequest !== null) {
41-
$name = $appRequest->getPresenterName();
42-
if (strncmp($name, 'Nette:', 6)) {
43-
$appRequest->setPresenterName($this->module . $name);
41+
$params = $route->match($httpRequest);
42+
if ($params !== null) {
43+
$presenter = $params[self::PRESENTER_KEY] ?? null;
44+
if (strncmp($presenter, 'Nette:', 6)) {
45+
$params[self::PRESENTER_KEY] = $this->module . $presenter;
4446
}
45-
return $appRequest;
47+
return $params;
4648
}
4749
}
4850
return null;
4951
}
5052

5153

5254
/**
53-
* Constructs absolute URL from Request object.
55+
* Constructs absolute URL from array.
5456
*/
55-
public function constructUrl(Nette\Application\Request $appRequest, Nette\Http\Url $refUrl): ?string
57+
public function constructUrl(array $params, Nette\Http\Url $refUrl): ?string
5658
{
5759
if ($this->cachedRoutes === null) {
5860
$this->warmupCache();
5961
}
6062

6163
if ($this->module) {
62-
if (strncmp($tmp = $appRequest->getPresenterName(), $this->module, strlen($this->module)) === 0) {
63-
$appRequest = clone $appRequest;
64-
$appRequest->setPresenterName(substr($tmp, strlen($this->module)));
64+
if (strncmp($params[self::PRESENTER_KEY], $this->module, strlen($this->module)) === 0) {
65+
$params[self::PRESENTER_KEY] = substr($params[self::PRESENTER_KEY], strlen($this->module));
6566
} else {
6667
return null;
6768
}
6869
}
6970

70-
$presenter = $appRequest->getPresenterName();
71+
$presenter = $params[self::PRESENTER_KEY];
7172
if (!isset($this->cachedRoutes[$presenter])) {
7273
$presenter = '*';
7374
}
7475

7576
foreach ($this->cachedRoutes[$presenter] as $route) {
76-
$url = $route->constructUrl($appRequest, $refUrl);
77+
$url = $route->constructUrl($params, $refUrl);
7778
if ($url !== null) {
7879
return $url;
7980
}

src/Application/Routers/SimpleRouter.php

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,9 @@ public function __construct($defaults = [], int $flags = 0)
5858

5959

6060
/**
61-
* Maps HTTP request to a Request object.
61+
* Maps HTTP request to an array.
6262
*/
63-
public function match(Nette\Http\IRequest $httpRequest): ?Application\Request
63+
public function match(Nette\Http\IRequest $httpRequest): ?array
6464
{
6565
if ($httpRequest->getUrl()->getPathInfo() !== '') {
6666
return null;
@@ -69,38 +69,28 @@ public function match(Nette\Http\IRequest $httpRequest): ?Application\Request
6969
$params = $httpRequest->getQuery();
7070
$params += $this->defaults;
7171

72-
if (!isset($params[self::PRESENTER_KEY]) || !is_string($params[self::PRESENTER_KEY])) {
72+
$presenter = $params[self::PRESENTER_KEY] ?? null;
73+
if (!is_string($presenter)) {
7374
return null;
7475
}
7576

76-
$presenter = $this->module . $params[self::PRESENTER_KEY];
77-
unset($params[self::PRESENTER_KEY]);
78-
79-
return new Application\Request(
80-
$presenter,
81-
$httpRequest->getMethod(),
82-
$params,
83-
$httpRequest->getPost(),
84-
$httpRequest->getFiles(),
85-
[Application\Request::SECURED => $httpRequest->isSecured()]
86-
);
77+
$params[self::PRESENTER_KEY] = $this->module . $presenter;
78+
return $params;
8779
}
8880

8981

9082
/**
91-
* Constructs absolute URL from Request object.
83+
* Constructs absolute URL from array.
9284
*/
93-
public function constructUrl(Application\Request $appRequest, Nette\Http\Url $refUrl): ?string
85+
public function constructUrl(array $params, Nette\Http\Url $refUrl): ?string
9486
{
9587
if ($this->flags & self::ONE_WAY) {
9688
return null;
9789
}
98-
$params = $appRequest->getParameters();
9990

10091
// presenter name
101-
$presenter = $appRequest->getPresenterName();
102-
if (strncmp($presenter, $this->module, strlen($this->module)) === 0) {
103-
$params[self::PRESENTER_KEY] = substr($presenter, strlen($this->module));
92+
if (strncmp($params[self::PRESENTER_KEY], $this->module, strlen($this->module)) === 0) {
93+
$params[self::PRESENTER_KEY] = substr($params[self::PRESENTER_KEY], strlen($this->module));
10494
} else {
10595
return null;
10696
}

src/Application/UI/Presenter.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ abstract class Presenter extends Control implements Application\IPresenter
3939

4040
/** @internal special parameter key */
4141
public const
42+
PRESENTER_KEY = 'presenter',
4243
SIGNAL_KEY = 'do',
4344
ACTION_KEY = 'action',
4445
FLASH_KEY = '_fid',
@@ -916,10 +917,10 @@ protected function requestToUrl(Application\Request $request, bool $relative = n
916917
throw new Nette\InvalidStateException('Unable to generate URL, service Router has not been set.');
917918
}
918919

919-
$url = $this->router->constructUrl($request, $this->refUrlCache);
920+
$url = $this->router->constructUrl($request->toArray(), $this->refUrlCache);
920921
if ($url === null) {
921922
$params = $request->getParameters();
922-
unset($params[self::ACTION_KEY]);
923+
unset($params[self::ACTION_KEY], $params[self::PRESENTER_KEY]);
923924
$params = urldecode(http_build_query($params, '', ', '));
924925
throw new InvalidLinkException("No route for {$request->getPresenterName()}:{$request->getParameter('action')}($params)");
925926
}

0 commit comments

Comments
 (0)