Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add debug stuff to application's fallback handler #145

Merged
merged 12 commits into from
Jun 21, 2024
1 change: 1 addition & 0 deletions composer-require-checker.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"Yiisoft\\Assets\\AssetManager",
"Yiisoft\\Assets\\AssetPublisherInterface",
"Yiisoft\\Yii\\View\\ViewRenderer",
"Yiisoft\\Yii\\Http\\Application",
"Codeception\\Event\\FailEvent",
"Codeception\\Event\\PrintResultEvent",
"Codeception\\Event\\TestEvent",
Expand Down
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

Check failure on line 18 in src/Debug/Http/DebugHttpApplicationWrapper.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.1-ubuntu-latest

UndefinedClass

src/Debug/Http/DebugHttpApplicationWrapper.php:18:26: UndefinedClass: Class, interface or enum named Yiisoft\Yii\Http\Application does not exist (see https://psalm.dev/019)

Check failure on line 18 in src/Debug/Http/DebugHttpApplicationWrapper.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.2-ubuntu-latest

UndefinedClass

src/Debug/Http/DebugHttpApplicationWrapper.php:18:26: UndefinedClass: Class, interface or enum named Yiisoft\Yii\Http\Application does not exist (see https://psalm.dev/019)

Check failure on line 18 in src/Debug/Http/DebugHttpApplicationWrapper.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.3-ubuntu-latest

UndefinedClass

src/Debug/Http/DebugHttpApplicationWrapper.php:18:26: UndefinedClass: Class, interface or enum named Yiisoft\Yii\Http\Application does not exist (see https://psalm.dev/019)
{
$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;
}
}
10 changes: 9 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,16 @@
public function getExtensions(): array
{
return [
RouteCollectorInterface::class => static function (ContainerInterface $container, RouteCollectorInterface $routeCollector) {
RouteCollectorInterface::class => static function (RouteCollectorInterface $routeCollector) {
$routeCollector->prependMiddleware(DebugHeaders::class);
return $routeCollector;
},
Application::class => static function (ContainerInterface $container, Application $application) {

Check failure on line 31 in src/Debug/Provider/DebugApiProvider.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.1-ubuntu-latest

UndefinedClass

src/Debug/Provider/DebugApiProvider.php:31:13: UndefinedClass: Class, interface or enum named Yiisoft\Yii\Http\Application does not exist (see https://psalm.dev/019)

Check failure on line 31 in src/Debug/Provider/DebugApiProvider.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.1-ubuntu-latest

UndefinedClass

src/Debug/Provider/DebugApiProvider.php:31:83: UndefinedClass: Class, interface or enum named Yiisoft\Yii\Http\Application does not exist (see https://psalm.dev/019)

Check failure on line 31 in src/Debug/Provider/DebugApiProvider.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.2-ubuntu-latest

UndefinedClass

src/Debug/Provider/DebugApiProvider.php:31:13: UndefinedClass: Class, interface or enum named Yiisoft\Yii\Http\Application does not exist (see https://psalm.dev/019)

Check failure on line 31 in src/Debug/Provider/DebugApiProvider.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.2-ubuntu-latest

UndefinedClass

src/Debug/Provider/DebugApiProvider.php:31:83: UndefinedClass: Class, interface or enum named Yiisoft\Yii\Http\Application does not exist (see https://psalm.dev/019)

Check failure on line 31 in src/Debug/Provider/DebugApiProvider.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.3-ubuntu-latest

UndefinedClass

src/Debug/Provider/DebugApiProvider.php:31:13: UndefinedClass: Class, interface or enum named Yiisoft\Yii\Http\Application does not exist (see https://psalm.dev/019)

Check failure on line 31 in src/Debug/Provider/DebugApiProvider.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.3-ubuntu-latest

UndefinedClass

src/Debug/Provider/DebugApiProvider.php:31:83: UndefinedClass: Class, interface or enum named Yiisoft\Yii\Http\Application does not exist (see https://psalm.dev/019)
$applicationWrapper = $container->get(DebugHttpApplicationWrapper::class);
$applicationWrapper->wrap($application);

return $application;
},
];
}
}
Loading