Skip to content

Commit

Permalink
Merge commit b3d9511b716c7b8631b59796b83c60767596a6ba into new-master
Browse files Browse the repository at this point in the history
  • Loading branch information
spiralbot committed May 15, 2024
1 parent 1b4b86b commit 402211d
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 59 deletions.
41 changes: 13 additions & 28 deletions src/CoreHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,15 @@
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Server\RequestHandlerInterface;
use Spiral\Core\CoreInterface;
use Spiral\Core\Exception\ControllerException;
use Spiral\Core\ScopeInterface;
use Spiral\Http\Exception\ClientException;
use Spiral\Http\Exception\ClientException\BadRequestException;
use Spiral\Http\Exception\ClientException\ForbiddenException;
use Spiral\Http\Exception\ClientException\NotFoundException;
use Spiral\Http\Exception\ClientException\ServerErrorException;
use Spiral\Http\Exception\ClientException\UnauthorizedException;
use Spiral\Http\Stream\GeneratorStream;
use Spiral\Http\Traits\JsonTrait;
use Spiral\Interceptors\Context\CallContext;
use Spiral\Interceptors\Context\Target;
use Spiral\Interceptors\Exception\TargetCallException;
use Spiral\Interceptors\HandlerInterface;
use Spiral\Router\Exception\HandlerException;
use Spiral\Telemetry\NullTracer;
use Spiral\Telemetry\TracerInterface;
Expand All @@ -41,16 +37,13 @@ final class CoreHandler implements RequestHandlerInterface
/** @readonly */
private ?array $parameters = null;

private bool $isLegacyPipeline;

public function __construct(
private readonly HandlerInterface|CoreInterface $core,
private readonly CoreInterface $core,
private readonly ScopeInterface $scope,
private readonly ResponseFactoryInterface $responseFactory,
?TracerInterface $tracer = null
) {
$this->tracer = $tracer ?? new NullTracer($scope);
$this->isLegacyPipeline = !$core instanceof HandlerInterface;
}

/**
Expand Down Expand Up @@ -109,26 +102,19 @@ public function handle(Request $request): Response
],
fn (): mixed => $this->tracer->trace(
name: 'Controller [' . $controller . ':' . $action . ']',
callback: $this->isLegacyPipeline
? fn (): mixed => $this->core->callAction(
controller: $controller,
action: $action,
parameters: $parameters,
)
: fn (): mixed => $this->core->handle(
new CallContext(
Target::fromPair($controller, $action),
$parameters,
),
),
callback: fn (): mixed => $this->core->callAction(
controller: $controller,
action: $action,
parameters: $parameters,
),
attributes: [
'route.controller' => $this->controller,
'route.action' => $action,
'route.parameters' => \array_keys($parameters),
]
)
);
} catch (TargetCallException $e) {
} catch (ControllerException $e) {
\ob_get_clean();
throw $this->mapException($e);
} catch (\Throwable $e) {
Expand Down Expand Up @@ -183,14 +169,13 @@ private function wrapResponse(Response $response, mixed $result = null, string $
/**
* Converts core specific ControllerException into HTTP ClientException.
*/
private function mapException(TargetCallException $exception): ClientException
private function mapException(ControllerException $exception): ClientException
{
return match ($exception->getCode()) {
TargetCallException::BAD_ACTION,
TargetCallException::NOT_FOUND => new NotFoundException('Not found', $exception),
TargetCallException::FORBIDDEN => new ForbiddenException('Forbidden', $exception),
TargetCallException::UNAUTHORIZED => new UnauthorizedException('Unauthorized', $exception),
TargetCallException::INVALID_CONTROLLER => new ServerErrorException('Server error', $exception),
ControllerException::BAD_ACTION,
ControllerException::NOT_FOUND => new NotFoundException('Not found', $exception),
ControllerException::FORBIDDEN => new ForbiddenException('Forbidden', $exception),
ControllerException::UNAUTHORIZED => new UnauthorizedException('Unauthorized', $exception),
default => new BadRequestException('Bad request', $exception),
};
}
Expand Down
3 changes: 1 addition & 2 deletions src/Loader/Configurator/ImportConfigurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use Psr\Http\Server\MiddlewareInterface;
use Spiral\Core\CoreInterface;
use Spiral\Interceptors\HandlerInterface;
use Spiral\Router\RouteCollection;

final class ImportConfigurator
Expand Down Expand Up @@ -78,7 +77,7 @@ public function namePrefix(string $prefix): self
return $this;
}

public function core(HandlerInterface|CoreInterface $core): self
public function core(CoreInterface $core): self
{
foreach ($this->routes->all() as $configurator) {
$configurator->core($core);
Expand Down
5 changes: 2 additions & 3 deletions src/Loader/Configurator/RouteConfigurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Spiral\Core\CoreInterface;
use Spiral\Interceptors\HandlerInterface;
use Spiral\Router\Exception\TargetException;
use Spiral\Router\RouteCollection;
use Spiral\Router\Target\Action;
Expand All @@ -22,7 +21,7 @@ final class RouteConfigurator
private ?string $group = null;
private ?array $methods = null;
private string $prefix = '';
private HandlerInterface|CoreInterface|null $core = null;
private ?CoreInterface $core = null;
private ?array $middleware = null;

/** @var null|string|callable|RequestHandlerInterface|TargetInterface */
Expand Down Expand Up @@ -134,7 +133,7 @@ public function prefix(string $prefix): self
return $this;
}

public function core(HandlerInterface|CoreInterface $core): self
public function core(CoreInterface $core): self
{
$this->core = $core;

Expand Down
7 changes: 3 additions & 4 deletions src/RouteGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use Spiral\Core\Container\Autowire;
use Spiral\Core\CoreInterface;
use Spiral\Core\FactoryInterface;
use Spiral\Interceptors\HandlerInterface;
use Spiral\Router\Target\AbstractTarget;

/**
Expand All @@ -28,7 +27,7 @@ final class RouteGroup
/** @var array<MiddlewareType> */
private array $middleware = [];

private Autowire|HandlerInterface|CoreInterface|string|null $core = null;
private Autowire|CoreInterface|string|null $core = null;

public function __construct(
/** @deprecated since v3.3.0 */
Expand Down Expand Up @@ -68,7 +67,7 @@ public function setNamePrefix(string $prefix): self
return $this;
}

public function setCore(Autowire|CoreInterface|HandlerInterface|string $core): self
public function setCore(Autowire|CoreInterface|string $core): self
{
$this->core = $core;

Expand All @@ -94,7 +93,7 @@ public function register(RouterInterface $router, FactoryInterface $factory): vo
{
foreach ($this->routes as $name => $route) {
if ($this->core !== null) {
if (!$this->core instanceof CoreInterface && !$this->core instanceof HandlerInterface) {
if (!$this->core instanceof CoreInterface) {
$this->core = $factory->make($this->core);
}

Expand Down
22 changes: 4 additions & 18 deletions src/Target/AbstractTarget.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use Psr\Http\Server\RequestHandlerInterface as Handler;
use Spiral\Core\CoreInterface;
use Spiral\Core\ScopeInterface;
use Spiral\Interceptors\HandlerInterface;
use Spiral\Router\CoreHandler;
use Spiral\Router\Exception\TargetException;
use Spiral\Router\TargetInterface;
Expand All @@ -25,7 +24,7 @@ abstract class AbstractTarget implements TargetInterface
// Automatically prepend HTTP verb to all action names.
public const RESTFUL = 1;

private HandlerInterface|CoreInterface|null $pipeline = null;
private ?CoreInterface $core = null;
private ?CoreHandler $handler = null;
private bool $verbActions;

Expand All @@ -50,24 +49,11 @@ public function getConstrains(): array

/**
* @mutation-free
* @deprecated Use {@see withHandler()} instead.
*/
public function withCore(HandlerInterface|CoreInterface $core): TargetInterface
public function withCore(CoreInterface $core): TargetInterface
{
$target = clone $this;
$target->pipeline = $core;
$target->handler = null;

return $target;
}

/**
* @mutation-free
*/
public function withHandler(HandlerInterface $handler): TargetInterface
{
$target = clone $this;
$target->pipeline = $handler;
$target->core = $core;
$target->handler = null;

return $target;
Expand All @@ -91,7 +77,7 @@ protected function coreHandler(ContainerInterface $container): CoreHandler
try {
// construct on demand
$this->handler = new CoreHandler(
$this->pipeline ?? $container->get(HandlerInterface::class),
$this->core ?? $container->get(CoreInterface::class),
$container->get(ScopeInterface::class),
$container->get(ResponseFactoryInterface::class),
$container->get(TracerInterface::class)
Expand Down
2 changes: 0 additions & 2 deletions tests/BaseTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
use Spiral\Core\Container\Autowire;
use Spiral\Core\CoreInterface;
use Spiral\Http\Config\HttpConfig;
use Spiral\Interceptors\HandlerInterface;
use Spiral\Router\GroupRegistry;
use Spiral\Router\Loader\Configurator\RoutingConfigurator;
use Spiral\Router\Loader\DelegatingLoader;
Expand Down Expand Up @@ -88,7 +87,6 @@ private function initContainer(): void
)
);

$this->container->bind(HandlerInterface::class, Core::class);
$this->container->bind(CoreInterface::class, Core::class);
$this->container->bindSingleton(GroupRegistry::class, GroupRegistry::class);
$this->container->bindSingleton(RoutingConfigurator::class, RoutingConfigurator::class);
Expand Down
4 changes: 2 additions & 2 deletions tests/RouteGroupTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function testCoreString(): void
$this->assertSame('controller', $this->getProperty($t, 'controller'));
$this->assertSame('method', $this->getProperty($t, 'action'));

$this->assertInstanceOf(RoutesTestCore::class, $this->getActionProperty($t, 'pipeline'));
$this->assertInstanceOf(RoutesTestCore::class, $this->getActionProperty($t, 'core'));
}

public function testCoreObject(): void
Expand All @@ -63,7 +63,7 @@ public function testCoreObject(): void
$this->assertSame('controller', $this->getProperty($t, 'controller'));
$this->assertSame('method', $this->getProperty($t, 'action'));

$this->assertInstanceOf(RoutesTestCore::class, $this->getActionProperty($t, 'pipeline'));
$this->assertInstanceOf(RoutesTestCore::class, $this->getActionProperty($t, 'core'));
}

public function testGroupHasRoute(): void
Expand Down

0 comments on commit 402211d

Please sign in to comment.