-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRouter.php
152 lines (124 loc) · 4.87 KB
/
Router.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
<?php
declare(strict_types=1);
/*
* This file is part of the "Extended Base Class Library for Symphony CMS" repository.
*
* Copyright 2020-2021 Alannah Kearney <hi@alannahkearney.com>
*
* For the full copyright and license information, please view the LICENCE
* file that was distributed with this source code.
*/
namespace pointybeard\Symphony\Extended;
use Symfony\Component\HttpFoundation;
class Router implements Interfaces\RouterInterface
{
protected $routes = [];
public function add(Route $route): self
{
$this->routes[] = $route;
return $this;
}
public function find(HttpFoundation\Request $request, ?int $flags = null): Route
{
foreach ($this->routes as $route) {
if (true == $route->match($request, $flags)) {
return $route;
}
}
// Determine if there is no route at all, or if its just because
// the request method is not allowed.
$methodsAllowed = null;
foreach ($this->routes as $route) {
if (true == $route->match($request, $flags | Route::SKIP_METHOD_MATCH)) {
$methodsAllowed = $methodsAllowed | $route->methods();
}
}
if (null !== $methodsAllowed) {
throw new Exceptions\MethodNotAllowedException($request->getMethod());
}
throw new Exceptions\MatchingRouteNotFound();
}
// This method will use the Page entries in Symphony to build
// a set of default routes. They are simplistic and have no
// validation attached but fill the gap for any routes that aren't defined
// manually
public function buildDefaultRoutes(): self
{
$pages = \PageManager::fetch();
foreach ($pages as $page) {
$page = (object) $page;
// Ignore anything that isn't a JSON page
if (false == is_array($page->type) || false == in_array('JSON', $page->type)) {
continue;
}
// See if there is already a route defined for this page
$params = [];
if (false == empty($page->params)) {
$params = explode('/', trim($page->params, '/'));
$params = array_map(function (string $item) {return "{{$item}}"; }, $params);
}
// Note: Symphony allows all, some, or no params in a request. e.g.
// if page is /p/{a}/{b}/{c}, then /p/{a}, /p/{a}/{b}, and /p/{a}/{b}/{c}
// are all possible. We'll need to check for and add a route for each of these
// variations
$variations = [
'/'.trim(implode('/', [$page->path, $page->handle]), '/'),
];
$chain = [];
foreach ($params as $p) {
$chain[] = $p;
$variations[] = '/'.trim(implode('/', [$page->path, $page->handle, implode('/', $chain)]), '/');
}
foreach ($variations as $url) {
// Create a dummy request for this variation. Method doesn't matter.
$request = HttpFoundation\Request::create($url, HttpFoundation\Request::METHOD_GET);
$match = false;
foreach ($this->routes as $r) {
$match = $r->match(
$request,
Route:: SKIP_METHOD_MATCH
| Route::SKIP_REGEX_VALIDATION
| Route::TOKENIZER_USE_SYMPHONY_COMPATIBLE_URL
);
if (true == $match) {
break;
}
}
// A match is found, but there are some methods it doesn't handle
// i.e. the difference of Route::METHOD_ALL and $r->methods is not
// int(0)
if (true == $match && Route::METHOD_ALL !== $r->methods()) {
$this->add(
(new Route())
->url($request->getPathInfo())
->methods(Route::METHOD_ALL ^ $r->methods())
);
// No match was found at all
} elseif (false == $match) {
$this->add(
(new Route())
->url($request->getPathInfo())
->methods(Route::METHOD_ALL)
);
}
}
}
return $this;
}
public function toArray(): array
{
$result = [];
foreach ($this->routes as $r) {
$result[] = $r->toArray();
}
return $result;
}
public function toJson(): string
{
return (string) json_encode($this->toArray(), JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT);
}
public function __toString()
{
return $this->toJson();
}
}