diff --git a/src/CoreHandler.php b/src/CoreHandler.php index e61f8cb..7eac6c1 100644 --- a/src/CoreHandler.php +++ b/src/CoreHandler.php @@ -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; @@ -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; } /** @@ -109,18 +102,11 @@ 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, @@ -128,7 +114,7 @@ public function handle(Request $request): Response ] ) ); - } catch (TargetCallException $e) { + } catch (ControllerException $e) { \ob_get_clean(); throw $this->mapException($e); } catch (\Throwable $e) { @@ -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), }; } diff --git a/src/Loader/Configurator/ImportConfigurator.php b/src/Loader/Configurator/ImportConfigurator.php index 8f06263..61d5357 100644 --- a/src/Loader/Configurator/ImportConfigurator.php +++ b/src/Loader/Configurator/ImportConfigurator.php @@ -6,7 +6,6 @@ use Psr\Http\Server\MiddlewareInterface; use Spiral\Core\CoreInterface; -use Spiral\Interceptors\HandlerInterface; use Spiral\Router\RouteCollection; final class ImportConfigurator @@ -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); diff --git a/src/Loader/Configurator/RouteConfigurator.php b/src/Loader/Configurator/RouteConfigurator.php index b6ed22a..12e38d4 100644 --- a/src/Loader/Configurator/RouteConfigurator.php +++ b/src/Loader/Configurator/RouteConfigurator.php @@ -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; @@ -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 */ @@ -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; diff --git a/src/RouteGroup.php b/src/RouteGroup.php index 0ef0384..bee98bf 100644 --- a/src/RouteGroup.php +++ b/src/RouteGroup.php @@ -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; /** @@ -28,7 +27,7 @@ final class RouteGroup /** @var array */ 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 */ @@ -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; @@ -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); } diff --git a/src/Target/AbstractTarget.php b/src/Target/AbstractTarget.php index 8d2231a..52b0fdb 100644 --- a/src/Target/AbstractTarget.php +++ b/src/Target/AbstractTarget.php @@ -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; @@ -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; @@ -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; @@ -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) diff --git a/tests/BaseTestCase.php b/tests/BaseTestCase.php index 1654e11..2c8ced7 100644 --- a/tests/BaseTestCase.php +++ b/tests/BaseTestCase.php @@ -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; @@ -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); diff --git a/tests/RouteGroupTest.php b/tests/RouteGroupTest.php index c4bb000..c0e8679 100644 --- a/tests/RouteGroupTest.php +++ b/tests/RouteGroupTest.php @@ -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 @@ -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