diff --git a/CHANGELOG.md b/CHANGELOG.md index 77fc0dc..cc2d224 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) princip ### Changed * update minimum PHP to version 8.2 (currently still supported version) + update league/fractal library [BC] * change Inject from PHPDoc format to native PHP attribute +* [Reverted] Remove BC addApi register method now requires ApiHandlerInterface as second parameter instead of string or service name ## 3.2.0 diff --git a/src/Api.php b/src/Api.php index 8435789..06a117f 100644 --- a/src/Api.php +++ b/src/Api.php @@ -17,7 +17,7 @@ public function __construct( private EndpointInterface $endpoint, private ApiHandlerInterface $handler, private ApiAuthorizationInterface $authorization, - ?RateLimitInterface $rateLimit = null + ?RateLimitInterface $rateLimit = null, ) { $this->rateLimit = $rateLimit ?: new NoRateLimit(); } diff --git a/src/ApiDecider.php b/src/ApiDecider.php index 4220bc6..712eddf 100644 --- a/src/ApiDecider.php +++ b/src/ApiDecider.php @@ -4,6 +4,7 @@ namespace Tomaj\NetteApi; +use Nette\DI\Container; use Nette\Http\Response; use Tomaj\NetteApi\Authorization\ApiAuthorizationInterface; use Tomaj\NetteApi\Authorization\NoAuthorization; @@ -15,11 +16,16 @@ class ApiDecider { - /** @var Api[] */ - private $apis = []; + /** @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. @@ -62,9 +68,9 @@ public function enableGlobalPreflight(?CorsPreflightHandlerInterface $corsHandle /** * Register new api handler */ - public function addApi(EndpointInterface $endpointIdentifier, ApiHandlerInterface $handler, ApiAuthorizationInterface $apiAuthorization, ?RateLimitInterface $rateLimit = null): self + public function addApi(EndpointInterface $endpointIdentifier, ApiHandlerInterface|string $handler, ApiAuthorizationInterface $apiAuthorization, ?RateLimitInterface $rateLimit = null): self { - $this->apis[] = new Api($endpointIdentifier, $handler, $apiAuthorization, $rateLimit); + $this->apis[] = new ApiHolder($endpointIdentifier, $handler, $apiAuthorization, $rateLimit); return $this; } @@ -84,8 +90,26 @@ public function getApis(): array return $apis; } - private function getHandler(Api $api): ApiHandlerInterface + private function getHandler(ApiHolder $api): ApiHandlerInterface { - return $api->getHandler(); + $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 $handler + */ + $apiHandler = $this->container->getByType($handler); + return $apiHandler; } } diff --git a/src/ApiHolder.php b/src/ApiHolder.php new file mode 100644 index 0000000..b57771a --- /dev/null +++ b/src/ApiHolder.php @@ -0,0 +1,50 @@ +rateLimit = $rateLimit ?: new NoRateLimit(); + } + + public function getEndpoint(): EndpointInterface + { + return $this->endpoint; + } + + public function getHandler(): ApiHandlerInterface|string + { + return $this->handler; + } + + public function getAuthorization(): ApiAuthorizationInterface + { + return $this->authorization; + } + + public function getRateLimit(): RateLimitInterface + { + return $this->rateLimit; + } +}