Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion src/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
36 changes: 30 additions & 6 deletions src/ApiDecider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand Down Expand Up @@ -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;
}

Expand All @@ -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<object> $handler
*/
$apiHandler = $this->container->getByType($handler);
return $apiHandler;
}
}
50 changes: 50 additions & 0 deletions src/ApiHolder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

declare(strict_types=1);

namespace Tomaj\NetteApi;

use Tomaj\NetteApi\Authorization\ApiAuthorizationInterface;
use Tomaj\NetteApi\Handlers\ApiHandlerInterface;
use Tomaj\NetteApi\RateLimit\NoRateLimit;
use Tomaj\NetteApi\RateLimit\RateLimitInterface;

class ApiHolder
{
private RateLimitInterface $rateLimit;

/**
* Allows to define api with handler as a string.
* Alows asynchronous loading of handlers if handler is defined as a string.
* ApiDecider will try to load the handler from container.
* Should not be used outside of ApiDecider.
*/
public function __construct(
private EndpointInterface $endpoint,
private ApiHandlerInterface|string $handler,
private ApiAuthorizationInterface $authorization,
?RateLimitInterface $rateLimit = null,
) {
$this->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;
}
}