1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of DivineNii opensource projects. |
7
|
|
|
* |
8
|
|
|
* PHP version 7.4 and above required |
9
|
|
|
* |
10
|
|
|
* @author Divine Niiquaye Ibok <[email protected]> |
11
|
|
|
* @copyright 2019 DivineNii (https://divinenii.com/) |
12
|
|
|
* @license https://opensource.org/licenses/BSD-3-Clause License |
13
|
|
|
* |
14
|
|
|
* For the full copyright and license information, please view the LICENSE |
15
|
|
|
* file that was distributed with this source code. |
16
|
|
|
*/ |
17
|
|
|
|
18
|
|
|
namespace App\BenchMark\Routers; |
19
|
|
|
|
20
|
|
|
use App\BenchMark\AbstractRouter; |
21
|
|
|
use FastRoute\Dispatcher; |
22
|
|
|
use FastRoute\RouteCollector; |
23
|
|
|
|
24
|
|
|
class FastRoute extends AbstractRouter |
25
|
|
|
{ |
26
|
|
|
protected Dispatcher $router; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* {@inheritdoc} |
30
|
|
|
*/ |
31
|
|
|
public static function isCacheable(): bool |
32
|
|
|
{ |
33
|
|
|
return true; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* {@inheritdoc} |
38
|
|
|
*/ |
39
|
|
|
public function testStatic(): bool |
40
|
|
|
{ |
41
|
|
|
$methods = $this->generator->getMethods(); |
42
|
|
|
|
43
|
|
|
foreach ($methods as $method) { |
44
|
|
|
$path = ($this->strategy)($method); |
45
|
|
|
|
46
|
|
|
$result = $this->router->dispatch($method, $path); |
47
|
|
|
|
48
|
|
|
if ($result[0] !== $this->router::FOUND) { |
49
|
|
|
return false; |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
return true; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* {@inheritdoc} |
58
|
|
|
*/ |
59
|
|
|
public function testPath(): bool |
60
|
|
|
{ |
61
|
|
|
$methods = $this->generator->getMethods(); |
62
|
|
|
|
63
|
|
|
foreach ($methods as $method) { |
64
|
|
|
$path = ($this->strategy)($method); |
65
|
|
|
|
66
|
|
|
$result = $this->router->dispatch($method, $path . 'fastroute'); |
67
|
|
|
|
68
|
|
|
if ($result[0] !== $this->router::FOUND) { |
69
|
|
|
return false; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return true; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* {@inheritdoc} |
78
|
|
|
*/ |
79
|
|
|
public function buildRoutes(array $routes): void |
80
|
|
|
{ |
81
|
|
|
$routes = function (RouteCollector $router) use ($routes): void { |
82
|
|
|
foreach ($routes as $route) { |
83
|
|
|
foreach ($route['methods'] as $method) { |
84
|
|
|
$router->addRoute($method, $route['pattern'], 'phpinfo'); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
}; |
88
|
|
|
|
89
|
|
|
if (null !== $cacheDir = $this->getCache('fastroute')) { |
90
|
|
|
if (!file_exists($cacheDir)) { |
91
|
|
|
mkdir($cacheDir, 0777, true); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
$router = \FastRoute\cachedDispatcher($routes, ['cacheFile' => $cacheDir . '/compiled.php']); |
95
|
|
|
} else { |
96
|
|
|
$router = \FastRoute\simpleDispatcher($routes); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$this->router = $router; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|