Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<Response>`
- `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

Expand Down
8 changes: 4 additions & 4 deletions src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<I, O>
*/
Expand All @@ -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<I, O>
*/
Expand All @@ -146,7 +146,7 @@ public function mapCommand(callable $map): self
/**
* @psalm-mutation-free
*
* @param Http\Route\Reference|callable(Pipe, Container, OperatingSystem, Environment): Component<SideEffect, Response> $handle
* @param Http\Route\Reference|callable(Pipe, Container): Component<SideEffect, Response> $handle
*
* @return self<I, O>
*/
Expand Down Expand Up @@ -192,7 +192,7 @@ public function mapRoute(callable $map): self
/**
* @psalm-mutation-free
*
* @param callable(ServerRequest, Container, OperatingSystem, Environment): Attempt<Response> $handle
* @param callable(ServerRequest, Container): Attempt<Response> $handle
*
* @return self<I, O>
*/
Expand Down
10 changes: 4 additions & 6 deletions src/Application/Async/Http.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<callable(Pipe, Container, OperatingSystem, Environment): Component<SideEffect, Response>> $routes
* @param Sequence<callable(Pipe, Container): Component<SideEffect, Response>> $routes
* @param \Closure(Component<SideEffect, Response>, Container): Component<SideEffect, Response> $mapRoute
* @param Maybe<callable(ServerRequest, Container, OperatingSystem, Environment): Attempt<Response>> $notFound
* @param Maybe<callable(ServerRequest, Container): Attempt<Response>> $notFound
* @param \Closure(ServerRequest, \Throwable, Container): Attempt<Response> $recover
*/
private function __construct(
Expand All @@ -67,7 +67,7 @@ private function __construct(
*/
public static function of(OperatingSystem $os): self
{
/** @var Maybe<callable(ServerRequest, Container, OperatingSystem, Environment): Attempt<Response>> */
/** @var Maybe<callable(ServerRequest, Container): Attempt<Response>> */
$notFound = Maybe::nothing();

return new self(
Expand Down Expand Up @@ -271,16 +271,14 @@ 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,
$notFound->map(
static fn($handle) => static fn(ServerRequest $request) => $handle(
$request,
$container,
$os,
$env,
),
),
static fn($request, $e) => $recover($request, $e, $container),
Expand Down
14 changes: 3 additions & 11 deletions src/Application/Cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ final class Cli implements Implementation
* @psalm-mutation-free
*
* @param \Closure(OperatingSystem, Environment): Builder $container
* @param Sequence<callable(Container, OperatingSystem, Environment): Command> $commands
* @param \Closure(Command, Container, OperatingSystem, Environment): Command $mapCommand
* @param Sequence<callable(Container): Command> $commands
* @param \Closure(Command, Container): Command $mapCommand
*/
private function __construct(
private OperatingSystem $os,
Expand Down Expand Up @@ -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,
),
);
}
Expand Down Expand Up @@ -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,
));

Expand Down
12 changes: 4 additions & 8 deletions src/Application/Http.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ final class Http implements Implementation
* @psalm-mutation-free
*
* @param \Closure(OperatingSystem, Environment): Builder $container
* @param Sequence<callable(Pipe, Container, OperatingSystem, Environment): Component<SideEffect, Response>> $routes
* @param Sequence<callable(Pipe, Container): Component<SideEffect, Response>> $routes
* @param \Closure(Component<SideEffect, Response>, Container): Component<SideEffect, Response> $mapRoute
* @param Maybe<callable(ServerRequest, Container, OperatingSystem, Environment): Attempt<Response>> $notFound
* @param Maybe<callable(ServerRequest, Container): Attempt<Response>> $notFound
* @param \Closure(ServerRequest, \Throwable, Container): Attempt<Response> $recover
*/
private function __construct(
Expand All @@ -59,7 +59,7 @@ private function __construct(
*/
public static function of(OperatingSystem $os, Environment $env): self
{
/** @var Maybe<callable(ServerRequest, Container, OperatingSystem, Environment): Attempt<Response>> */
/** @var Maybe<callable(ServerRequest, Container): Attempt<Response>> */
$notFound = Maybe::nothing();

return new self(
Expand Down Expand Up @@ -230,23 +230,19 @@ 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,
$this->notFound->map(
static fn($handle) => static fn(ServerRequest $request) => $handle(
$request,
$container,
$os,
$env,
),
),
static fn($request, $e) => $recover($request, $e, $container),
Expand Down
8 changes: 4 additions & 4 deletions src/Application/Implementation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<I, O>
*/
Expand All @@ -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<I, O>
*/
Expand All @@ -81,7 +81,7 @@ public function mapCommand(callable $map): self;
/**
* @psalm-mutation-free
*
* @param callable(Pipe, Container, OperatingSystem, Environment): Component<SideEffect, Response> $handle
* @param callable(Pipe, Container): Component<SideEffect, Response> $handle
*
* @return self<I, O>
*/
Expand All @@ -99,7 +99,7 @@ public function mapRoute(callable $map): self;
/**
* @psalm-mutation-free
*
* @param callable(ServerRequest, Container, OperatingSystem, Environment): Attempt<Response> $handle
* @param callable(ServerRequest, Container): Attempt<Response> $handle
*
* @return self<I, O>
*/
Expand Down
8 changes: 2 additions & 6 deletions src/Cli/Command/Defer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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,
) {
}
Expand Down Expand Up @@ -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);
}
}
4 changes: 1 addition & 3 deletions src/Http/Route/Reference.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -19,7 +17,7 @@
interface Reference extends \UnitEnum
{
/**
* @return callable(Pipe, Container, OperatingSystem, Environment): Component<SideEffect, Response>
* @return callable(Pipe, Container): Component<SideEffect, Response>
*/
public function route(): callable;
}
9 changes: 6 additions & 3 deletions tests/ApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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,
) {
Expand Down Expand Up @@ -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,
) {
Expand Down Expand Up @@ -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,
) {
Expand Down