This repository was archived by the owner on Jan 28, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathManifest.php
More file actions
139 lines (110 loc) · 3.98 KB
/
Manifest.php
File metadata and controls
139 lines (110 loc) · 3.98 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
<?php
namespace Spatian\Ziglite\Routes;
use Illuminate\Support\Str;
use Illuminate\Routing\Router;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\App;
use JsonSerializable;
use stdClass;
final class Manifest implements JsonSerializable {
private const UNFILTERED = 'default';
protected static array $cache = [];
private string $base;
private array $filters;
private string $filters_hash;
private array $routes;
private array $defaults;
public function __construct(
array|string $filters = [],
string $base = null
) {
$url = App::make('url');
$base = Str::of($base);
$this->base = rtrim($base->isEmpty() ? $url->to('/') : $base, '/');
$this->processFilters(Arr::wrap($filters));
$this->defaults = method_exists($url, 'getDefaultParameters') ?
$url->getDefaultParameters() : [];
if (!array_key_exists($this->filters_hash, self::$cache))
$this->routes = $this->applyFilters();
$this->routes = self::$cache[$this->filters_hash];
}
public static function clearRoutes() {
static::$cache = [];
}
public function jsonSerialize(): array {
return $this->toArray();
}
public function toArray(): array {
$cls = new stdClass();
return [
'base' => $this->base,
'routes' => (count($this->routes) > 0) ? $this->routes : $cls,
'defaults' => (count($this->defaults) > 0) ? $this->defaults : $cls,
];
}
/**
* @throws \JsonException
* */
public function toJson(int $options = 0): string {
return json_encode($this, JSON_THROW_ON_ERROR | $options);
}
private function processFilters(array $filters): void {
$this->filters_hash = (count($filters) > 0) ?
$this->filters_hash = md5(implode('|', $filters)) :
$this->filters_hash = self::UNFILTERED;
array_walk($filters, function (mixed &$pattern, int $i) {
$pattern = Str::of($pattern);
$exclude = $pattern->startsWith('!');
$pattern = (string) ($exclude ? $pattern->substr(1) : $pattern);
$pattern = compact('pattern', 'exclude');
});
$this->filters = $filters;
}
private function applyFilters(): array {
if (!array_key_exists(self::UNFILTERED, self::$cache))
self::$cache[self::UNFILTERED] = $this->namedRoutes();
self::$cache[$this->filters_hash] = [];
foreach (self::$cache[self::UNFILTERED] as $name => $details) {
$pass = false;
foreach ($this->filters as ['pattern' => $pattern, 'exclude' => $exclude]) {
if (!Str::is($pattern, $name))
continue;
$pass = !$exclude;
if ($exclude)
break;
}
if (!$pass)
continue;
self::$cache[$this->filters_hash][$name] = $details;
}
return self::$cache[$this->filters_hash];
}
private function namedRoutes(): array {
$router = App::make('router');
/** @var Router $router */
$cls = new stdClass();
$routes = [];
$fallback = [];
foreach ($router->getRoutes()->getRoutesByName() as $name => $route) {
if (Str::of($name)->isEmpty())
continue;
if ($route->isFallback) {
$fallback[$name] = $route;
continue;
}
$routes[$name] = [
'uri' => $route->uri,
'domain' => $route->domain(),
'wheres' => (count($route->wheres) > 0) ? $route->wheres : $cls,
];
}
foreach ($fallback as $name => $route) {
$routes[$name] = [
'uri' => $route->uri,
'domain' => $route->domain(),
'wheres' => (count($route->wheres) > 0) ? $route->wheres : $cls,
];
}
return $routes;
}
}