-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrouter.php
More file actions
119 lines (100 loc) · 3.24 KB
/
Copy pathrouter.php
File metadata and controls
119 lines (100 loc) · 3.24 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
<?php
declare(strict_types=1);
final class Router
{
/**
* @var array<string, array<string, mixed>>
*/
private array $routes = [];
/**
* @param array<string, array<string, mixed>> $routes
*/
public function __construct(array $routes = [])
{
foreach ($routes as $path => $route) {
$this->add($path, $route);
}
}
/**
* @param array<string, mixed> $route
*/
public function add(string $path, array $route): void
{
$this->routes[$this->normalizePath($path)] = $route;
}
/**
* @return array<string, array<string, mixed>>
*/
public function routes(): array
{
return $this->routes;
}
public function currentPath(?string $requestUri = null): string
{
$path = parse_url($requestUri ?? ($_SERVER['REQUEST_URI'] ?? '/'), PHP_URL_PATH);
return $this->normalizePath((string) $path);
}
public function routeUrl(string $path): string
{
$path = $this->normalizePath($path);
return $path === '/' ? '/' : $path;
}
public function projectRouteUrl(string $route, string $slug): string
{
return rtrim($this->routeUrl($route), '/') . '/' . rawurlencode($slug);
}
/**
* @param array<string, mixed> $projectsConfig
* @return array{route: ?array<string, mixed>, project: ?array<string, mixed>, project_slug: ?string, error: ?string}
*/
public function resolve(string $path, array $projectsConfig = []): array
{
$path = $this->normalizePath($path);
if (isset($this->routes[$path])) {
return [
'route' => $this->routes[$path],
'project' => null,
'project_slug' => null,
'error' => null,
];
}
if (preg_match('#^/(kanban|roadmap|design|next|agents)/([^/]+)$#', $path, $matches) !== 1) {
return [
'route' => null,
'project' => null,
'project_slug' => null,
'error' => 'This page does not exist.',
];
}
$basePath = '/' . $matches[1];
$slug = rawurldecode($matches[2]);
$projects = is_array($projectsConfig['projects'] ?? null) ? $projectsConfig['projects'] : [];
if (!isset($this->routes[$basePath])) {
return [
'route' => null,
'project' => null,
'project_slug' => $slug,
'error' => 'This page does not exist.',
];
}
if (!isset($projects[$slug]) || !empty($projects[$slug]['missing'])) {
return [
'route' => $this->routes[$basePath],
'project' => null,
'project_slug' => $slug,
'error' => 'The selected project does not exist: ' . $slug,
];
}
return [
'route' => $this->routes[$basePath],
'project' => is_array($projects[$slug]) ? $projects[$slug] : null,
'project_slug' => $slug,
'error' => null,
];
}
private function normalizePath(string $path): string
{
$path = '/' . trim($path, '/');
return $path === '/' ? '/' : rtrim($path, '/');
}
}