/
RouteCollection.php
169 lines (134 loc) · 4.39 KB
/
RouteCollection.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
<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\Http;
use FastRoute\DataGenerator;
use FastRoute\RouteParser;
use Illuminate\Support\Arr;
/**
* @internal
*/
class RouteCollection
{
/**
* @var array
*/
protected $reverse = [];
/**
* @var DataGenerator
*/
protected $dataGenerator;
/**
* @var RouteParser
*/
protected $routeParser;
/**
* @var array
*/
protected $routes = [];
/**
* @var array
*/
protected $pendingRoutes = [];
public function __construct()
{
$this->dataGenerator = new DataGenerator\GroupCountBased;
$this->routeParser = new RouteParser\Std;
}
public function get($path, $name, $handler)
{
return $this->addRoute('GET', $path, $name, $handler);
}
public function post($path, $name, $handler)
{
return $this->addRoute('POST', $path, $name, $handler);
}
public function put($path, $name, $handler)
{
return $this->addRoute('PUT', $path, $name, $handler);
}
public function patch($path, $name, $handler)
{
return $this->addRoute('PATCH', $path, $name, $handler);
}
public function delete($path, $name, $handler)
{
return $this->addRoute('DELETE', $path, $name, $handler);
}
public function addRoute($method, $path, $name, $handler)
{
if (isset($this->routes[$name])) {
throw new \RuntimeException("Route $name already exists");
}
$this->routes[$name] = $this->pendingRoutes[$name] = compact('method', 'path', 'handler');
return $this;
}
public function removeRoute(string $name): self
{
unset($this->routes[$name], $this->pendingRoutes[$name]);
return $this;
}
protected function applyRoutes(): void
{
foreach ($this->pendingRoutes as $name => $route) {
$routeDatas = $this->routeParser->parse($route['path']);
foreach ($routeDatas as $routeData) {
$this->dataGenerator->addRoute($route['method'], $routeData, ['name' => $name, 'handler' => $route['handler']]);
}
$this->reverse[$name] = $routeDatas;
}
$this->pendingRoutes = [];
}
public function getRoutes(): array
{
return $this->routes;
}
public function getRouteData()
{
if (! empty($this->pendingRoutes)) {
$this->applyRoutes();
}
return $this->dataGenerator->getData();
}
protected function fixPathPart($part, array $parameters, string $routeName)
{
if (! is_array($part)) {
return $part;
}
if (! array_key_exists($part[0], $parameters)) {
throw new \InvalidArgumentException("Could not generate URL for route '$routeName': no value provided for required part '$part[0]'.");
}
return $parameters[$part[0]];
}
public function getPath($name, array $parameters = [])
{
if (! empty($this->pendingRoutes)) {
$this->applyRoutes();
}
if (isset($this->reverse[$name])) {
$maxMatches = 0;
$matchingParts = $this->reverse[$name][0];
// For a given route name, we want to choose the option that best matches the given parameters.
// Each routing option is an array of parts. Each part is either a constant string
// (which we don't care about here), or an array where the first element is the parameter name
// and the second element is a regex into which the parameter value is inserted, if the parameter matches.
foreach ($this->reverse[$name] as $parts) {
foreach ($parts as $i => $part) {
if (is_array($part) && Arr::exists($parameters, $part[0]) && $i > $maxMatches) {
$maxMatches = $i;
$matchingParts = $parts;
}
}
}
$fixedParts = array_map(function ($part) use ($parameters, $name) {
return $this->fixPathPart($part, $parameters, $name);
}, $matchingParts);
return '/'.ltrim(implode('', $fixedParts), '/');
}
throw new \RuntimeException("Route $name not found");
}
}