|
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
|
|
|
namespace SURFnet\VPN\Server\Api; |
|
19
|
|
|
|
|
20
|
|
|
use SURFnet\VPN\Server\Api\Exception\HttpException; |
|
21
|
|
|
|
|
22
|
|
|
class Service |
|
23
|
|
|
{ |
|
24
|
|
|
private $routes; |
|
25
|
|
|
private $hooks; |
|
26
|
|
|
|
|
27
|
|
|
public function __construct() |
|
28
|
|
|
{ |
|
29
|
|
|
$this->routes = []; |
|
30
|
|
|
$this->hooks = []; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function addHook($type, $name, callable $callback) |
|
34
|
|
|
{ |
|
35
|
|
|
if (!array_key_exists($type, $this->hooks)) { |
|
36
|
|
|
$this->hooks[$type] = []; |
|
37
|
|
|
} |
|
38
|
|
|
$this->hooks[$type][] = ['name' => $name, 'cb' => $callback]; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function addRoute($requestMethod, $pathInfo, callable $callback) |
|
42
|
|
|
{ |
|
43
|
|
|
$this->routes[$requestMethod][$pathInfo] = $callback; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function get($pathInfo, callable $callback) |
|
47
|
|
|
{ |
|
48
|
|
|
$this->addRoute('GET', $pathInfo, $callback); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function post($pathInfo, callable $callback) |
|
52
|
|
|
{ |
|
53
|
|
|
$this->addRoute('POST', $pathInfo, $callback); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function addModule(ServiceModuleInterface $module) |
|
57
|
|
|
{ |
|
58
|
|
|
$module->init($this); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function run(Request $request) |
|
62
|
|
|
{ |
|
63
|
|
|
try { |
|
64
|
|
|
// before hooks |
|
65
|
|
|
$hookData = []; |
|
66
|
|
|
if (array_key_exists('before', $this->hooks)) { |
|
67
|
|
|
foreach ($this->hooks['before'] as $hook) { |
|
68
|
|
|
$hookData[$hook['name']] = $hook['cb']($request); |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
$requestMethod = $request->getRequestMethod(); |
|
73
|
|
|
$pathInfo = $request->getPathInfo(); |
|
74
|
|
|
|
|
75
|
|
|
if (!array_key_exists($requestMethod, $this->routes)) { |
|
76
|
|
|
throw new HttpException( |
|
77
|
|
|
sprintf('method "%s" not allowed', $requestMethod), |
|
78
|
|
|
405 |
|
79
|
|
|
); |
|
80
|
|
|
} |
|
81
|
|
|
if (!array_key_exists($pathInfo, $this->routes[$requestMethod])) { |
|
82
|
|
|
throw new HttpException( |
|
83
|
|
|
sprintf('"%s" not found', $pathInfo), |
|
84
|
|
|
404 |
|
85
|
|
|
); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
return $this->routes[$requestMethod][$pathInfo]($request, $hookData); |
|
89
|
|
|
} catch (HttpException $e) { |
|
90
|
|
|
$response = new Response($e->getCode()); |
|
91
|
|
|
$response->setBody($e->getMessage()); |
|
92
|
|
|
|
|
93
|
|
|
return $response; |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|