diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 189105d..2f3eecb 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -12,5 +12,3 @@ jobs: uses: innmind/github-workflows/.github/workflows/psalm-matrix.yml@main cs: uses: innmind/github-workflows/.github/workflows/cs.yml@main - with: - php-version: '8.2' diff --git a/CHANGELOG.md b/CHANGELOG.md index 5375ac1..03c3582 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,14 @@ # Changelog +## [Unreleased] + +### Changed + +- Requires PHP `8.4` +- Requires `innmind/foundation:~2.1` +- Requires `innmind/http-parser:~4.0` +- The environment variables are now exposed as third argument of `Innmind\Async\HttpServer\Main::handle()` + ## 4.0.0 - 2025-08-24 ### Changed diff --git a/README.md b/README.md index 8530b34..6a0ba43 100644 --- a/README.md +++ b/README.md @@ -29,10 +29,17 @@ use Innmind\Http\{ }; use Innmind\Filesystem\Name; use Innmind\Url\Path; +use Innmind\Immutable\Map; new class extends Main { - protected static function handle(ServerRequest $request, OperatingSystem $os): Response - { + /** + * @param Map $env + */ + protected static function handle( + ServerRequest $request, + OperatingSystem $os, + Map $env, + ): Response { return $os ->filesystem() ->mount(Path::of('somewhere/')) diff --git a/composer.json b/composer.json index b45d0fe..fa95fe6 100644 --- a/composer.json +++ b/composer.json @@ -15,9 +15,9 @@ "issues": "http://github.com/innmind/async-http-server/issues" }, "require": { - "php": "~8.2", - "innmind/foundation": "~1.9", - "innmind/http-parser": "~3.0" + "php": "~8.4", + "innmind/foundation": "~2.1", + "innmind/http-parser": "~4.0" }, "autoload": { "psr-4": { @@ -30,7 +30,7 @@ } }, "require-dev": { - "innmind/static-analysis": "^1.2.1", + "innmind/static-analysis": "~1.3", "innmind/black-box": "~6.5", "innmind/coding-standard": "~2.0" } diff --git a/fixtures/server.php b/fixtures/server.php index 89f1367..817a5ee 100644 --- a/fixtures/server.php +++ b/fixtures/server.php @@ -13,10 +13,14 @@ Header\ContentLength, }; use Innmind\Filesystem\File\Content; +use Innmind\Immutable\Map; new class extends Main { - protected static function handle(ServerRequest $request, OperatingSystem $os): Response - { + protected static function handle( + ServerRequest $request, + OperatingSystem $os, + Map $env, + ): Response { return Response::of( StatusCode::ok, $request->protocolVersion(), diff --git a/src/Command/Serve.php b/src/Command/Serve.php index 485b75e..4661aee 100644 --- a/src/Command/Serve.php +++ b/src/Command/Serve.php @@ -6,7 +6,6 @@ use Innmind\Async\HttpServer\{ Server, Open, - InjectEnvironment, Display\Nothing, }; use Innmind\CLI\{ @@ -19,20 +18,22 @@ use Innmind\Http\{ ServerRequest, Response, - ServerRequest\Environment as HttpEnv, }; use Innmind\Url\Authority\Port; use Innmind\IP\IP; -use Innmind\Immutable\Attempt; +use Innmind\Immutable\{ + Attempt, + Map, +}; final class Serve implements Command { private OperatingSystem $os; - /** @var callable(ServerRequest, OperatingSystem): Response */ + /** @var callable(ServerRequest, OperatingSystem, Map): Response */ private $handle; /** - * @param callable(ServerRequest, OperatingSystem): Response $handle + * @param callable(ServerRequest, OperatingSystem, Map): Response $handle */ private function __construct( OperatingSystem $os, @@ -67,7 +68,7 @@ public function __invoke(Console $console): Attempt } /** - * @param callable(ServerRequest, OperatingSystem): Response $handle + * @param callable(ServerRequest, OperatingSystem, Map): Response $handle */ public static function of( OperatingSystem $os, @@ -97,11 +98,16 @@ public function usage(): Usage */ private function serve(Console $console, Open $open): Attempt { + $handle = $this->handle; + $server = Server::of( $this->os->clock(), $open, - InjectEnvironment::of(HttpEnv::of($console->variables())), - $this->handle, + static fn(ServerRequest $request, OperatingSystem $os) => $handle( + $request, + $os, + $console->variables(), + ), ); if ($console->options()->contains('no-output')) { diff --git a/src/Encode.php b/src/Encode.php index 5fe9672..2700189 100644 --- a/src/Encode.php +++ b/src/Encode.php @@ -3,7 +3,7 @@ namespace Innmind\Async\HttpServer; -use Innmind\TimeContinuum\Clock; +use Innmind\Time\Clock; use Innmind\Http\{ Response, Header\Date, diff --git a/src/InjectEnvironment.php b/src/InjectEnvironment.php deleted file mode 100644 index 76303c0..0000000 --- a/src/InjectEnvironment.php +++ /dev/null @@ -1,40 +0,0 @@ -env = $env; - } - - public function __invoke(ServerRequest $request): ServerRequest - { - return ServerRequest::of( - $request->url(), - $request->method(), - $request->protocolVersion(), - $request->headers(), - $request->body(), - $this->env, - $request->cookies(), - $request->query(), - $request->form(), - $request->files(), - ); - } - - public static function of(Environment $env): self - { - return new self($env); - } -} diff --git a/src/Main.php b/src/Main.php index b731bed..81c000d 100644 --- a/src/Main.php +++ b/src/Main.php @@ -14,7 +14,10 @@ ServerRequest, Response, }; -use Innmind\Immutable\Attempt; +use Innmind\Immutable\{ + Attempt, + Map, +}; abstract class Main extends Cli { @@ -28,9 +31,12 @@ protected function main(Environment $env, OperatingSystem $os): Attempt /** * The handler is static to prevent sharing state between requests + * + * @param Map $env Environment variables */ abstract protected static function handle( ServerRequest $request, OperatingSystem $os, + Map $env, ): Response; } diff --git a/src/Server.php b/src/Server.php index eaa5d3d..886fa7d 100644 --- a/src/Server.php +++ b/src/Server.php @@ -10,7 +10,7 @@ Task\Discard, }; use Innmind\OperatingSystem\OperatingSystem; -use Innmind\TimeContinuum\Clock; +use Innmind\Time\Clock; use Innmind\Filesystem\File\Content; use Innmind\HttpParser\{ Request\Parse, @@ -28,44 +28,29 @@ use Innmind\IO\Sockets\Servers\Server as IOServer; use Innmind\Immutable\{ Attempt, - Sequence, Maybe, Str, }; final class Server { - private Open $open; - private IOServer|IOServer\Pool|null $servers = null; - private InjectEnvironment $injectEnv; - private Encode $encode; - /** @var callable(ServerRequest, OperatingSystem): Response */ - private $handle; - private Display $display; - /** * @psalm-mutation-free * - * @param callable(ServerRequest, OperatingSystem): Response $handle + * @param \Closure(ServerRequest, OperatingSystem): Response $handle */ private function __construct( - Open $open, - InjectEnvironment $injectEnv, - Encode $encode, - callable $handle, - Display $display, + private Open $open, + private Encode $encode, + private \Closure $handle, + private Display $display, + private IOServer|IOServer\Pool|null $servers = null, ) { - $this->open = $open; - $this->injectEnv = $injectEnv; - $this->encode = $encode; - $this->handle = $handle; - $this->display = $display; } /** * @param Attempt $console * @param Continuation> $continuation - * @param Sequence $results * * @return Continuation> */ @@ -73,7 +58,6 @@ public function __invoke( Attempt $console, OperatingSystem $os, Continuation $continuation, - Sequence $results, ): Continuation { $failed = $console->match( static fn() => false, @@ -117,7 +101,6 @@ public function __invoke( ), ); - $injectEnv = $this->injectEnv; $handle = $this->handle; $encode = $this->encode; @@ -126,7 +109,6 @@ public function __invoke( ->accept() ->map(static fn($connection) => static function(OperatingSystem $os) use ( $connection, - $injectEnv, $handle, $encode, ) { @@ -141,7 +123,6 @@ public function __invoke( ->map(DecodeCookie::of()) ->map(DecodeQuery::of()) ->map(DecodeForm::of()) - ->map($injectEnv) ->map(static function($request) use ($os, $handle) { try { return $handle($request, $os); @@ -195,17 +176,15 @@ public function __invoke( } /** - * @param callable(ServerRequest, OperatingSystem): Response $handle + * @param \Closure(ServerRequest, OperatingSystem): Response $handle */ public static function of( Clock $clock, Open $open, - InjectEnvironment $injectEnv, - callable $handle, + \Closure $handle, ): self { return new self( $open, - $injectEnv, new Encode($clock), $handle, Display::of(), @@ -219,7 +198,6 @@ public function withOutput(Output $output): self { return new self( $this->open, - $this->injectEnv, $this->encode, $this->handle, $this->display->with($output),