-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSerializedRouteCollection.php
142 lines (113 loc) · 3.29 KB
/
SerializedRouteCollection.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
<?php
declare(strict_types=1);
namespace Snicco\Component\HttpRouting\Routing\Route;
use ArrayIterator;
use Snicco\Component\HttpRouting\Routing\Exception\RouteNotFound;
use Traversable;
use Webmozart\Assert\Assert;
use function count;
use function get_class;
use function gettype;
use function is_object;
use function unserialize;
/**
* @psalm-external-mutation-free
*
* @psalm-internal Snicco\Component\HttpRouting
* @interal
*/
final class SerializedRouteCollection implements Routes
{
/**
* @var array<string,string>
*/
private array $serialized_routes = [];
/**
* @var array<string,Route>
*/
private array $hydrated_routes = [];
/**
* @param array<string,string> $serialized_routes
*/
public function __construct(array $serialized_routes)
{
$this->serialized_routes = $serialized_routes;
$this->hydrated_routes = [];
}
public function toArray(): array
{
if ($this->isFullyHydrated()) {
return $this->hydrated_routes;
}
$routes = $this->hydrateAll();
$this->hydrated_routes = $routes;
return $this->hydrated_routes;
}
public function count(): int
{
return count($this->serialized_routes);
}
public function getByName(string $name): Route
{
if (isset($this->hydrated_routes[$name])) {
return $this->hydrated_routes[$name];
}
if (isset($this->serialized_routes[$name])) {
$route = unserialize($this->serialized_routes[$name]);
$this->checkIsValidRoute($route);
$this->checkValidName($name, $route);
$this->hydrated_routes[$name] = $route;
return $route;
}
throw RouteNotFound::name($name);
}
public function getIterator(): Traversable
{
return new ArrayIterator($this->toArray());
}
private function isFullyHydrated(): bool
{
return count($this->hydrated_routes) === count($this->serialized_routes);
}
/**
* @return array<string,Route>
*/
private function hydrateAll(): array
{
$_routes = [];
foreach ($this->serialized_routes as $name => $route) {
if (isset($this->hydrated_routes[$name])) {
$_routes[$name] = $this->hydrated_routes[$name];
continue;
}
/** @var false|mixed|Route $route */
$route = unserialize($route);
$this->checkIsValidRoute($route);
$this->checkValidName($name, $route);
$_routes[$name] = $route;
}
return $_routes;
}
/**
* @psalm-assert Route $route
*
* @param mixed $route
*/
private function checkIsValidRoute($route): void
{
Assert::isInstanceOf(
$route,
Route::class,
sprintf(
"Your route cache seems corrupted.\nThe cached route collection contained a serialized of type [%s].",
is_object($route) ? get_class($route) : gettype($route)
)
);
}
private function checkValidName(string $used_name, Route $route): void
{
if ($route->getName() !== $used_name) {
throw RouteNotFound::accessByBadName($used_name, $route->getName());
}
}
}