-
-
Notifications
You must be signed in to change notification settings - Fork 106
/
Copy pathRouteList.php
151 lines (118 loc) · 3.32 KB
/
RouteList.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
<?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\Routers;
use JetBrains\PhpStorm\Language;
use Nette;
/**
* The router broker.
*/
class RouteList extends Nette\Routing\RouteList implements Nette\Routing\Router, \ArrayAccess
{
private const PresenterKey = 'presenter';
private ?string $module;
public function __construct(?string $module = null)
{
parent::__construct();
$this->module = $module ? $module . ':' : null;
}
/**
* Support for modules.
*/
protected function completeParameters(array $params): ?array
{
$presenter = $params[self::PresenterKey] ?? null;
if (is_string($presenter) && strncmp($presenter, 'Nette:', 6)) {
$params[self::PresenterKey] = $this->module . $presenter;
}
return $params;
}
/**
* Constructs absolute URL from array.
*/
public function constructUrl(array $params, Nette\Http\UrlScript $refUrl): ?string
{
if ($this->module) {
if (strncmp($params[self::PresenterKey], $this->module, strlen($this->module)) !== 0) {
return null;
}
$params[self::PresenterKey] = substr($params[self::PresenterKey], strlen($this->module));
}
return parent::constructUrl($params, $refUrl);
}
public function addRoute(
#[Language('TEXT')]
string $mask,
array|string|\Closure $metadata = [],
int|bool $oneWay = 0,
): static
{
$this->add(new Route($mask, $metadata), (int) $oneWay);
return $this;
}
public function withModule(string $module): static
{
$router = new static;
$router->module = $module . ':';
$router->parent = $this;
$this->add($router);
return $router;
}
public function getModule(): ?string
{
return $this->module;
}
/**
* @param mixed $index
* @param Nette\Routing\Router $router
*/
public function offsetSet($index, $router): void
{
if ($router instanceof Route) {
trigger_error('Usage `$router[] = new Route(...)` is deprecated, use `$router->addRoute(...)`.', E_USER_DEPRECATED);
} else {
$class = getclass($router);
trigger_error("Usage `\$router[] = new $class` is deprecated, use `\$router->add(new $class)`.", E_USER_DEPRECATED);
}
if ($index === null) {
$this->add($router);
} else {
$this->modify($index, $router);
}
}
/**
* @param int $index
* @throws Nette\OutOfRangeException
*/
public function offsetGet($index): Nette\Routing\Router
{
trigger_error('Usage `$route = $router[...]` is deprecated, use `$router->getRouters()`.', E_USER_DEPRECATED);
if (!$this->offsetExists($index)) {
throw new Nette\OutOfRangeException('Offset invalid or out of range');
}
return $this->getRouters()[$index];
}
/**
* @param int $index
*/
public function offsetExists($index): bool
{
trigger_error('Usage `isset($router[...])` is deprecated.', E_USER_DEPRECATED);
return is_int($index) && $index >= 0 && $index < count($this->getRouters());
}
/**
* @param int $index
* @throws Nette\OutOfRangeException
*/
public function offsetUnset($index): void
{
trigger_error('Usage `unset($router[$index])` is deprecated, use `$router->modify($index, null)`.', E_USER_DEPRECATED);
if (!$this->offsetExists($index)) {
throw new Nette\OutOfRangeException('Offset invalid or out of range');
}
$this->modify($index, null);
}
}