|
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 fkooman\Http\Exception\MethodNotAllowedException; |
|
21
|
|
|
use fkooman\Http\Exception\NotFoundException; |
|
22
|
|
|
use RuntimeException; |
|
23
|
|
|
|
|
24
|
|
|
class Service |
|
25
|
|
|
{ |
|
26
|
|
|
private $routes; |
|
27
|
|
|
private $hooks; |
|
28
|
|
|
|
|
29
|
|
|
public function __construct() |
|
30
|
|
|
{ |
|
31
|
|
|
$this->routes = []; |
|
32
|
|
|
$this->hooks = []; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function addHook($type, $name, callable $callback) |
|
36
|
|
|
{ |
|
37
|
|
|
if (!array_key_exists($type, $this->hooks)) { |
|
38
|
|
|
$this->hooks[$type] = []; |
|
39
|
|
|
} |
|
40
|
|
|
$this->hooks[$type][] = ['name' => $name, 'cb' => $callback]; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function addRoute($requestMethod, $pathInfo, callable $callback) |
|
44
|
|
|
{ |
|
45
|
|
|
$this->routes[$requestMethod][$pathInfo] = $callback; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function get($pathInfo, callable $callback) |
|
49
|
|
|
{ |
|
50
|
|
|
$this->addRoute('GET', $pathInfo, $callback); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function post($pathInfo, callable $callback) |
|
54
|
|
|
{ |
|
55
|
|
|
$this->addRoute('POST', $pathInfo, $callback); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function addModule(ServiceModuleInterface $module) |
|
59
|
|
|
{ |
|
60
|
|
|
$module->init($this); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public function run(array $serverData, array $getData, array $postData) |
|
64
|
|
|
{ |
|
65
|
|
|
if (!array_key_exists('REQUEST_METHOD', $serverData)) { |
|
66
|
|
|
throw new RuntimeException('invalid HTTP request, missing REQUEST_METHOD'); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
// before hooks |
|
70
|
|
|
$hookData = []; |
|
71
|
|
|
if (array_key_exists('before', $this->hooks)) { |
|
72
|
|
|
foreach ($this->hooks['before'] as $hook) { |
|
73
|
|
|
$hookData[$hook['name']] = $hook['cb']($serverData, $postData, $getData); |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
$requestMethod = $serverData['REQUEST_METHOD']; |
|
78
|
|
|
$pathInfo = '/'; |
|
79
|
|
|
if (array_key_exists('PATH_INFO', $serverData)) { |
|
80
|
|
|
$pathInfo = $serverData['PATH_INFO']; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
if (!array_key_exists($requestMethod, $this->routes)) { |
|
84
|
|
|
throw new MethodNotAllowedException(sprintf('method "%s" not allowed', $requestMethod)); |
|
|
|
|
|
|
85
|
|
|
} |
|
86
|
|
|
if (!array_key_exists($pathInfo, $this->routes[$requestMethod])) { |
|
87
|
|
|
throw new NotFoundException(sprintf('"%s" not found', $pathInfo)); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
return $this->routes[$requestMethod][$pathInfo]($serverData, $getData, $postData, $hookData); |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
This check looks for function calls that miss required arguments.