-
Notifications
You must be signed in to change notification settings - Fork 452
Expand file tree
/
Copy pathFastRoute.php
More file actions
203 lines (179 loc) · 5.95 KB
/
FastRoute.php
File metadata and controls
203 lines (179 loc) · 5.95 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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
<?php
declare(strict_types=1);
namespace FastRoute;
use Closure;
use FastRoute\Cache\FileCache;
use function assert;
use function is_string;
/** @phpstan-import-type ProcessedData from ConfigureRoutes */
final class FastRoute
{
/** @var ProcessedData|null */
private ?array $processedConfiguration = null;
/**
* @param Closure(ConfigureRoutes):void $routeDefinitionCallback
* @param class-string<RouteParser> $routeParser
* @param class-string<DataGenerator> $dataGenerator
* @param class-string<Dispatcher> $dispatcher
* @param class-string<ConfigureRoutes> $configureRoutes
* @param class-string<GenerateUri> $uriGenerator
* @param Cache|class-string<Cache>|null $cacheDriver
* @param non-empty-string|null $cacheKey
*/
private function __construct(
private readonly Closure $routeDefinitionCallback,
private readonly string $routeParser,
private readonly string $dataGenerator,
private readonly string $dispatcher,
private readonly string $configureRoutes,
private readonly string $uriGenerator,
private readonly Cache|string|null $cacheDriver,
private readonly ?string $cacheKey,
) {
}
/**
* @param Closure(ConfigureRoutes):void $routeDefinitionCallback
* @param non-empty-string $cacheKey
*/
public static function recommendedSettings(Closure $routeDefinitionCallback, string $cacheKey): self
{
return new self(
$routeDefinitionCallback,
RouteParser\Std::class,
DataGenerator\MarkBased::class,
Dispatcher\MarkBased::class,
RouteCollector::class,
GenerateUri\FromProcessedConfiguration::class,
FileCache::class,
$cacheKey,
);
}
public function disableCache(): self
{
return new self(
$this->routeDefinitionCallback,
$this->routeParser,
$this->dataGenerator,
$this->dispatcher,
$this->configureRoutes,
$this->uriGenerator,
null,
null,
);
}
/**
* @param Cache|class-string<Cache> $driver
* @param non-empty-string $cacheKey
*/
public function withCache(Cache|string $driver, string $cacheKey): self
{
return new self(
$this->routeDefinitionCallback,
$this->routeParser,
$this->dataGenerator,
$this->dispatcher,
$this->configureRoutes,
$this->uriGenerator,
$driver,
$cacheKey,
);
}
public function useCharCountDispatcher(): self
{
return $this->useCustomDispatcher(DataGenerator\CharCountBased::class, Dispatcher\CharCountBased::class);
}
public function useGroupCountDispatcher(): self
{
return $this->useCustomDispatcher(DataGenerator\GroupCountBased::class, Dispatcher\GroupCountBased::class);
}
public function useGroupPosDispatcher(): self
{
return $this->useCustomDispatcher(DataGenerator\GroupPosBased::class, Dispatcher\GroupPosBased::class);
}
public function useMarkDispatcher(): self
{
return $this->useCustomDispatcher(DataGenerator\MarkBased::class, Dispatcher\MarkBased::class);
}
/**
* @param class-string<DataGenerator> $dataGenerator
* @param class-string<Dispatcher> $dispatcher
*/
public function useCustomDispatcher(string $dataGenerator, string $dispatcher): self
{
return new self(
$this->routeDefinitionCallback,
$this->routeParser,
$dataGenerator,
$dispatcher,
$this->configureRoutes,
$this->uriGenerator,
$this->cacheDriver,
$this->cacheKey,
);
}
/**
* @param class-string<ConfigureRoutes> $configureRoutes
*/
public function useCustomConfigureRoutes(string $configureRoutes): self
{
return new self(
$this->routeDefinitionCallback,
$this->routeParser,
$this->dataGenerator,
$this->dispatcher,
$configureRoutes,
$this->uriGenerator,
$this->cacheDriver,
$this->cacheKey,
);
}
/** @param class-string<GenerateUri> $uriGenerator */
public function withUriGenerator(string $uriGenerator): self
{
return new self(
$this->routeDefinitionCallback,
$this->routeParser,
$this->dataGenerator,
$this->dispatcher,
$this->configureRoutes,
$uriGenerator,
$this->cacheDriver,
$this->cacheKey,
);
}
/** @return ProcessedData */
public function processedConfiguration(): array
{
if ($this->processedConfiguration !== null) {
return $this->processedConfiguration;
}
$loader = function (): array {
$configureRoutes = $this->configureRoutes();
($this->routeDefinitionCallback)($configureRoutes);
return $configureRoutes->processedRoutes();
};
if ($this->cacheDriver === null) {
return $this->processedConfiguration = $loader();
}
assert(is_string($this->cacheKey));
$cache = is_string($this->cacheDriver)
? new $this->cacheDriver()
: $this->cacheDriver;
return $this->processedConfiguration = $cache->get($this->cacheKey, $loader);
}
public function configureRoutes(): ConfigureRoutes
{
return new $this->configureRoutes(
new $this->routeParser(),
new $this->dataGenerator(),
);
}
public function dispatcher(): Dispatcher
{
return new $this->dispatcher($this->processedConfiguration());
}
public function uriGenerator(): GenerateUri
{
return new $this->uriGenerator($this->processedConfiguration()[2]);
}
}