1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* apparat-server |
5
|
|
|
* |
6
|
|
|
* @category Apparat |
7
|
|
|
* @package Apparat\Server |
8
|
|
|
* @subpackage Apparat\Server\Ports |
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\Ports; |
38
|
|
|
|
39
|
|
|
use Apparat\Kernel\Ports\Kernel; |
40
|
|
|
use Apparat\Object\Ports\Object; |
41
|
|
|
use Apparat\Server\Domain\Contract\RouteInterface; |
42
|
|
|
use Apparat\Server\Infrastructure\Action\DayAction; |
43
|
|
|
use Apparat\Server\Infrastructure\Action\HourAction; |
44
|
|
|
use Apparat\Server\Infrastructure\Action\MinuteAction; |
45
|
|
|
use Apparat\Server\Infrastructure\Action\MonthAction; |
46
|
|
|
use Apparat\Server\Infrastructure\Action\ObjectAction; |
47
|
|
|
use Apparat\Server\Infrastructure\Action\SecondAction; |
48
|
|
|
use Apparat\Server\Infrastructure\Action\TypeAction; |
49
|
|
|
use Apparat\Server\Infrastructure\Action\YearAction; |
50
|
|
|
use Psr\Http\Message\ResponseInterface; |
51
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Server facade |
55
|
|
|
* |
56
|
|
|
* @package Apparat\Server\Ports |
57
|
|
|
*/ |
58
|
|
|
class Server |
59
|
|
|
{ |
60
|
|
|
/** |
61
|
|
|
* Server instance |
62
|
|
|
* |
63
|
|
|
* @var \Apparat\Server\Domain\Model\Server |
64
|
|
|
*/ |
65
|
|
|
protected static $server = null; |
66
|
|
|
/** |
67
|
|
|
* Year route token |
68
|
|
|
* |
69
|
|
|
* @var array |
70
|
|
|
*/ |
71
|
|
|
protected static $TOKEN_YEAR = ['year' => self::REGEX_ASTERISK.'|(?:\d{4})']; |
72
|
|
|
/** |
73
|
|
|
* Month route token |
74
|
|
|
* |
75
|
|
|
* @var array |
76
|
|
|
*/ |
77
|
|
|
protected static $TOKEN_MONTH = ['month' => self::REGEX_ASTERISK.'|(?:0[1-9])|(?:1[0-2])']; |
78
|
|
|
/** |
79
|
|
|
* Day route token |
80
|
|
|
* |
81
|
|
|
* @var array |
82
|
|
|
*/ |
83
|
|
|
protected static $TOKEN_DAY = ['day' => self::REGEX_ASTERISK.'|(?:0[1-9])|(?:[1-2]\d)|(?:3[0-1])']; |
84
|
|
|
/** |
85
|
|
|
* Hour route token |
86
|
|
|
* |
87
|
|
|
* @var array |
88
|
|
|
*/ |
89
|
|
|
protected static $TOKEN_HOUR = ['hour' => self::REGEX_ASTERISK.'|(?:0[1-9])|(?:1[0-2])']; |
90
|
|
|
/** |
91
|
|
|
* Day route token |
92
|
|
|
* |
93
|
|
|
* @var array |
94
|
|
|
*/ |
95
|
|
|
protected static $TOKEN_MINUTE = ['minute' => self::REGEX_ASTERISK.'|(?:0[1-9])|(?:[1-4]\d)|(?:5[0-9])']; |
96
|
|
|
/** |
97
|
|
|
* Second route token |
98
|
|
|
* |
99
|
|
|
* @var array |
100
|
|
|
*/ |
101
|
|
|
protected static $TOKEN_SECOND = ['second' => self::REGEX_ASTERISK.'|(?:0[1-9])|(?:[1-4]\d)|(?:5[0-9])']; |
102
|
|
|
/** |
103
|
|
|
* Asterisk regular expression |
104
|
|
|
* |
105
|
|
|
* @var string |
106
|
|
|
*/ |
107
|
|
|
const REGEX_ASTERISK = '(?:\%2A)'; |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Register a route |
111
|
|
|
* |
112
|
|
|
* @param RouteInterface $route |
113
|
|
|
*/ |
114
|
2 |
|
public static function registerRoute(RouteInterface $route) |
115
|
|
|
{ |
116
|
2 |
|
self::getServer()->registerRoute($route); |
117
|
2 |
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Register the default routes for a particular repository |
121
|
|
|
* |
122
|
|
|
* @param string $repositoryPath Repository path |
123
|
|
|
* @param bool $enable Enable / disable default routes |
124
|
|
|
*/ |
125
|
1 |
|
public static function registerRepositoryDefaultRoutes($repositoryPath = '', $enable = true) |
|
|
|
|
126
|
|
|
{ |
127
|
|
|
// Repository route prefix |
128
|
1 |
|
$prefix = rtrim('/'.$repositoryPath, '/'); |
129
|
|
|
|
130
|
|
|
// Build the list of default routes |
131
|
1 |
|
$defaultDateRoutes = self::buildDefaultDateRoutes($prefix, getenv('OBJECT_DATE_PRECISION')); |
132
|
1 |
|
$baseDateRoute = count($defaultDateRoutes) ? current($defaultDateRoutes) : ['/', []]; |
133
|
1 |
|
$defaultObjectRoutes = self::buildDefaultObjectRoutes($prefix, $baseDateRoute, count($defaultDateRoutes)); |
134
|
1 |
|
$defaultRoutes = $defaultObjectRoutes + $defaultDateRoutes; |
135
|
|
|
|
136
|
|
|
// Iterate through and register all base routes |
137
|
1 |
|
foreach ($defaultRoutes as $routeName => $routeConfig) { |
138
|
1 |
|
$route = new Route(Route::GET, $routeName, $routeConfig[0], $routeConfig[2], true); |
139
|
1 |
|
self::registerRoute($route->setTokens($routeConfig[1])); |
140
|
|
|
} |
141
|
1 |
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Dispatch a request |
145
|
|
|
* |
146
|
|
|
* @param ServerRequestInterface $request |
147
|
|
|
* @return ResponseInterface $response |
148
|
|
|
*/ |
149
|
2 |
|
public static function dispatchRequest(ServerRequestInterface $request) |
150
|
|
|
{ |
151
|
2 |
|
return self::getServer()->dispatchRequest($request); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Create and return the server instance |
156
|
|
|
* |
157
|
|
|
* @return \Apparat\Server\Domain\Model\Server Server instance |
158
|
|
|
*/ |
159
|
2 |
|
protected static function getServer() |
160
|
|
|
{ |
161
|
2 |
|
if (self::$server === null) { |
162
|
1 |
|
self::$server = Kernel::create(\Apparat\Server\Domain\Model\Server::class); |
163
|
|
|
} |
164
|
2 |
|
return self::$server; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Build and return the default date routes |
169
|
|
|
* |
170
|
|
|
* @param string $prefix Repository route prefix |
171
|
|
|
* @param int $precision Date precision |
172
|
|
|
* @return array Default date routes |
173
|
|
|
*/ |
174
|
1 |
|
protected static function buildDefaultDateRoutes($prefix, $precision) |
175
|
|
|
{ |
176
|
1 |
|
return array_slice( |
177
|
|
|
[ |
178
|
|
|
Route::SECOND => [ |
179
|
1 |
|
$prefix.'/{year}/{month}/{day}/{hour}/{minute}/{second}', |
180
|
1 |
|
self::$TOKEN_YEAR + |
181
|
1 |
|
self::$TOKEN_MONTH + |
182
|
1 |
|
self::$TOKEN_DAY + |
183
|
1 |
|
self::$TOKEN_HOUR + |
184
|
1 |
|
self::$TOKEN_MINUTE + |
185
|
1 |
|
self::$TOKEN_SECOND, |
186
|
|
|
SecondAction::class |
187
|
1 |
|
], |
188
|
|
|
Route::MINUTE => [ |
189
|
1 |
|
$prefix.'/{year}/{month}/{day}/{hour}/{minute}', |
190
|
1 |
|
self::$TOKEN_YEAR + |
191
|
1 |
|
self::$TOKEN_MONTH + |
192
|
1 |
|
self::$TOKEN_DAY + |
193
|
1 |
|
self::$TOKEN_HOUR + |
194
|
1 |
|
self::$TOKEN_MINUTE, |
195
|
|
|
MinuteAction::class |
196
|
|
|
], |
197
|
|
|
Route::HOUR => [ |
198
|
1 |
|
$prefix.'/{year}/{month}/{day}/{hour}', |
199
|
1 |
|
self::$TOKEN_YEAR + |
200
|
1 |
|
self::$TOKEN_MONTH + |
201
|
1 |
|
self::$TOKEN_DAY + |
202
|
1 |
|
self::$TOKEN_HOUR, |
203
|
|
|
HourAction::class |
204
|
|
|
], |
205
|
|
|
Route::DAY => [ |
206
|
1 |
|
$prefix.'/{year}/{month}/{day}', |
207
|
1 |
|
self::$TOKEN_YEAR + |
208
|
1 |
|
self::$TOKEN_MONTH + |
209
|
1 |
|
self::$TOKEN_DAY, |
210
|
|
|
DayAction::class |
211
|
|
|
], |
212
|
|
|
Route::MONTH => [ |
213
|
1 |
|
$prefix.'/{year}/{month}', |
214
|
1 |
|
self::$TOKEN_YEAR + |
215
|
1 |
|
self::$TOKEN_MONTH, |
216
|
|
|
MonthAction::class |
217
|
|
|
], |
218
|
|
|
Route::YEAR => [ |
219
|
1 |
|
$prefix.'/{year}', |
220
|
1 |
|
self::$TOKEN_YEAR, |
221
|
|
|
YearAction::class |
222
|
|
|
] |
223
|
|
|
], |
224
|
1 |
|
6 - $precision |
225
|
|
|
); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* Build and return the default object routes |
230
|
|
|
* |
231
|
|
|
* @param string $prefix Repository route prefix |
232
|
|
|
* @param array $baseDateRoute Base date route |
233
|
|
|
* @param int $numDefaultRoutes Total number of date routes |
234
|
|
|
* @return array Default object routes |
235
|
|
|
*/ |
236
|
1 |
|
protected static function buildDefaultObjectRoutes($prefix, $baseDateRoute, $numDefaultRoutes) |
237
|
|
|
{ |
238
|
|
|
// Build a regular expression for all supported object types |
239
|
1 |
|
$enabledObjectTypes = '(?:-(?:(?:'.implode(')|(?:', array_map('preg_quote', Object::getSupportedTypes())).')))'; |
|
|
|
|
240
|
|
|
|
241
|
|
|
return [ |
242
|
|
|
// Default object route |
243
|
|
|
Route::OBJECT => [ |
244
|
1 |
|
$prefix.$baseDateRoute[0].'/{hidden}{id}{type}{draft}{revision}{format}', |
245
|
1 |
|
$baseDateRoute[1] + [ |
246
|
1 |
|
'hidden' => '\.?', |
247
|
1 |
|
'id' => '\d+', |
248
|
1 |
|
'type' => $enabledObjectTypes.'?', |
249
|
1 |
|
'draft' => '(?:/(\.)?\\'.(2 + $numDefaultRoutes).')?', |
250
|
1 |
|
'revision' => '(?('.(5 + $numDefaultRoutes).')|(?:-\d+)?)', |
251
|
1 |
|
'format' => '(?('.(4 + $numDefaultRoutes).')(?:\.'.preg_quote(getenv('OBJECT_RESOURCE_EXTENSION')).')?)', |
252
|
|
|
], |
253
|
|
|
ObjectAction::class |
254
|
1 |
|
], |
255
|
|
|
// Default type route |
256
|
|
|
Route::TYPE => [ |
257
|
1 |
|
$baseDateRoute[0].'/{hidden}{id}{type}{draft}{revision}{format}', |
258
|
1 |
|
$baseDateRoute[1] + [ |
259
|
1 |
|
'hidden' => '\.?', |
260
|
1 |
|
'id' => self::REGEX_ASTERISK, |
261
|
1 |
|
'type' => $enabledObjectTypes.'?', |
262
|
1 |
|
'draft' => '(?:/(\.)?'.self::REGEX_ASTERISK.')?', |
263
|
1 |
|
'revision' => '(?('.(5 + $numDefaultRoutes).')|(?:-\d+)?)', |
264
|
1 |
|
'format' => '(?('.(4 + $numDefaultRoutes).')(?:\.'.preg_quote(getenv('OBJECT_RESOURCE_EXTENSION')).')?)', |
265
|
|
|
], |
266
|
|
|
TypeAction::class |
267
|
|
|
] |
268
|
|
|
]; |
269
|
|
|
} |
270
|
|
|
} |
271
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.