|
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 Symfony\Component\Routing\Exception\ResourceNotFoundException; |
|
22
|
|
|
use Symfony\Component\Routing\Loader\ClosureLoader; |
|
23
|
|
|
use Symfony\Component\Routing\Route; |
|
24
|
|
|
use Symfony\Component\Routing\RouteCollection; |
|
25
|
|
|
use Symfony\Component\Routing\Router; |
|
26
|
|
|
|
|
27
|
|
|
class SymfonyRouter extends AbstractRouter |
|
28
|
|
|
{ |
|
29
|
|
|
protected Router $router; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* {@inheritdoc} |
|
33
|
|
|
*/ |
|
34
|
|
|
public static function isCacheable(): bool |
|
35
|
|
|
{ |
|
36
|
|
|
return true; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* {@inheritdoc} |
|
41
|
|
|
*/ |
|
42
|
|
|
public function testStatic(): bool |
|
43
|
|
|
{ |
|
44
|
|
|
$methods = $this->generator->getMethods(); |
|
45
|
|
|
|
|
46
|
|
|
foreach ($methods as $method) { |
|
47
|
|
|
$path = ($this->strategy)($method); |
|
48
|
|
|
$this->router->getContext()->setMethod($method); |
|
49
|
|
|
|
|
50
|
|
|
try { |
|
51
|
|
|
$this->router->match($path); |
|
52
|
|
|
} catch (ResourceNotFoundException $e) { |
|
53
|
|
|
return false; |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
return true; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* {@inheritdoc} |
|
62
|
|
|
*/ |
|
63
|
|
|
public function testPath(): bool |
|
64
|
|
|
{ |
|
65
|
|
|
$methods = $this->generator->getMethods(); |
|
66
|
|
|
|
|
67
|
|
|
foreach ($methods as $method) { |
|
68
|
|
|
$path = ($this->strategy)($method); |
|
69
|
|
|
|
|
70
|
|
|
$this->router->getContext()->setMethod($method); |
|
71
|
|
|
|
|
72
|
|
|
try { |
|
73
|
|
|
$this->router->match($path . 'symfony'); |
|
74
|
|
|
} catch (ResourceNotFoundException $e) { |
|
75
|
|
|
return false; |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
return true; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Test Sub Domain with route path |
|
84
|
|
|
* |
|
85
|
|
|
* @return bool |
|
86
|
|
|
*/ |
|
87
|
|
|
public function testSubDomain(): bool |
|
88
|
|
|
{ |
|
89
|
|
|
$hosts = $this->generator->getHosts(); |
|
90
|
|
|
$router = clone $this->router; |
|
91
|
|
|
|
|
92
|
|
|
foreach ($hosts as $host) { |
|
93
|
|
|
$methods = $this->generator->getMethods(); |
|
94
|
|
|
|
|
95
|
|
|
foreach ($methods as $method) { |
|
96
|
|
|
$path = ($this->strategy)($method, $host); |
|
97
|
|
|
|
|
98
|
|
|
if ($host !== '*') { |
|
99
|
|
|
$router->getContext()->setHost($host . 'symfony'); |
|
100
|
|
|
} |
|
101
|
|
|
$router->getContext()->setMethod($method); |
|
102
|
|
|
|
|
103
|
|
|
try { |
|
104
|
|
|
$router->match($path . 'symfony'); |
|
105
|
|
|
} catch (ResourceNotFoundException $e) { |
|
106
|
|
|
return false; |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
return true; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* {@inheritdoc} |
|
116
|
|
|
*/ |
|
117
|
|
|
public function buildRoutes(array $routes): void |
|
118
|
|
|
{ |
|
119
|
|
|
$resource = static function () use ($routes): RouteCollection { |
|
120
|
|
|
$sfCollection = new RouteCollection(); |
|
121
|
|
|
|
|
122
|
|
|
foreach ($routes as $route) { |
|
123
|
|
|
$sfRoute = new Route($route['pattern']); |
|
124
|
|
|
|
|
125
|
|
|
if ($route['host'] !== '*') { |
|
126
|
|
|
$sfRoute->setHost($route['host']); |
|
127
|
|
|
} |
|
128
|
|
|
$sfRoute->setMethods($route['methods']); |
|
129
|
|
|
$sfRoute->setRequirements($route['constraints']); |
|
130
|
|
|
|
|
131
|
|
|
$sfCollection->add($route['pattern'], $sfRoute); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
return $sfCollection; |
|
135
|
|
|
}; |
|
136
|
|
|
|
|
137
|
|
|
$router = new Router(new ClosureLoader(), $resource); |
|
138
|
|
|
|
|
139
|
|
|
if (null !== $cacheDir = $this->getCache('symfony')) { |
|
140
|
|
|
$router->setOption('cache_dir', $cacheDir); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
$this->router = $router; |
|
144
|
|
|
} |
|
145
|
|
|
} |
|
146
|
|
|
|