Skip to content

Commit

Permalink
Wrap application's fallback handler to provider debug headers
Browse files Browse the repository at this point in the history
  • Loading branch information
xepozz committed Jun 12, 2024
1 parent 3486414 commit af5fd05
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 1 deletion.
6 changes: 6 additions & 0 deletions config/di-web.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Cycle\Database\DatabaseProviderInterface;
use Psr\Container\ContainerInterface;
use Yiisoft\Db\Connection\ConnectionInterface;
use Yiisoft\Yii\Debug\Api\Debug\Middleware\DebugFallbackHandler;
use Yiisoft\Yii\Debug\Api\Debug\Repository\CollectorRepository;
use Yiisoft\Yii\Debug\Api\Debug\Repository\CollectorRepositoryInterface;
use Yiisoft\Yii\Debug\Api\Inspector\Database\Cycle\CycleSchemaProvider;
Expand Down Expand Up @@ -34,4 +35,9 @@
)
);
},
DebugFallbackHandler::class => [
'__construct()' => [
'middlewareDefinitions' => $params['yiisoft/yii-debug-api']['fallbackHandler']['middlewares'],
],
],
];
6 changes: 6 additions & 0 deletions config/params.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
declare(strict_types=1);

use Codeception\Extension;
use Yiisoft\Yii\Debug\Api\Debug\Middleware\DebugHeaders;
use Yiisoft\Yii\Debug\Api\Inspector\Command\CodeceptionCommand;
use Yiisoft\Yii\Debug\Api\Inspector\Command\PHPUnitCommand;
use Yiisoft\Yii\Debug\Api\Inspector\Command\PsalmCommand;
Expand Down Expand Up @@ -34,6 +35,11 @@
],
],
],
'fallbackHandler' => [
'middlewares' => [
DebugHeaders::class,
],
]
],
'yiisoft/yii-swagger' => [
'annotation-paths' => [
Expand Down
27 changes: 27 additions & 0 deletions src/Debug/Http/DebugHttpApplicationWrapper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Yii\Debug\Api\Debug\Http;

use Closure;
use Yiisoft\Yii\Debug\Api\Debug\Middleware\DebugFallbackHandler;
use Yiisoft\Yii\Http\Application;

final readonly class DebugHttpApplicationWrapper
{
public function __construct(
private DebugFallbackHandler $debugFallbackHandler
) {
}

public function wrap(Application $application): void
{
$debugFallbackHandler = $this->debugFallbackHandler;
$closure = Closure::bind(static function (Application $application) use ($debugFallbackHandler) {
$application->fallbackHandler = $debugFallbackHandler->withFallbackRequestHandler($application->fallbackHandler);
}, null, $application);

$closure($application);
}
}
46 changes: 46 additions & 0 deletions src/Debug/Middleware/DebugFallbackHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Yii\Debug\Api\Debug\Middleware;

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Yiisoft\Middleware\Dispatcher\MiddlewareDispatcher;

final class DebugFallbackHandler implements MiddlewareInterface, RequestHandlerInterface
{
private ?RequestHandlerInterface $fallbackHandler = null;

public function __construct(
private readonly MiddlewareDispatcher $middlewareDispatcher,
private readonly array $middlewareDefinitions,
) {
}

public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
return $this->middlewareDispatcher
->withMiddlewares($this->middlewareDefinitions)
->dispatch($request, $handler);
}

public function handle(ServerRequestInterface $request): ResponseInterface
{
if ($this->fallbackHandler === null) {
throw new \RuntimeException('No fallback handler defined.');
}

return $this->process($request, $this->fallbackHandler);
}

public function withFallbackRequestHandler(RequestHandlerInterface $fallbackHandler): self
{
$new = clone $this;
$new->fallbackHandler = $fallbackHandler;

return $new;
}
}
13 changes: 12 additions & 1 deletion src/Debug/Provider/DebugApiProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
use Psr\Container\ContainerInterface;
use Yiisoft\Di\ServiceProviderInterface;
use Yiisoft\Router\RouteCollectorInterface;
use Yiisoft\Yii\Debug\Api\Debug\Http\DebugHttpApplicationWrapper;
use Yiisoft\Yii\Debug\Api\Debug\Middleware\DebugHeaders;
use Yiisoft\Yii\Http\Application;

final class DebugApiProvider implements ServiceProviderInterface
{
Expand All @@ -22,10 +24,19 @@ public function getDefinitions(): array
public function getExtensions(): array
{
return [
RouteCollectorInterface::class => static function (ContainerInterface $container, RouteCollectorInterface $routeCollector) {
RouteCollectorInterface::class => static function (
ContainerInterface $container,
RouteCollectorInterface $routeCollector
) {
$routeCollector->prependMiddleware(DebugHeaders::class);
return $routeCollector;
},
Application::class => static function (ContainerInterface $container, Application $application) {
$applicationWrapper = $container->get(DebugHttpApplicationWrapper::class);
$applicationWrapper->wrap($application);

return $application;
},
];
}
}

0 comments on commit af5fd05

Please sign in to comment.