/
RouteList.php
156 lines (123 loc) 路 3.09 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
152
153
154
155
156
<?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 Nette;
/**
* The router broker.
*/
class RouteList extends Nette\Routing\RouteList implements Nette\Routing\Router, \ArrayAccess, \Countable, \IteratorAggregate
{
private const PRESENTER_KEY = 'presenter';
/** @var string|null */
private $module;
public function __construct(string $module = null)
{
parent::__construct();
$this->module = $module ? $module . ':' : null;
}
/**
* Maps HTTP request to an array.
*/
public function match(Nette\Http\IRequest $httpRequest): ?array
{
$params = parent::match($httpRequest);
$presenter = $params[self::PRESENTER_KEY] ?? null;
if (is_string($presenter) && strncmp($presenter, 'Nette:', 6)) {
$params[self::PRESENTER_KEY] = $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::PRESENTER_KEY], $this->module, strlen($this->module)) === 0) {
$params[self::PRESENTER_KEY] = substr($params[self::PRESENTER_KEY], strlen($this->module));
} else {
return null;
}
}
return parent::constructUrl($params, $refUrl);
}
/**
* @param string $mask e.g. '<presenter>/<action>/<id \d{1,3}>'
* @param array|string|\Closure $metadata default values or metadata or callback for NetteModule\MicroPresenter
* @return static
*/
public function addRoute(string $mask, $metadata = [], int $flags = 0)
{
$this->add(new Route($mask, $metadata), $flags);
return $this;
}
/**
* @return static
*/
public function withModule(string $module)
{
$router = new static;
$router->module = $module . ':';
$router->parent = $this;
$this->add($router);
return $router;
}
public function getModule(): ?string
{
return $this->module;
}
public function count(): int
{
return count($this->getRouters());
}
/**
* @param mixed $index
* @param Nette\Routing\Router $router
*/
public function offsetSet($index, $router): void
{
if ($index === null) {
$this->add($router);
} else {
$this->modify($index, $router);
}
}
/**
* @param int $index
* @return mixed
* @throws Nette\OutOfRangeException
*/
public function offsetGet($index)
{
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
{
return is_int($index) && $index >= 0 && $index < $this->count();
}
/**
* @param int $index
* @throws Nette\OutOfRangeException
*/
public function offsetUnset($index): void
{
if (!$this->offsetExists($index)) {
throw new Nette\OutOfRangeException('Offset invalid or out of range');
}
$this->modify($index, null);
}
public function getIterator(): \ArrayIterator
{
return new \ArrayIterator($this->getRouters());
}
}