|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Tomaj\NetteApi; |
|
6
|
|
|
|
|
7
|
|
|
use Nette\Http\Response; |
|
8
|
|
|
use Tomaj\NetteApi\Authorization\ApiAuthorizationInterface; |
|
9
|
|
|
use Tomaj\NetteApi\Authorization\NoAuthorization; |
|
10
|
|
|
use Tomaj\NetteApi\Handlers\ApiHandlerInterface; |
|
11
|
|
|
use Tomaj\NetteApi\Handlers\CorsPreflightHandler; |
|
12
|
|
|
use Tomaj\NetteApi\Handlers\DefaultHandler; |
|
13
|
|
|
|
|
14
|
|
|
class ApiDecider |
|
15
|
|
|
{ |
|
16
|
|
|
/** @var Api[] */ |
|
17
|
|
|
private $apis = []; |
|
18
|
|
|
|
|
19
|
|
|
/** @var ApiHandlerInterface|null */ |
|
20
|
|
|
private $globalPreflightHandler = null; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Get api handler that match input method, version, package and apiAction. |
|
24
|
|
|
* If decider cannot find handler for given handler, returns defaults. |
|
25
|
|
|
* |
|
26
|
|
|
* @param string $method |
|
27
|
|
|
* @param integer $version |
|
28
|
|
|
* @param string $package |
|
29
|
|
|
* @param string $apiAction |
|
30
|
|
|
* |
|
31
|
|
|
* @return Api |
|
32
|
|
|
*/ |
|
33
|
27 |
|
public function getApi(string $method, int $version, string $package, ?string $apiAction = null) |
|
34
|
|
|
{ |
|
35
|
27 |
|
$method = strtoupper($method); |
|
36
|
27 |
|
$apiAction = $apiAction === '' ? null : $apiAction; |
|
37
|
|
|
|
|
38
|
27 |
|
foreach ($this->apis as $api) { |
|
39
|
24 |
|
$identifier = $api->getEndpoint(); |
|
40
|
24 |
|
if ($method === $identifier->getMethod() && $identifier->getVersion() === $version && $identifier->getPackage() === $package && $identifier->getApiAction() === $apiAction) { |
|
41
|
21 |
|
$endpointIdentifier = new EndpointIdentifier($method, $version, $package, $apiAction); |
|
42
|
21 |
|
$api->getHandler()->setEndpointIdentifier($endpointIdentifier); |
|
43
|
21 |
|
return $api; |
|
44
|
|
|
} |
|
45
|
9 |
|
if ($method === 'OPTIONS' && $this->globalPreflightHandler && $identifier->getVersion() === $version && $identifier->getPackage() === $package && $identifier->getApiAction() === $apiAction) { |
|
46
|
5 |
|
return new Api(new EndpointIdentifier('OPTIONS', $version, $package, $apiAction), $this->globalPreflightHandler, new NoAuthorization()); |
|
47
|
|
|
} |
|
48
|
|
|
} |
|
49
|
3 |
|
return new Api(new EndpointIdentifier($method, $version, $package, $apiAction), new DefaultHandler(), new NoAuthorization()); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
3 |
|
public function enableGlobalPreflight(ApiHandlerInterface $corsHandler = null) |
|
53
|
|
|
{ |
|
54
|
3 |
|
if (!$corsHandler) { |
|
55
|
3 |
|
$corsHandler = new CorsPreflightHandler(new Response()); |
|
56
|
|
|
} |
|
57
|
3 |
|
$this->globalPreflightHandler = $corsHandler; |
|
58
|
3 |
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Register new api handler |
|
62
|
|
|
* |
|
63
|
|
|
* @param EndpointInterface $endpointIdentifier |
|
64
|
|
|
* @param ApiHandlerInterface $handler |
|
65
|
|
|
* @param ApiAuthorizationInterface $apiAuthorization |
|
66
|
|
|
* @return self |
|
67
|
|
|
*/ |
|
68
|
27 |
|
public function addApi(EndpointInterface $endpointIdentifier, ApiHandlerInterface $handler, ApiAuthorizationInterface $apiAuthorization): self |
|
69
|
|
|
{ |
|
70
|
27 |
|
$this->apis[] = new Api($endpointIdentifier, $handler, $apiAuthorization); |
|
71
|
27 |
|
return $this; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Get all registered apis |
|
76
|
|
|
* |
|
77
|
|
|
* @return Api[] |
|
78
|
|
|
*/ |
|
79
|
12 |
|
public function getApis(): array |
|
80
|
|
|
{ |
|
81
|
12 |
|
return $this->apis; |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|