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