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 Psr\Http\Message\ResponseInterface; |
22
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
23
|
|
|
use Sunrise\Http\Message\ResponseFactory; |
24
|
|
|
use Sunrise\Http\Router\Exception\RouteNotFoundException; |
25
|
|
|
use Sunrise\Http\Router\RequestHandler\CallableRequestHandler; |
26
|
|
|
use Sunrise\Http\Router\Route; |
27
|
|
|
use Sunrise\Http\Router\Router; |
28
|
|
|
use Sunrise\Http\ServerRequest\ServerRequestFactory; |
29
|
|
|
use Sunrise\Uri\Uri; |
30
|
|
|
|
31
|
|
|
class SunriseRouter extends AbstractRouter |
32
|
|
|
{ |
33
|
|
|
public const HOST = ''; |
34
|
|
|
|
35
|
|
|
protected Router $router; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* {@inheritdoc} |
39
|
|
|
*/ |
40
|
|
|
public function testStatic(): bool |
41
|
|
|
{ |
42
|
|
|
$methods = $this->generator->getMethods(); |
43
|
|
|
|
44
|
|
|
foreach ($methods as $method) { |
45
|
|
|
$path = ($this->strategy)($method); |
46
|
|
|
|
47
|
|
|
try { |
48
|
|
|
$this->router->handle((new ServerRequestFactory())->createServerRequest($method, $path)); |
49
|
|
|
} catch (RouteNotFoundException $e) { |
50
|
|
|
return false; |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return true; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* {@inheritdoc} |
59
|
|
|
*/ |
60
|
|
|
public function testPath(): bool |
61
|
|
|
{ |
62
|
|
|
$methods = $this->generator->getMethods(); |
63
|
|
|
|
64
|
|
|
foreach ($methods as $method) { |
65
|
|
|
$path = ($this->strategy)($method); |
66
|
|
|
|
67
|
|
|
try { |
68
|
|
|
$this->router->handle((new ServerRequestFactory())->createServerRequest( |
69
|
|
|
$method, |
70
|
|
|
$path . 'sunrise_router' |
71
|
|
|
)); |
72
|
|
|
} catch (RouteNotFoundException $e) { |
73
|
|
|
return false; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return true; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Test Sub Domain with route path |
82
|
|
|
* |
83
|
|
|
* @return bool |
84
|
|
|
*/ |
85
|
|
|
public function testSubDomain(): bool |
86
|
|
|
{ |
87
|
|
|
$hosts = $this->generator->getHosts(); |
88
|
|
|
|
89
|
|
|
foreach ($hosts as $host) { |
90
|
|
|
$methods = $this->generator->getMethods(); |
91
|
|
|
|
92
|
|
|
foreach ($methods as $method) { |
93
|
|
|
$path = ($this->strategy)($method, $host); |
94
|
|
|
$uri = new Uri($path . 'sunrise_router'); |
95
|
|
|
|
96
|
|
|
if ($host !== '*') { |
97
|
|
|
$uri = $uri->withHost($host); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
try { |
101
|
|
|
$this->router->match((new ServerRequestFactory())->createServerRequest($method, $uri)); |
102
|
|
|
} catch (RouteNotFoundException $e) { |
103
|
|
|
return false; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return true; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* {@inheritdoc} |
113
|
|
|
*/ |
114
|
|
|
public function buildRoutes(array $routes): void |
115
|
|
|
{ |
116
|
|
|
$router = new Router(); |
117
|
|
|
|
118
|
|
|
foreach ($routes as $route) { |
119
|
|
|
$srRoute = new Route( |
120
|
|
|
$route['name'], |
121
|
|
|
$route['pattern'], |
122
|
|
|
$route['methods'], |
123
|
|
|
new CallableRequestHandler( |
124
|
|
|
function (ServerRequestInterface $request): ResponseInterface { |
125
|
|
|
return (new ResponseFactory())->createJsonResponse(200, [ |
126
|
|
|
'status' => 'ok', |
127
|
|
|
'method' => $request->getMethod(), |
128
|
|
|
]); |
129
|
|
|
} |
130
|
|
|
) |
131
|
|
|
); |
132
|
|
|
|
133
|
|
|
if ('*' !== $route['host']) { |
134
|
|
|
$srRoute->setHost($route['host']); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
$router->addRoute($srRoute); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
$this->router = $router; |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|