-
Notifications
You must be signed in to change notification settings - Fork 447
/
Copy pathfunctions.php
78 lines (64 loc) · 3.29 KB
/
functions.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
<?php
declare(strict_types=1);
namespace FastRoute;
use FastRoute\Cache\FileCache;
use LogicException;
use function function_exists;
use function is_string;
if (! function_exists('FastRoute\simpleDispatcher')) {
/**
* @deprecated since v2.0 and will be removed in v3.0
*
* @see FastRoute::recommendedSettings()
* @see FastRoute::disableCache()
*
* @param callable(ConfigureRoutes):void $routeDefinitionCallback
* @param array{routeParser?: class-string<RouteParser>, dataGenerator?: class-string<DataGenerator>, dispatcher?: class-string<Dispatcher>, routeCollector?: class-string<ConfigureRoutes>, cacheDisabled?: bool, cacheKey?: string, cacheFile?: string, cacheDriver?: class-string<Cache>|Cache} $options
*/
function simpleDispatcher(callable $routeDefinitionCallback, array $options = []): Dispatcher
{
return \FastRoute\cachedDispatcher(
$routeDefinitionCallback,
['cacheDisabled' => true] + $options,
);
}
/**
* @deprecated since v2.0 and will be removed in v3.0
*
* @see FastRoute::recommendedSettings()
*
* @param callable(ConfigureRoutes):void $routeDefinitionCallback
* @param array{routeParser?: class-string<RouteParser>, dataGenerator?: class-string<DataGenerator>, dispatcher?: class-string<Dispatcher>, routeCollector?: class-string<ConfigureRoutes>, cacheDisabled?: bool, cacheKey?: string, cacheFile?: string, cacheDriver?: class-string<Cache>|Cache} $options
*/
function cachedDispatcher(callable $routeDefinitionCallback, array $options = []): Dispatcher
{
$options += [
'routeParser' => RouteParser\Std::class,
'dataGenerator' => DataGenerator\MarkBased::class,
'dispatcher' => Dispatcher\MarkBased::class,
'routeCollector' => RouteCollector::class,
'cacheDisabled' => false,
'cacheDriver' => FileCache::class,
];
$loader = static function () use ($routeDefinitionCallback, $options): array {
$routeCollector = new $options['routeCollector'](
new $options['routeParser'](),
new $options['dataGenerator']()
);
$routeDefinitionCallback($routeCollector);
return $routeCollector->processedRoutes();
};
if ($options['cacheDisabled'] === true) {
return new $options['dispatcher']($loader());
}
$cacheKey = $options['cacheKey'] ?? $options['cacheFile'] ?? null;
if ($cacheKey === null) {
throw new LogicException('Must specify "cacheKey" option');
}
$cache = $options['cacheDriver'];
if (is_string($cache)) {
$cache = new $cache();
}
return new $options['dispatcher']($cache->get($cacheKey, $loader));
}
}