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 @@ -21,6 +21,7 @@
- `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)
- `Innmind\Framework\Application::run()` always return an `Innmind\Immutable\Attempt`

### Removed

Expand Down
10 changes: 5 additions & 5 deletions src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

/**
* @template I of ServerRequest|CliEnv
* @template O of Response|Attempt<CliEnv>
* @template O of Response|CliEnv
*/
final class Application
{
Expand Down Expand Up @@ -55,7 +55,7 @@ public static function http(OperatingSystem $os, Environment $env): self
/**
* @psalm-pure
*
* @return self<CliEnv, Attempt<CliEnv>>
* @return self<CliEnv, CliEnv>
*/
#[\NoDiscard]
public static function cli(OperatingSystem $os, Environment $env): self
Expand All @@ -67,7 +67,7 @@ public static function cli(OperatingSystem $os, Environment $env): self
* @psalm-pure
* @experimental
*
* @return self<CliEnv, Attempt<CliEnv>>
* @return self<CliEnv, CliEnv>
*/
#[\NoDiscard]
public static function asyncHttp(OperatingSystem $os): self
Expand Down Expand Up @@ -230,10 +230,10 @@ public function recoverRouteError(callable $recover): self
/**
* @param I $input
*
* @return O
* @return Attempt<O>
*/
#[\NoDiscard]
public function run(CliEnv|ServerRequest $input): Attempt|Response
public function run(CliEnv|ServerRequest $input): Attempt
{
return $this->app->run($input);
}
Expand Down
6 changes: 3 additions & 3 deletions src/Application/Async/Http.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
/**
* @experimental
* @internal
* @implements Implementation<CliEnv, Attempt<CliEnv>>
* @implements Implementation<CliEnv, CliEnv>
*/
final class Http implements Implementation
{
Expand Down Expand Up @@ -247,7 +247,7 @@ public function recoverRouteError(callable $recover): self
}

#[\Override]
public function run($input)
public function run($input): Attempt
{
$map = $this->map;
$container = $this->container;
Expand Down Expand Up @@ -284,7 +284,7 @@ static function(ServerRequest $request, OperatingSystem $os) use (
static fn($request, $e) => $recover($request, $e, $container),
);

return $router($request);
return $router($request)->unwrap();
},
));

Expand Down
4 changes: 2 additions & 2 deletions src/Application/Cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

/**
* @internal
* @implements Implementation<CliEnv, Attempt<CliEnv>>
* @implements Implementation<CliEnv, CliEnv>
*/
final class Cli implements Implementation
{
Expand Down Expand Up @@ -197,7 +197,7 @@ public function recoverRouteError(callable $recover): self
}

#[\Override]
public function run($input)
public function run($input): Attempt
{
$container = ($this->container)($this->os, $this->env)->build();
$mapCommand = $this->mapCommand;
Expand Down
2 changes: 1 addition & 1 deletion src/Application/Http.php
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ public function recoverRouteError(callable $recover): self
}

#[\Override]
public function run($input)
public function run($input): Attempt
{
$container = ($this->container)($this->os, $this->env)->build();
$mapRoute = $this->mapRoute;
Expand Down
6 changes: 3 additions & 3 deletions src/Application/Implementation.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
/**
* @internal
* @template I of ServerRequest|CliEnv
* @template O of Response|Attempt<CliEnv>
* @template O of Response|CliEnv
*/
interface Implementation
{
Expand Down Expand Up @@ -117,7 +117,7 @@ public function recoverRouteError(callable $recover): self;
/**
* @param I $input
*
* @return O
* @return Attempt<O>
*/
public function run($input);
public function run($input): Attempt;
}
7 changes: 5 additions & 2 deletions src/Http/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ public function __construct(
) {
}

public function __invoke(ServerRequest $request): Response
/**
* @return Attempt<Response>
*/
public function __invoke(ServerRequest $request): Attempt
{
$recover = $this->recover;

Expand All @@ -63,6 +66,6 @@ public function __invoke(ServerRequest $request): Response
)),
);

return $route($request)->unwrap();
return $route($request);
}
}
8 changes: 2 additions & 6 deletions src/Main/Async/Http.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,13 @@ abstract class Http extends Main
#[\Override]
protected function main(Environment $env, OperatingSystem $os): Attempt
{
/**
* @psalm-suppress InvalidReturnStatement Let the app crash in case of a misuse
* @var Attempt<Environment>
*/
return static::configure(Application::asyncHttp($os))->run($env);
}

/**
* @param Application<Environment, Attempt<Environment>> $app
* @param Application<Environment, Environment> $app
*
* @return Application<Environment, Attempt<Environment>>
* @return Application<Environment, Environment>
*/
abstract protected function configure(Application $app): Application;
}
8 changes: 2 additions & 6 deletions src/Main/Cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,13 @@ abstract class Cli extends Main
#[\Override]
protected function main(Environment $env, OperatingSystem $os): Attempt
{
/**
* @psalm-suppress InvalidReturnStatement Let the app crash in case of a misuse
* @var Attempt<Environment>
*/
return static::configure(Application::cli($os, AppEnv::of($env->variables())))->run($env);
}

/**
* @param Application<Environment, Attempt<Environment>> $app
* @param Application<Environment, Environment> $app
*
* @return Application<Environment, Attempt<Environment>>
* @return Application<Environment, Environment>
*/
abstract protected function configure(Application $app): Application;
}
7 changes: 2 additions & 5 deletions src/Main/Http.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

abstract class Http extends Main
{
/** @var Application<ServerRequest, Response> */
private Application $app;

#[\Override]
Expand All @@ -28,11 +29,7 @@ protected function preload(OperatingSystem $os, Environment $env): void
#[\Override]
protected function main(ServerRequest $request): Response
{
/**
* @psalm-suppress InvalidReturnStatement Let the app crash in case of a misuse
* @var Response
*/
return $this->app->run($request);
return $this->app->run($request)->unwrap();
}

/**
Expand Down
3 changes: 1 addition & 2 deletions src/Middleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@
Response,
};
use Innmind\CLI\Environment as CliEnv;
use Innmind\Immutable\Attempt;

interface Middleware
{
/**
* @template I of ServerRequest|CliEnv
* @template O of Response|Attempt<CliEnv>
* @template O of Response|CliEnv
*
* @param Application<I, O> $app
*
Expand Down
26 changes: 13 additions & 13 deletions tests/ApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -912,7 +912,7 @@ public function testHttpApplicationReturnsNotFoundByDefault(): BlackBox\Proof
$url,
$method,
$protocol,
));
))->unwrap();

$this->assertSame(StatusCode::notFound, $response->statusCode());
$this->assertSame($protocol, $response->protocolVersion());
Expand Down Expand Up @@ -962,15 +962,15 @@ public function testMatchRoutes(): BlackBox\Proof
Url::of('/foo'),
Method::get,
$protocol,
));
))->unwrap();

$this->assertSame($responseA, $response);

$response = $app->run(ServerRequest::of(
Url::of('/bar'),
Method::get,
$protocol,
));
))->unwrap();

$this->assertSame($responseB, $response);
});
Expand Down Expand Up @@ -1019,15 +1019,15 @@ public function testRouteShortDeclaration(): BlackBox\Proof
Url::of('/foo'),
Method::get,
$protocol,
));
))->unwrap();

$this->assertSame($responseA, $response);

$response = $app->run(ServerRequest::of(
Url::of('/bar'),
Method::post,
$protocol,
));
))->unwrap();

$this->assertSame($responseB, $response);
});
Expand Down Expand Up @@ -1071,7 +1071,7 @@ public function __invoke()
Url::of('/foo'),
Method::get,
$protocol,
));
))->unwrap();

$this->assertSame($expected, $response);
});
Expand Down Expand Up @@ -1114,7 +1114,7 @@ public function __invoke()
Url::of('/foo'),
$method,
$protocol,
));
))->unwrap();

$this->assertSame($expected, $response);
});
Expand Down Expand Up @@ -1149,7 +1149,7 @@ public function testAllowToSpecifyHttpNotFoundRequestHandler(): BlackBox\Proof
$url,
$method,
$protocol,
));
))->unwrap();

$this->assertSame($expected, $response);
});
Expand Down Expand Up @@ -1189,7 +1189,7 @@ public function testMatchMethodAllowed(): BlackBox\Proof
Url::of('/foo'),
Method::head,
$protocol,
));
))->unwrap();

$this->assertSame(405, $response->statusCode()->toInt());
$this->assertSame($protocol, $response->protocolVersion());
Expand Down Expand Up @@ -1240,7 +1240,7 @@ public function __invoke()
Url::of('/foo'),
Method::get,
$protocol,
));
))->unwrap();

$this->assertSame($expected, $response);
});
Expand Down Expand Up @@ -1272,15 +1272,15 @@ public function testRoutesAsEnumCases(): BlackBox\Proof
Url::of('/foo'),
Method::get,
$protocol,
));
))->unwrap();

$this->assertSame($responseA, $response);

$response = $app->run(ServerRequest::of(
Url::of('/bar'),
Method::get,
$protocol,
));
))->unwrap();

$this->assertSame($responseB, $response);
});
Expand Down Expand Up @@ -1311,7 +1311,7 @@ public function testRecoverRouteError(): BlackBox\Proof
Url::of('/foo'),
Method::get,
$protocol,
));
))->unwrap();

$this->assertSame($expected, $response);
});
Expand Down