-
-
Notifications
You must be signed in to change notification settings - Fork 116
Expand file tree
/
Copy pathRoute.php
More file actions
189 lines (156 loc) · 4.88 KB
/
Route.php
File metadata and controls
189 lines (156 loc) · 4.88 KB
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
<?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 bidirectional route is responsible for mapping
* HTTP request to an array for dispatch and vice-versa.
*/
class Route extends Nette\Routing\Route implements Nette\Routing\Router
{
private const
PresenterKey = 'presenter',
ModuleKey = 'module';
private const UIMeta = [
'module' => [
self::Pattern => '[a-z][a-z0-9.-]*',
self::FilterIn => [self::class, 'path2presenter'],
self::FilterOut => [self::class, 'presenter2path'],
],
'presenter' => [
self::Pattern => '[a-z][a-z0-9.-]*',
self::FilterIn => [self::class, 'path2presenter'],
self::FilterOut => [self::class, 'presenter2path'],
],
'action' => [
self::Pattern => '[a-z][a-z0-9-]*',
self::FilterIn => [self::class, 'path2action'],
self::FilterOut => [self::class, 'action2path'],
],
];
/**
* @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
*/
public function __construct(string $mask, array|string|\Closure $metadata = [])
{
if (is_string($metadata)) {
[$presenter, $action] = Nette\Application\Helpers::splitName($metadata);
if (!$presenter) {
throw new Nette\InvalidArgumentException("Second argument must be array or string in format Presenter:action, '$metadata' given.");
}
$metadata = [self::PresenterKey => $presenter];
if ($action !== '') {
$metadata['action'] = $action;
}
} elseif ($metadata instanceof \Closure) {
$metadata = [
self::PresenterKey => 'Nette:Micro',
'callback' => $metadata,
];
}
$this->defaultMeta += self::UIMeta;
parent::__construct($mask, $metadata);
}
/**
* Maps HTTP request to an array.
*/
public function match(Nette\Http\IRequest $httpRequest): ?array
{
$params = parent::match($httpRequest);
if ($params === null) {
return null;
} elseif (!isset($params[self::PresenterKey])) {
throw new Nette\InvalidStateException('Missing presenter in route definition.');
} elseif (!is_string($params[self::PresenterKey])) {
return null;
}
$presenter = $params[self::PresenterKey] ?? null;
if (isset($this->getMetadata()[self::ModuleKey], $params[self::ModuleKey]) && is_string($presenter)) {
$params[self::PresenterKey] = $params[self::ModuleKey] . ':' . $params[self::PresenterKey];
}
unset($params[self::ModuleKey]);
return $params;
}
/**
* Constructs absolute URL from array.
*/
public function constructUrl(array $params, Nette\Http\UrlScript $refUrl): ?string
{
$metadata = $this->getMetadata();
if (isset($metadata[self::ModuleKey])) { // try split into module and [submodule:]presenter parts
$presenter = $params[self::PresenterKey];
$module = $metadata[self::ModuleKey];
$a = isset($module['fixity'], $module[self::Value])
&& strncmp($presenter, $module[self::Value] . ':', strlen($module[self::Value]) + 1) === 0
? strlen($module[self::Value])
: strrpos($presenter, ':');
if ($a === false) {
$params[self::ModuleKey] = isset($module[self::Value]) ? '' : null;
} else {
$params[self::ModuleKey] = substr($presenter, 0, $a);
$params[self::PresenterKey] = substr($presenter, $a + 1);
}
}
return parent::constructUrl($params, $refUrl);
}
/** @internal */
public function getConstantParameters(): array
{
$res = parent::getConstantParameters();
if (isset($res[self::ModuleKey], $res[self::PresenterKey])) {
$res[self::PresenterKey] = $res[self::ModuleKey] . ':' . $res[self::PresenterKey];
} elseif (isset($this->getMetadata()[self::ModuleKey])) {
unset($res[self::PresenterKey]);
}
unset($res[self::ModuleKey]);
return $res;
}
/********************* Inflectors ****************d*g**/
/**
* camelCaseAction name -> dash-separated.
*/
public static function action2path(string $s): string
{
$s = preg_replace('#(.)(?=[A-Z])#', '$1-', $s);
$s = strtolower($s);
$s = rawurlencode($s);
return $s;
}
/**
* dash-separated -> camelCaseAction name.
*/
public static function path2action(string $s): string
{
$s = preg_replace('#-(?=[a-z])#', ' ', $s);
$s = lcfirst(ucwords($s));
$s = str_replace(' ', '', $s);
return $s;
}
/**
* PascalCase:Presenter name -> dash-and-dot-separated.
*/
public static function presenter2path(string $s): string
{
$s = strtr($s, ':', '.');
$s = preg_replace('#([^.])(?=[A-Z])#', '$1-', $s);
$s = strtolower($s);
$s = rawurlencode($s);
return $s;
}
/**
* dash-and-dot-separated -> PascalCase:Presenter name.
*/
public static function path2presenter(string $s): string
{
$s = preg_replace('#([.-])(?=[a-z])#', '$1 ', $s);
$s = ucwords($s);
$s = str_replace('. ', ':', $s);
$s = str_replace('- ', '', $s);
return $s;
}
}