|
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 Flight\Routing\Exceptions\RouteNotFoundException; |
|
22
|
|
|
use Flight\Routing\Interfaces\RouteMatcherInterface; |
|
23
|
|
|
use Flight\Routing\Matchers\SimpleRouteMatcher; |
|
24
|
|
|
use Flight\Routing\RouteCollection; |
|
25
|
|
|
use Laminas\Diactoros\ServerRequest; |
|
26
|
|
|
use Laminas\Diactoros\Uri; |
|
27
|
|
|
|
|
28
|
|
|
class FlightRouting extends AbstractRouter |
|
29
|
|
|
{ |
|
30
|
|
|
protected RouteMatcherInterface $router; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* {@inheritdoc} |
|
34
|
|
|
*/ |
|
35
|
|
|
public function testStatic(): bool |
|
36
|
|
|
{ |
|
37
|
|
|
$methods = $this->generator->getMethods(); |
|
38
|
|
|
|
|
39
|
|
|
foreach ($methods as $method) { |
|
40
|
|
|
$path = ($this->strategy)($method); |
|
41
|
|
|
|
|
42
|
|
|
try { |
|
43
|
|
|
$this->router->match(new ServerRequest([], [], $path, $method)); |
|
44
|
|
|
} catch (RouteNotFoundException $e) { |
|
45
|
|
|
return false; |
|
46
|
|
|
} |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
return true; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* {@inheritdoc} |
|
54
|
|
|
*/ |
|
55
|
|
|
public function testPath(): bool |
|
56
|
|
|
{ |
|
57
|
|
|
$methods = $this->generator->getMethods(); |
|
58
|
|
|
|
|
59
|
|
|
foreach ($methods as $method) { |
|
60
|
|
|
$path = ($this->strategy)($method); |
|
61
|
|
|
|
|
62
|
|
|
try { |
|
63
|
|
|
$this->router->match(new ServerRequest([], [], $path . 'flight_routing', $method)); |
|
64
|
|
|
} catch (RouteNotFoundException $e) { |
|
65
|
|
|
return false; |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
return true; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Test Sub Domain with route path |
|
74
|
|
|
* |
|
75
|
|
|
* @return bool |
|
76
|
|
|
*/ |
|
77
|
|
|
public function testSubDomain(): bool |
|
78
|
|
|
{ |
|
79
|
|
|
$hosts = $this->generator->getHosts(); |
|
80
|
|
|
|
|
81
|
|
|
foreach ($hosts as $host) { |
|
82
|
|
|
$methods = $this->generator->getMethods(); |
|
83
|
|
|
|
|
84
|
|
|
foreach ($methods as $method) { |
|
85
|
|
|
$path = ($this->strategy)($method, $host); |
|
86
|
|
|
$uri = new Uri($path . 'flight_routing'); |
|
87
|
|
|
|
|
88
|
|
|
if ($host !== '*') { |
|
89
|
|
|
$uri = $uri->withHost($host . 'flight_routing'); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
try { |
|
93
|
|
|
$this->router->match(new ServerRequest([], [], $uri, $method)); |
|
94
|
|
|
} catch (RouteNotFoundException $e) { |
|
95
|
|
|
return false; |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
return true; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* {@inheritdoc} |
|
105
|
|
|
*/ |
|
106
|
|
|
public function buildRoutes(array $routes): void |
|
107
|
|
|
{ |
|
108
|
|
|
$frCollection = new RouteCollection(false); |
|
109
|
|
|
|
|
110
|
|
|
foreach ($routes as $route) { |
|
111
|
|
|
$frRoute = $frCollection->addRoute($route['pattern'], $route['methods']) |
|
112
|
|
|
->bind($route['name'])->asserts($route['constraints']); |
|
113
|
|
|
|
|
114
|
|
|
if ('*' !== $route['host']) { |
|
115
|
|
|
$frRoute->domain($route['host']); |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
$this->router = new SimpleRouteMatcher($frCollection); |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|