|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* WebHemi |
|
4
|
|
|
* |
|
5
|
|
|
* PHP version 7.1 |
|
6
|
|
|
* |
|
7
|
|
|
* @copyright 2012 - 2017 Gixx-web (http://www.gixx-web.com) |
|
8
|
|
|
* @license https://opensource.org/licenses/MIT The MIT License (MIT) |
|
9
|
|
|
* @link http://www.gixx-web.com |
|
10
|
|
|
*/ |
|
11
|
|
|
declare(strict_types = 1); |
|
12
|
|
|
|
|
13
|
|
|
namespace WebHemi\Router\ServiceAdapter\Base; |
|
14
|
|
|
|
|
15
|
|
|
use WebHemi\Configuration\ServiceInterface as ConfigurationInterface; |
|
16
|
|
|
use WebHemi\Environment\ServiceInterface as EnvironmentInterface; |
|
17
|
|
|
use WebHemi\Http\ServerRequestInterface; |
|
18
|
|
|
use WebHemi\Router\ProxyInterface; |
|
19
|
|
|
use WebHemi\Router\Result\Result; |
|
20
|
|
|
use WebHemi\Router\ServiceInterface; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Class ServiceAdapter. |
|
24
|
|
|
*/ |
|
25
|
|
|
class ServiceAdapter implements ServiceInterface |
|
26
|
|
|
{ |
|
27
|
|
|
/** @var Result */ |
|
28
|
|
|
private $result; |
|
29
|
|
|
/** @var array */ |
|
30
|
|
|
private $routes; |
|
31
|
|
|
/** @var string */ |
|
32
|
|
|
private $module; |
|
33
|
|
|
/** @var string */ |
|
34
|
|
|
private $application; |
|
35
|
|
|
/** @var string */ |
|
36
|
|
|
private $applicationPath; |
|
37
|
|
|
/** @var ProxyInterface */ |
|
38
|
|
|
private $proxy; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* ServiceAdapter constructor. |
|
42
|
|
|
* |
|
43
|
|
|
* @param ConfigurationInterface $configuration |
|
44
|
|
|
* @param EnvironmentInterface $environmentManager |
|
45
|
|
|
* @param Result $routeResult |
|
46
|
|
|
* @param null|ProxyInterface $routerProxy |
|
47
|
|
|
*/ |
|
48
|
9 |
|
public function __construct( |
|
49
|
|
|
ConfigurationInterface $configuration, |
|
50
|
|
|
EnvironmentInterface $environmentManager, |
|
51
|
|
|
Result $routeResult, |
|
52
|
|
|
? ProxyInterface $routerProxy = null |
|
53
|
|
|
) { |
|
54
|
9 |
|
$module = $environmentManager->getSelectedModule(); |
|
55
|
9 |
|
$this->routes = $configuration->getData('router/'.$module); |
|
56
|
9 |
|
$this->proxy = $routerProxy; |
|
57
|
9 |
|
$this->result = $routeResult; |
|
58
|
9 |
|
$this->module = $environmentManager->getSelectedModule(); |
|
59
|
9 |
|
$this->application = $environmentManager->getSelectedApplication(); |
|
60
|
9 |
|
$this->applicationPath = $environmentManager->getSelectedApplicationUri(); |
|
61
|
9 |
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Processes the Request and give a Result. |
|
65
|
|
|
* |
|
66
|
|
|
* @param ServerRequestInterface $request |
|
67
|
|
|
* @return Result |
|
68
|
|
|
*/ |
|
69
|
7 |
|
public function match(ServerRequestInterface $request) : Result |
|
70
|
|
|
{ |
|
71
|
7 |
|
$httpMethod = $request->getMethod(); |
|
72
|
7 |
|
$uri = $this->getApplicationRouteUri($request); |
|
73
|
|
|
|
|
74
|
7 |
|
$result = clone $this->result; |
|
75
|
7 |
|
$routeDefinition = $this->findRouteDefinition($uri); |
|
76
|
|
|
|
|
77
|
7 |
|
if (empty($routeDefinition)) { |
|
78
|
3 |
|
return $result->setStatus(Result::CODE_NOT_FOUND); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
6 |
|
if (!in_array($httpMethod, $routeDefinition['allowed_methods'])) { |
|
82
|
3 |
|
return $result->setStatus(Result::CODE_BAD_METHOD); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
5 |
|
$result->setParameters($routeDefinition['parameters']) |
|
86
|
5 |
|
->setResource($routeDefinition['resource']) |
|
87
|
5 |
|
->setMatchedMiddleware($routeDefinition['middleware']); |
|
88
|
|
|
|
|
89
|
|
|
// Check if we marked the middleware to be resolved by the proxy |
|
90
|
5 |
|
if ($routeDefinition['middleware'] === 'proxy' && $this->proxy instanceof ProxyInterface) { |
|
91
|
|
|
$this->proxy->resolveMiddleware($this->application, $result); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
5 |
|
if (empty($result->getMatchedMiddleware())) { |
|
95
|
|
|
return $result->setStatus(Result::CODE_NOT_FOUND); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
5 |
|
return $result->setStatus(Result::CODE_FOUND); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* According to the application path, determines the route uri |
|
103
|
|
|
* |
|
104
|
|
|
* @param ServerRequestInterface $request |
|
105
|
|
|
* @return string |
|
106
|
|
|
*/ |
|
107
|
8 |
|
private function getApplicationRouteUri(ServerRequestInterface $request) : string |
|
108
|
|
|
{ |
|
109
|
8 |
|
$uri = $request->getUri()->getPath(); |
|
110
|
|
|
|
|
111
|
8 |
|
if ($this->applicationPath != '/' && strpos($uri, $this->applicationPath) === 0) { |
|
112
|
2 |
|
$uri = substr($uri, strlen($this->applicationPath)); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
8 |
|
return empty($uri) ? '/' : $uri; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Searches definition and returns data is found. First find, first served. |
|
120
|
|
|
* |
|
121
|
|
|
* @param string $uri |
|
122
|
|
|
* @return array|null |
|
123
|
|
|
*/ |
|
124
|
7 |
|
private function findRouteDefinition(string $uri) : ? array |
|
125
|
|
|
{ |
|
126
|
7 |
|
$routeDefinition = []; |
|
127
|
7 |
|
$matches = []; |
|
128
|
|
|
|
|
129
|
7 |
|
foreach ($this->routes as $resource => $routeData) { |
|
130
|
7 |
|
$pattern = '#'.$routeData['path'].'#'; |
|
131
|
|
|
|
|
132
|
7 |
|
if (preg_match_all($pattern, $uri, $matches, PREG_SET_ORDER, 0)) { |
|
133
|
6 |
|
$parameters = $this->getMatchParameters($matches); |
|
134
|
|
|
|
|
135
|
|
|
$routeDefinition = [ |
|
136
|
6 |
|
'uri' => $uri, |
|
137
|
6 |
|
'middleware' => $routeData['middleware'], |
|
138
|
6 |
|
'parameters' => $parameters, |
|
139
|
6 |
|
'resource' => $resource, |
|
140
|
6 |
|
'allowed_methods' => $routeData['allowed_methods'] |
|
141
|
|
|
]; |
|
142
|
|
|
|
|
143
|
6 |
|
break; |
|
144
|
|
|
} |
|
145
|
|
|
} |
|
146
|
7 |
|
return $routeDefinition; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* Gets the paramters from the route pattern match result. |
|
151
|
|
|
* |
|
152
|
|
|
* @param array $matches |
|
153
|
|
|
* @return array |
|
154
|
|
|
*/ |
|
155
|
6 |
|
private function getMatchParameters(array $matches) : array |
|
156
|
|
|
{ |
|
157
|
6 |
|
$parameters = []; |
|
158
|
|
|
|
|
159
|
6 |
|
foreach ($matches[0] as $index => $value) { |
|
160
|
6 |
|
if (!is_numeric($index)) { |
|
161
|
|
|
$parameters[$index] = $value; |
|
162
|
|
|
} |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
6 |
|
if ($this->module == 'Website') { |
|
166
|
5 |
|
$parameters['path'] = !empty($matches[0]['path']) ? $matches[0]['path'] : '/'; |
|
167
|
5 |
|
$parameters['basename'] = !empty($matches[0]['basename']) ? $matches[0]['basename'] : 'index.html'; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
6 |
|
return $parameters; |
|
171
|
|
|
} |
|
172
|
|
|
} |
|
173
|
|
|
|