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