diff --git a/CHANGELOG.md b/CHANGELOG.md index 32ce4ab..d5b02d7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,7 @@ - `Innmind\Framework\Application::route()` first parameter must now be expressed via a component inside the callable - `Innminf\Framework\Application::notFoundRequestHandler()` callable must now return an `Innmind\Immutable\Attempt` - `Innminf\Framework\Application::notFoundRequestHandler()` has been renamed `::routeNotFound()` +- `Innmind\Framework\Application::mapCommand()` callable now longer has access to `OperatingSystem` and `Environment` (use services instead) ### Removed diff --git a/src/Application.php b/src/Application.php index 03b6290..3e9894e 100644 --- a/src/Application.php +++ b/src/Application.php @@ -122,7 +122,7 @@ public function service(Service $name, callable $definition): self /** * @psalm-mutation-free * - * @param callable(Container, OperatingSystem, Environment): Command $command + * @param callable(Container): Command $command * * @return self */ @@ -134,7 +134,7 @@ public function command(callable $command): self /** * @psalm-mutation-free * - * @param callable(Command, Container, OperatingSystem, Environment): Command $map + * @param callable(Command, Container): Command $map * * @return self */ @@ -146,7 +146,7 @@ public function mapCommand(callable $map): self /** * @psalm-mutation-free * - * @param Http\Route\Reference|callable(Pipe, Container, OperatingSystem, Environment): Component $handle + * @param Http\Route\Reference|callable(Pipe, Container): Component $handle * * @return self */ @@ -192,7 +192,7 @@ public function mapRoute(callable $map): self /** * @psalm-mutation-free * - * @param callable(ServerRequest, Container, OperatingSystem, Environment): Attempt $handle + * @param callable(ServerRequest, Container): Attempt $handle * * @return self */ diff --git a/src/Application/Async/Http.php b/src/Application/Async/Http.php index 1ee4f1c..9114d0f 100644 --- a/src/Application/Async/Http.php +++ b/src/Application/Async/Http.php @@ -46,9 +46,9 @@ final class Http implements Implementation * * @param \Closure(OperatingSystem, Environment): array{OperatingSystem, Environment} $map * @param \Closure(OperatingSystem, Environment): Builder $container - * @param Sequence> $routes + * @param Sequence> $routes * @param \Closure(Component, Container): Component $mapRoute - * @param Maybe> $notFound + * @param Maybe> $notFound * @param \Closure(ServerRequest, \Throwable, Container): Attempt $recover */ private function __construct( @@ -67,7 +67,7 @@ private function __construct( */ public static function of(OperatingSystem $os): self { - /** @var Maybe> */ + /** @var Maybe> */ $notFound = Maybe::nothing(); return new self( @@ -271,7 +271,7 @@ static function(ServerRequest $request, OperatingSystem $os) use ( $container = $container($os, $env)->build(); $pipe = Pipe::new(); $routes = $routes - ->map(static fn($handle) => $handle($pipe, $container, $os, $env)) + ->map(static fn($handle) => $handle($pipe, $container)) ->map(static fn($component) => $mapRoute($component, $container)); $router = new Router( $routes, @@ -279,8 +279,6 @@ static function(ServerRequest $request, OperatingSystem $os) use ( static fn($handle) => static fn(ServerRequest $request) => $handle( $request, $container, - $os, - $env, ), ), static fn($request, $e) => $recover($request, $e, $container), diff --git a/src/Application/Cli.php b/src/Application/Cli.php index 350ea25..100781a 100644 --- a/src/Application/Cli.php +++ b/src/Application/Cli.php @@ -34,8 +34,8 @@ final class Cli implements Implementation * @psalm-mutation-free * * @param \Closure(OperatingSystem, Environment): Builder $container - * @param Sequence $commands - * @param \Closure(Command, Container, OperatingSystem, Environment): Command $mapCommand + * @param Sequence $commands + * @param \Closure(Command, Container): Command $mapCommand */ private function __construct( private OperatingSystem $os, @@ -143,13 +143,9 @@ public function mapCommand(callable $map): self static fn( Command $command, Container $service, - OperatingSystem $os, - Environment $env, ) => $map( - $previous($command, $service, $os, $env), + $previous($command, $service), $service, - $os, - $env, ), ); } @@ -200,14 +196,10 @@ public function run($input) $mapCommand = static fn(Command $command): Command => $mapCommand( $command, $container, - $os, - $env, ); $commands = $this->commands->map(static fn($command) => new Defer( \Closure::fromCallable($command), $container, - $os, - $env, $mapCommand, )); diff --git a/src/Application/Http.php b/src/Application/Http.php index bbe3131..1338c2f 100644 --- a/src/Application/Http.php +++ b/src/Application/Http.php @@ -38,9 +38,9 @@ final class Http implements Implementation * @psalm-mutation-free * * @param \Closure(OperatingSystem, Environment): Builder $container - * @param Sequence> $routes + * @param Sequence> $routes * @param \Closure(Component, Container): Component $mapRoute - * @param Maybe> $notFound + * @param Maybe> $notFound * @param \Closure(ServerRequest, \Throwable, Container): Attempt $recover */ private function __construct( @@ -59,7 +59,7 @@ private function __construct( */ public static function of(OperatingSystem $os, Environment $env): self { - /** @var Maybe> */ + /** @var Maybe> */ $notFound = Maybe::nothing(); return new self( @@ -230,14 +230,12 @@ public function recoverRouteError(callable $recover): self public function run($input) { $container = ($this->container)($this->os, $this->env)->build(); - $os = $this->os; - $env = $this->env; $mapRoute = $this->mapRoute; $recover = $this->recover; $pipe = Pipe::new(); $routes = $this ->routes - ->map(static fn($handle) => $handle($pipe, $container, $os, $env)) + ->map(static fn($handle) => $handle($pipe, $container)) ->map(static fn($component) => $mapRoute($component, $container)); $router = new Router( $routes, @@ -245,8 +243,6 @@ public function run($input) static fn($handle) => static fn(ServerRequest $request) => $handle( $request, $container, - $os, - $env, ), ), static fn($request, $e) => $recover($request, $e, $container), diff --git a/src/Application/Implementation.php b/src/Application/Implementation.php index 0137121..5b25031 100644 --- a/src/Application/Implementation.php +++ b/src/Application/Implementation.php @@ -63,7 +63,7 @@ public function service(Service $name, callable $definition): self; /** * @psalm-mutation-free * - * @param callable(Container, OperatingSystem, Environment): Command $command + * @param callable(Container): Command $command * * @return self */ @@ -72,7 +72,7 @@ public function command(callable $command): self; /** * @psalm-mutation-free * - * @param callable(Command, Container, OperatingSystem, Environment): Command $map + * @param callable(Command, Container): Command $map * * @return self */ @@ -81,7 +81,7 @@ public function mapCommand(callable $map): self; /** * @psalm-mutation-free * - * @param callable(Pipe, Container, OperatingSystem, Environment): Component $handle + * @param callable(Pipe, Container): Component $handle * * @return self */ @@ -99,7 +99,7 @@ public function mapRoute(callable $map): self; /** * @psalm-mutation-free * - * @param callable(ServerRequest, Container, OperatingSystem, Environment): Attempt $handle + * @param callable(ServerRequest, Container): Attempt $handle * * @return self */ diff --git a/src/Cli/Command/Defer.php b/src/Cli/Command/Defer.php index 7d6418f..ca2203a 100644 --- a/src/Cli/Command/Defer.php +++ b/src/Cli/Command/Defer.php @@ -3,13 +3,11 @@ namespace Innmind\Framework\Cli\Command; -use Innmind\Framework\Environment; use Innmind\CLI\{ Command, Command\Usage, Console, }; -use Innmind\OperatingSystem\OperatingSystem; use Innmind\DI\Container; use Innmind\Immutable\Attempt; @@ -21,14 +19,12 @@ final class Defer implements Command private ?Command $command = null; /** - * @param \Closure(Container, OperatingSystem, Environment): Command $build + * @param \Closure(Container): Command $build * @param \Closure(Command): Command $map */ public function __construct( private \Closure $build, private Container $locate, - private OperatingSystem $os, - private Environment $env, private \Closure $map, ) { } @@ -61,6 +57,6 @@ private function command(): Command * @psalm-suppress PropertyTypeCoercion * @var Command */ - return $this->command ??= ($this->build)($this->locate, $this->os, $this->env); + return $this->command ??= ($this->build)($this->locate); } } diff --git a/src/Http/Route/Reference.php b/src/Http/Route/Reference.php index a25e4e3..9967289 100644 --- a/src/Http/Route/Reference.php +++ b/src/Http/Route/Reference.php @@ -3,8 +3,6 @@ namespace Innmind\Framework\Http\Route; -use Innmind\Framework\Environment; -use Innmind\OperatingSystem\OperatingSystem; use Innmind\DI\Container; use Innmind\Router\{ Component, @@ -19,7 +17,7 @@ interface Reference extends \UnitEnum { /** - * @return callable(Pipe, Container, OperatingSystem, Environment): Component + * @return callable(Pipe, Container): Component */ public function route(): callable; } diff --git a/tests/ApplicationTest.php b/tests/ApplicationTest.php index d273e9d..f787f82 100644 --- a/tests/ApplicationTest.php +++ b/tests/ApplicationTest.php @@ -633,6 +633,7 @@ public function testMiddleware(): BlackBox\Proof ) ->prove(function($inputs, $interactive, $variables) { $app = Application::cli(Factory::build(), Environment::test($variables)) + ->service(Services::service, static fn($_, $__, $env) => $env) ->map(new class implements Middleware { public function __invoke(Application $app): Application { @@ -663,7 +664,7 @@ public function __invoke(Application $app): Application }); } }) - ->command(static fn($_, $__, $env) => new class($env) implements Command { + ->command(static fn($get) => new class($get(Services::service)) implements Command { public function __construct( private $env, ) { @@ -722,9 +723,10 @@ public function __invoke(Application $app): Application }; $app = Application::cli(Factory::build(), Environment::test($variables)) + ->service(Services::service, static fn($_, $__, $env) => $env) ->map(Optional::of(Unknown::class, static fn() => throw new \Exception)) ->map(Optional::of($middleware::class, static fn() => $middleware)) - ->command(static fn($_, $__, $env) => new class($env) implements Command { + ->command(static fn($get) => new class($get(Services::service)) implements Command { public function __construct( private $env, ) { @@ -773,8 +775,9 @@ public function testLoadDotEnv(): BlackBox\Proof ) ->prove(function($inputs, $interactive, $variables) { $app = Application::cli(Factory::build(), Environment::test($variables)) + ->service(Services::service, static fn($_, $__, $env) => $env) ->map(LoadDotEnv::at(Path::of(__DIR__.'/../fixtures/'))) - ->command(static fn($_, $__, $env) => new class($env) implements Command { + ->command(static fn($get) => new class($get(Services::service)) implements Command { public function __construct( private $env, ) {