1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* apparat-server |
5
|
|
|
* |
6
|
|
|
* @category Apparat |
7
|
|
|
* @package Apparat\Server |
8
|
|
|
* @subpackage Apparat\Server\Infrastructure |
9
|
|
|
* @author Joschi Kuphal <[email protected]> / @jkphl |
10
|
|
|
* @copyright Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl |
11
|
|
|
* @license http://opensource.org/licenses/MIT The MIT License (MIT) |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
/*********************************************************************************** |
15
|
|
|
* The MIT License (MIT) |
16
|
|
|
* |
17
|
|
|
* Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl |
18
|
|
|
* |
19
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy of |
20
|
|
|
* this software and associated documentation files (the "Software"), to deal in |
21
|
|
|
* the Software without restriction, including without limitation the rights to |
22
|
|
|
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of |
23
|
|
|
* the Software, and to permit persons to whom the Software is furnished to do so, |
24
|
|
|
* subject to the following conditions: |
25
|
|
|
* |
26
|
|
|
* The above copyright notice and this permission notice shall be included in all |
27
|
|
|
* copies or substantial portions of the Software. |
28
|
|
|
* |
29
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
30
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
31
|
|
|
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
32
|
|
|
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
33
|
|
|
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
34
|
|
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
35
|
|
|
***********************************************************************************/ |
36
|
|
|
|
37
|
|
|
namespace Apparat\Server\Infrastructure; |
38
|
|
|
|
39
|
|
|
use Apparat\Kernel\Ports\Kernel; |
40
|
|
|
use Apparat\Server\Domain\Contract\RouteInterface; |
41
|
|
|
use Apparat\Server\Domain\Contract\RouterContainerInterface; |
42
|
|
|
use Aura\Router\Matcher; |
43
|
|
|
use Aura\Router\Route; |
44
|
|
|
use Aura\Router\RouterContainer; |
45
|
|
|
use Psr\Http\Message\ResponseInterface; |
46
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Aura.Router adapter |
50
|
|
|
* |
51
|
|
|
* @package Apparat\Server |
52
|
|
|
* @subpackage Apparat\Server\Infrastructure |
53
|
|
|
*/ |
54
|
|
|
class AuraRouterAdapter implements RouterContainerInterface |
55
|
|
|
{ |
56
|
|
|
/** |
57
|
|
|
* Aura.Router container |
58
|
|
|
* |
59
|
|
|
* @var RouterContainer |
60
|
|
|
*/ |
61
|
|
|
protected $routerContainer; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Constructor |
65
|
|
|
* |
66
|
|
|
* @param RouterContainer $routerContainer Router container |
67
|
|
|
*/ |
68
|
1 |
|
public function __construct(RouterContainer $routerContainer) |
69
|
|
|
{ |
70
|
1 |
|
$this->routerContainer = $routerContainer; |
71
|
1 |
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Register a route |
75
|
|
|
* |
76
|
|
|
* @param RouteInterface $route Route |
77
|
|
|
* @return RouterContainerInterface Self reference |
78
|
|
|
*/ |
79
|
1 |
|
public function registerRoute(RouteInterface $route) |
80
|
|
|
{ |
81
|
1 |
|
$this->routerContainer->getMap() |
82
|
1 |
|
->route($route->getName(), $route->getPath(), $route->getAction()) |
83
|
1 |
|
->allows($route->getVerbs()) |
84
|
1 |
|
->tokens($route->getTokens()) |
85
|
1 |
|
->defaults($route->getDefaults()) |
86
|
1 |
|
->wildcard($route->getWildcard()) |
87
|
1 |
|
->host($route->getHost()) |
88
|
1 |
|
->accepts($route->getAccepts()) |
89
|
1 |
|
->auth($route->getAuth()) |
90
|
1 |
|
->secure($route->getSecure()) |
91
|
1 |
|
->extras($route->getExtras()); |
92
|
1 |
|
return $this; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Dispatch a request |
97
|
|
|
* |
98
|
|
|
* @param ServerRequestInterface $request |
99
|
|
|
* @return ResponseInterface $response |
100
|
|
|
*/ |
101
|
1 |
|
public function dispatchRequest(ServerRequestInterface $request) |
102
|
|
|
{ |
103
|
1 |
|
$matcher = $this->routerContainer->getMatcher(); |
104
|
1 |
|
$route = $matcher->match($request); |
105
|
|
|
|
106
|
|
|
// If a registered Route could be matched |
107
|
1 |
|
if ($route instanceof Route) { |
108
|
1 |
|
return $this->handleRequestRoute($request, $route); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
// Handle the request mismatch |
112
|
|
|
return $this->handleRequestMismatch($request, $matcher); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Handle a matched route request |
117
|
|
|
* |
118
|
|
|
* @param ServerRequestInterface $request Server request |
119
|
|
|
* @param Route $route Matched route |
120
|
|
|
* @return ResponseInterface $response |
121
|
|
|
*/ |
122
|
1 |
|
protected function handleRequestRoute(ServerRequestInterface $request, Route $route) { |
123
|
|
|
// Copy all route attributes to the server request |
124
|
1 |
|
foreach ($route->attributes as $key => $val) { |
125
|
1 |
|
$request = $request->withAttribute($key, $val); |
126
|
1 |
|
} |
127
|
|
|
|
128
|
|
|
// Instantiate and call the route handler |
129
|
1 |
|
$handler = is_callable($route->handler) ? $route->handler : Kernel::create($route->handler); |
130
|
1 |
|
return $handler($request); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Handle a mismatched request |
135
|
|
|
* |
136
|
|
|
* @param ServerRequestInterface $request Server request |
137
|
|
|
* @param Matcher $matcher Matcher |
138
|
|
|
* @return ResponseInterface Response |
139
|
|
|
*/ |
140
|
|
|
protected function handleRequestMismatch(ServerRequestInterface $request, Matcher $matcher) { |
|
|
|
|
141
|
|
|
// TODO Error responder |
142
|
|
|
// // Instantiate a response |
143
|
|
|
// $response = Kernel::create(ResponseInterface::class); |
144
|
|
|
// |
145
|
|
|
// // Get the first of the best-available non-matched routes |
146
|
|
|
// $failedRoute = $matcher->getFailedRoute(); |
147
|
|
|
// |
148
|
|
|
// // Which matching rule failed? |
149
|
|
|
// switch ($failedRoute->failedRule) { |
150
|
|
|
// case 'Aura\Router\Rule\Allows': |
151
|
|
|
// // 405 METHOD NOT ALLOWED |
152
|
|
|
// // Send the $failedRoute->allows as 'Allow:' |
153
|
|
|
// break; |
154
|
|
|
// case 'Aura\Router\Rule\Accepts': |
155
|
|
|
// // 406 NOT ACCEPTABLE |
156
|
|
|
// break; |
157
|
|
|
// default: |
158
|
|
|
// // 404 NOT FOUND |
159
|
|
|
// break; |
160
|
|
|
// } |
161
|
|
|
// |
162
|
|
|
// return $response; |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.