Skip to content

Commit

Permalink
Support HttpDriverFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
bwoebi committed Feb 18, 2024
1 parent c0434ad commit d61f888
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@
"email": "[email protected]"
}
],
"minimum-stability": "dev",
"require": {
"php": ">=8.1",
"amphp/amp": "^3",
"amphp/cache": "^2",
"amphp/http": "^2",
"amphp/http-server": "^3",
"amphp/http": "dev-structured-fields as v2.1.0",
"amphp/http-server": "dev-http3 as 3.1",
"amphp/socket": "^2",
"nikic/fast-route": "^1",
"psr/log": "^1|^2|^3"
Expand Down
27 changes: 26 additions & 1 deletion src/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,18 @@
use Amp\ForbidCloning;
use Amp\ForbidSerialization;
use Amp\Http\HttpStatus;
use Amp\Http\Server\Driver\Client;
use Amp\Http\Server\Driver\HttpDriver;
use Amp\Http\Server\Driver\HttpDriverFactory;
use Amp\Http\Server\Driver\HttpDriverMergedFactory;
use Amp\Http\Server\Driver\HttpDriverMiddleware;
use Amp\Http\Server\RequestHandler\ClosureRequestHandler;
use FastRoute\Dispatcher;
use FastRoute\RouteCollector;
use Psr\Log\LoggerInterface;
use function FastRoute\simpleDispatcher;

final class Router implements RequestHandler
final class Router implements RequestHandler, HttpDriverMiddleware
{
use ForbidCloning;
use ForbidSerialization;
Expand Down Expand Up @@ -113,6 +118,26 @@ public function handleRequest(Request $request): Response
}
}

public function createHttpDriver(HttpDriverFactory $factory, RequestHandler $requestHandler, ErrorHandler $errorHandler, Client $client): HttpDriver
{
$middlewares = [];
foreach ($this->routes as [, , $routeHandler]) {
if ($requestHandler instanceof HttpDriverMiddleware) {
$middlewares[] = $routeHandler;
}
}

if ($middlewares) {
if (count($middlewares) == 1) {
return $middlewares[0]->createHttpDriver($factory, $requestHandler, $errorHandler, $client);

Check failure on line 132 in src/Router.php

View workflow job for this annotation

GitHub Actions / PHP 8.1

UndefinedInterfaceMethod

src/Router.php:132:41: UndefinedInterfaceMethod: Method Amp\Http\Server\RequestHandler::createHttpDriver does not exist (see https://psalm.dev/181)
}

$factory = new HttpDriverMergedFactory($middlewares, $factory);

Check failure on line 135 in src/Router.php

View workflow job for this annotation

GitHub Actions / PHP 8.1

InvalidArgument

src/Router.php:135:52: InvalidArgument: Argument 1 of Amp\Http\Server\Driver\HttpDriverMergedFactory::__construct expects list<Amp\Http\Server\Driver\HttpDriverMiddleware>, but non-empty-list<Amp\Http\Server\RequestHandler> provided (see https://psalm.dev/004)
}

return $factory->createHttpDriver($requestHandler, $errorHandler, $client);
}

/**
* Merge another router's routes into this router.
*
Expand Down

0 comments on commit d61f888

Please sign in to comment.