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; |
19
|
|
|
|
20
|
|
|
use Fig\Http\Message\RequestMethodInterface; |
21
|
|
|
use Lead\Text\Text; |
22
|
|
|
|
23
|
|
|
class RouteGenerator |
24
|
|
|
{ |
25
|
|
|
/** The number of routes. */ |
26
|
|
|
protected int $nbRoutes; |
27
|
|
|
|
28
|
|
|
/** The number of hosts. */ |
29
|
|
|
protected int $nbHosts; |
30
|
|
|
|
31
|
|
|
/** Where to use all methods or not. */ |
32
|
|
|
protected bool $isolated; |
33
|
|
|
|
34
|
|
|
/** The scheme constraint. */ |
35
|
|
|
protected ?string $scheme = null; |
36
|
|
|
|
37
|
|
|
/** The host structure template. */ |
38
|
|
|
protected ?string $host = null; |
39
|
|
|
|
40
|
|
|
/** @var string[] The built hosts. */ |
41
|
|
|
protected array $hosts = []; |
42
|
|
|
|
43
|
|
|
/** The path token structure template. */ |
44
|
|
|
protected ?string $template = null; |
45
|
|
|
|
46
|
|
|
/** @var array<string,string> The constraints. */ |
47
|
|
|
protected array $constraints = []; |
48
|
|
|
|
49
|
|
|
/** @var string[] Archaic HTTP method distribution. */ |
50
|
|
|
protected array $methods = [ |
51
|
|
|
RequestMethodInterface::METHOD_GET, |
52
|
|
|
RequestMethodInterface::METHOD_POST, |
53
|
|
|
RequestMethodInterface::METHOD_PUT, |
54
|
|
|
RequestMethodInterface::METHOD_PATCH, |
55
|
|
|
RequestMethodInterface::METHOD_DELETE, |
56
|
|
|
]; |
57
|
|
|
|
58
|
|
|
public function __construct(bool $isolated, int $routes = 1, int $hosts = 1) |
59
|
|
|
{ |
60
|
|
|
$this->isolated = $isolated; |
61
|
|
|
$this->nbRoutes = $routes; |
62
|
|
|
$this->nbHosts = $hosts; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Get all methods |
67
|
|
|
* |
68
|
|
|
* @return string[] |
69
|
|
|
*/ |
70
|
|
|
public function getMethods(): array |
71
|
|
|
{ |
72
|
|
|
return $this->methods; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Get all generated hosts |
77
|
|
|
* |
78
|
|
|
* @return array |
79
|
|
|
*/ |
80
|
|
|
public function getHosts(): array |
81
|
|
|
{ |
82
|
|
|
return $this->hosts; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function setHost(string $host, string $scheme = 'http'): void |
86
|
|
|
{ |
87
|
|
|
$this->scheme = $scheme; |
88
|
|
|
$this->host = $host; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function setTemplate(string $template, array $constraints = []): void |
92
|
|
|
{ |
93
|
|
|
$this->template = $template; |
94
|
|
|
$this->constraints = $constraints; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Generates a bunch of routes. |
99
|
|
|
* |
100
|
|
|
* @param bool $isStatic |
101
|
|
|
* |
102
|
|
|
* @return mixed the generated routes array |
103
|
|
|
*/ |
104
|
|
|
public function generate(bool $isStatic = false): array |
105
|
|
|
{ |
106
|
|
|
$scheme = $this->scheme; |
107
|
|
|
$hosts = ['*' => '*']; |
108
|
|
|
$constraints = $this->constraints; |
109
|
|
|
|
110
|
|
|
if (!$isStatic && !$this->template) { |
111
|
|
|
throw new \RuntimeException('Missing path template.'); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
if (null !== $this->host) { |
115
|
|
|
$pattern = $this->host; |
116
|
|
|
|
117
|
|
|
for ($i = 1; $i <= $this->nbHosts; $i++) { |
118
|
|
|
$host = Text::insert($pattern, $constraints, ['before' => '{%', 'after' => '%}']); |
119
|
|
|
|
120
|
|
|
$hosts["subdomain{$i}.domain."] = \sprintf('subdomain%s.domain.%s', $i, $host); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
$ids = []; |
125
|
|
|
$id = 1; |
126
|
|
|
|
127
|
|
|
foreach ($hosts as $domain => $host) { |
128
|
|
|
$this->hosts[] = $domain; |
129
|
|
|
$ids[$domain] = [ |
130
|
|
|
RequestMethodInterface::METHOD_GET => [], |
131
|
|
|
RequestMethodInterface::METHOD_POST => [], |
132
|
|
|
RequestMethodInterface::METHOD_PUT => [], |
133
|
|
|
RequestMethodInterface::METHOD_PATCH => [], |
134
|
|
|
RequestMethodInterface::METHOD_DELETE => [], |
135
|
|
|
]; |
136
|
|
|
|
137
|
|
|
for ($i = 0; $i < $this->nbRoutes; $i++) { |
138
|
|
|
$path = \sprintf('/controller%s/action%1$s/', $id); |
139
|
|
|
$pattern = $path . Text::insert($this->template, $constraints, ['before' => '{%', 'after' => '%}']); |
140
|
|
|
$methods = $this->isolated ? [$this->methods[$i % 5]] : $this->methods; |
141
|
|
|
$name = '_' . $id; |
142
|
|
|
|
143
|
|
|
if ($isStatic) { |
144
|
|
|
$pattern = $this->template ?? $path; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
$routes[] = \compact('name', 'scheme', 'host', 'pattern', 'methods', 'constraints'); |
148
|
|
|
|
149
|
|
|
foreach ($methods as $method) { |
150
|
|
|
$ids[$domain][$method][] = $path; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
$id++; |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
return [$ids, $routes]; |
|
|
|
|
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|