-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathApiDecider.php
More file actions
115 lines (97 loc) · 3.89 KB
/
ApiDecider.php
File metadata and controls
115 lines (97 loc) · 3.89 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php
declare(strict_types=1);
namespace Tomaj\NetteApi;
use Nette\DI\Container;
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\CorsPreflightHandlerInterface;
use Tomaj\NetteApi\Handlers\DefaultHandler;
use Tomaj\NetteApi\RateLimit\RateLimitInterface;
class ApiDecider
{
/** @var ApiHolder[] */
private array $apis = [];
private ?ApiHandlerInterface $globalPreflightHandler = null;
public function __construct(
private Container $container,
) {
}
/**
* Get api handler that match input method, version, package and apiAction.
* If decider cannot find handler for given handler, returns defaults.
*
* @param string $apiAction
*
* @return Api
*/
public function getApi(string $method, string $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);
$handler = $this->getHandler($api);
$handler->setEndpointIdentifier($endpointIdentifier);
return new Api($api->getEndpoint(), $handler, $api->getAuthorization(), $api->getRateLimit());
}
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(?CorsPreflightHandlerInterface $corsHandler = null): void
{
if (!$corsHandler) {
$corsHandler = new CorsPreflightHandler(new Response());
}
$this->globalPreflightHandler = $corsHandler;
}
/**
* Register new api handler
*/
public function addApi(EndpointInterface $endpointIdentifier, ApiHandlerInterface|string $handler, ApiAuthorizationInterface $apiAuthorization, ?RateLimitInterface $rateLimit = null): self
{
$this->apis[] = new ApiHolder($endpointIdentifier, $handler, $apiAuthorization, $rateLimit);
return $this;
}
/**
* Get all registered apis
*
* @return Api[]
*/
public function getApis(): array
{
$apis = [];
foreach ($this->apis as $api) {
$handler = $this->getHandler($api);
$apis[] = new Api($api->getEndpoint(), $handler, $api->getAuthorization(), $api->getRateLimit());
}
return $apis;
}
private function getHandler(ApiHolder $api): ApiHandlerInterface
{
$handler = $api->getHandler();
if (!is_string($handler)) {
return $handler;
}
if (str_starts_with($handler, '@')) {
/**
* @var ApiHandlerInterface $apiHandler
*/
$apiHandler = $this->container->getByName(substr($handler, 1));
return $apiHandler;
}
/**
* @var ApiHandlerInterface $apiHandler
* @var class-string<object> $handler
*/
$apiHandler = $this->container->getByType($handler);
return $apiHandler;
}
}