|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Copyright (C) 2016 SURFnet. |
|
4
|
|
|
* |
|
5
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
6
|
|
|
* it under the terms of the GNU Affero General Public License as |
|
7
|
|
|
* published by the Free Software Foundation, either version 3 of the |
|
8
|
|
|
* License, or (at your option) any later version. |
|
9
|
|
|
* |
|
10
|
|
|
* This program is distributed in the hope that it will be useful, |
|
11
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13
|
|
|
* GNU Affero General Public License for more details. |
|
14
|
|
|
* |
|
15
|
|
|
* You should have received a copy of the GNU Affero General Public License |
|
16
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
17
|
|
|
*/ |
|
18
|
|
|
|
|
19
|
|
|
namespace fkooman\RemoteStorage\Http; |
|
20
|
|
|
|
|
21
|
|
|
use fkooman\RemoteStorage\Http\Exception\HttpException; |
|
22
|
|
|
use fkooman\RemoteStorage\Http\Exception\InputValidationException; |
|
23
|
|
|
use fkooman\RemoteStorage\TplInterface; |
|
24
|
|
|
|
|
25
|
|
|
class Service |
|
26
|
|
|
{ |
|
27
|
|
|
/** @var \fkooman\RemoteStorage\TplInterface|null */ |
|
28
|
|
|
private $tpl = null; |
|
29
|
|
|
|
|
30
|
|
|
/** @var array */ |
|
31
|
|
|
private $routes = [ |
|
32
|
|
|
'GET' => [], |
|
33
|
|
|
'POST' => [], |
|
34
|
|
|
]; |
|
35
|
|
|
|
|
36
|
|
|
/** @var array */ |
|
37
|
|
|
private $beforeHooks = []; |
|
38
|
|
|
|
|
39
|
|
|
/** @var array */ |
|
40
|
|
|
private $afterHooks = []; |
|
41
|
|
|
|
|
42
|
|
|
public function setTpl(TplInterface $tpl) |
|
43
|
|
|
{ |
|
44
|
|
|
$this->tpl = $tpl; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function addBeforeHook($name, BeforeHookInterface $beforeHook) |
|
48
|
|
|
{ |
|
49
|
|
|
$this->beforeHooks[$name] = $beforeHook; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function addAfterHook($name, AfterHookInterface $afterHook) |
|
53
|
|
|
{ |
|
54
|
|
|
$this->afterHooks[$name] = $afterHook; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function addRoute($requestMethod, $pathInfo, callable $callback) |
|
58
|
|
|
{ |
|
59
|
|
|
$this->routes[$requestMethod][$pathInfo] = $callback; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function get($pathInfo, callable $callback) |
|
63
|
|
|
{ |
|
64
|
|
|
$this->addRoute('GET', $pathInfo, $callback); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function post($pathInfo, callable $callback) |
|
68
|
|
|
{ |
|
69
|
|
|
$this->addRoute('POST', $pathInfo, $callback); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function addModule(ServiceModuleInterface $module) |
|
73
|
|
|
{ |
|
74
|
|
|
$module->init($this); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function run(Request $request) |
|
78
|
|
|
{ |
|
79
|
|
|
try { |
|
80
|
|
|
// before hooks |
|
81
|
|
|
$hookData = []; |
|
82
|
|
|
foreach ($this->beforeHooks as $k => $v) { |
|
83
|
|
|
$hookResponse = $v->executeBefore($request, $hookData); |
|
84
|
|
|
// if we get back a Response object, return it immediately |
|
85
|
|
|
if ($hookResponse instanceof Response) { |
|
86
|
|
|
// run afterHooks |
|
87
|
|
|
return $this->runAfterHooks($request, $hookResponse); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
$hookData[$k] = $hookResponse; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
$requestMethod = $request->getRequestMethod(); |
|
94
|
|
|
$pathInfo = $request->getPathInfo(); |
|
95
|
|
|
|
|
96
|
|
|
if (!array_key_exists($requestMethod, $this->routes)) { |
|
97
|
|
|
throw new HttpException( |
|
98
|
|
|
sprintf('method "%s" not allowed', $requestMethod), |
|
99
|
|
|
405, |
|
100
|
|
|
['Allow' => implode(',', array_keys($this->routes))] |
|
101
|
|
|
); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
if (!array_key_exists($pathInfo, $this->routes[$requestMethod])) { |
|
105
|
|
|
if (!array_key_exists('*', $this->routes[$requestMethod])) { |
|
106
|
|
|
throw new HttpException( |
|
107
|
|
|
sprintf('"%s" not found', $pathInfo), |
|
108
|
|
|
404 |
|
109
|
|
|
); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
$pathInfo = '*'; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
$response = $this->routes[$requestMethod][$pathInfo]($request, $hookData); |
|
116
|
|
|
|
|
117
|
|
|
// after hooks |
|
118
|
|
|
return $this->runAfterHooks($request, $response); |
|
119
|
|
|
} catch (InputValidationException $e) { |
|
120
|
|
|
// in case an InputValidationException is encountered, convert it |
|
121
|
|
|
// into a Bad Request HTTP response |
|
122
|
|
|
throw new HttpException($e->getMessage(), 400); |
|
123
|
|
|
} catch (HttpException $e) { |
|
124
|
|
|
if ($request->isBrowser()) { |
|
125
|
|
|
if (is_null($this->tpl)) { |
|
126
|
|
|
// template not available |
|
127
|
|
|
$response = new Response($e->getCode(), 'text/plain'); |
|
128
|
|
|
$response->setBody(sprintf('%d: %s', $e->getCode(), $e->getMessage())); |
|
129
|
|
|
} else { |
|
130
|
|
|
// template available |
|
131
|
|
|
$response = new HtmlResponse( |
|
132
|
|
|
$this->tpl->render( |
|
133
|
|
|
'errorPage', |
|
134
|
|
|
[ |
|
135
|
|
|
'code' => $e->getCode(), |
|
136
|
|
|
'message' => $e->getMessage(), |
|
137
|
|
|
] |
|
138
|
|
|
), |
|
139
|
|
|
$e->getCode() |
|
140
|
|
|
); |
|
141
|
|
|
} |
|
142
|
|
|
} else { |
|
143
|
|
|
// not a browser |
|
144
|
|
|
$response = new JsonResponse( |
|
145
|
|
|
['error' => $e->getMessage()], |
|
146
|
|
|
$e->getCode() |
|
147
|
|
|
); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
foreach ($e->getResponseHeaders() as $key => $value) { |
|
151
|
|
|
$response->addHeader($key, $value); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
// after hooks |
|
155
|
|
|
return $this->runAfterHooks($request, $response); |
|
156
|
|
|
} |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
private function runAfterHooks(Request $request, Response $response) |
|
160
|
|
|
{ |
|
161
|
|
|
foreach ($this->afterHooks as $v) { |
|
162
|
|
|
$response = $v->executeAfter($request, $response); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
return $response; |
|
166
|
|
|
} |
|
167
|
|
|
} |
|
168
|
|
|
|