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
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,16 @@

## [Unreleased]

### Changed

- Requires `innmind/foundation:~1.9`
- Requires `innmind/di:~3.0`
- `Innmind\Framework\Application::route()` callable must now return a `Innmind\Router\Component`

### Removed

- The ability to use `string`s to reference services
- `Innmind\Framework\Http\Service`

### Fixed

Expand Down
15 changes: 5 additions & 10 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,9 @@
},
"require": {
"php": "~8.2",
"innmind/operating-system": "~4.1|~5.0",
"innmind/cli": "^3.1",
"innmind/immutable": "~5.2",
"innmind/di": "~2.1",
"innmind/url": "^4.1",
"innmind/filesystem": "~7.0",
"innmind/http-server": "~4.0",
"innmind/router": "~4.1"
"innmind/foundation": "~1.9",
"innmind/di": "~3.0",
"innmind/router": "^5.0.1"
},
"autoload": {
"psr-4": {
Expand All @@ -39,11 +34,11 @@
"innmind/static-analysis": "^1.2.1",
"innmind/black-box": "~6.5",
"innmind/coding-standard": "~2.0",
"innmind/async-http-server": "~2.0|~3.0"
"innmind/async-http-server": "~4.0"
},
"conflict": {
"innmind/black-box": "<6.0|~7.0",
"innmind/async-http-server": "<2.0|~4.0"
"innmind/async-http-server": "<3.0|~5.0"
},
"suggest": {
"innmind/black-box": "For property based testing",
Expand Down
13 changes: 11 additions & 2 deletions fixtures/server.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,23 @@
Main\Async\Http,
Http\Routes,
};
use Innmind\Router\Route;
use Innmind\Router\{
Method,
Endpoint,
Handle,
Respond,
};
use Innmind\Http\Response\StatusCode;
use Innmind\Immutable\Attempt;

new class extends Http
{
protected function configure(Application $app): Application
{
return $app->appendRoutes(static fn($routes) => $routes->add(
Route::literal('GET /hello'),
Method::get()
->pipe(Endpoint::of('/hello'))
->pipe(Respond::with(StatusCode::ok)),
));
}
};
16 changes: 10 additions & 6 deletions src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,19 @@
Container,
Service,
};
use Innmind\Router\Component;
use Innmind\Http\{
ServerRequest,
Response,
};
use Innmind\Router\Route\Variables;
use Innmind\Immutable\{
Attempt,
SideEffect,
};

/**
* @template I of ServerRequest|CliEnv
* @template O of Response|CliEnv
* @template O of Response|Attempt<CliEnv>
*/
final class Application
{
Expand All @@ -51,7 +55,7 @@ public static function http(OperatingSystem $os, Environment $env): self
/**
* @psalm-pure
*
* @return self<CliEnv, CliEnv>
* @return self<CliEnv, Attempt<CliEnv>>
*/
public static function cli(OperatingSystem $os, Environment $env): self
{
Expand All @@ -62,7 +66,7 @@ public static function cli(OperatingSystem $os, Environment $env): self
* @psalm-pure
* @experimental
*
* @return self<CliEnv, CliEnv>
* @return self<CliEnv, Attempt<CliEnv>>
*/
public static function asyncHttp(OperatingSystem $os): self
{
Expand Down Expand Up @@ -144,7 +148,7 @@ public function mapCommand(callable $map): self
* @psalm-mutation-free
*
* @param literal-string $pattern
* @param callable(ServerRequest, Variables, Container, OperatingSystem, Environment): Response $handle
* @param callable(Container, OperatingSystem, Environment): Component<SideEffect, Response> $handle
*
* @return self<I, O>
*/
Expand Down Expand Up @@ -194,7 +198,7 @@ public function notFoundRequestHandler(callable $handle): self
*
* @return O
*/
public function run(CliEnv|ServerRequest $input): CliEnv|Response
public function run(CliEnv|ServerRequest $input): Attempt|Response
{
return $this->app->run($input);
}
Expand Down
44 changes: 33 additions & 11 deletions src/Application/Async/Http.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,24 @@
Builder,
Service,
};
use Innmind\Router\{
Method,
Endpoint,
};
use Innmind\Http\{
ServerRequest,
Response,
};
use Innmind\Router\{
Route,
};
use Innmind\Immutable\{
Maybe,
Sequence,
Attempt,
};

/**
* @experimental
* @internal
* @implements Implementation<CliEnv, CliEnv>
* @implements Implementation<CliEnv, Attempt<CliEnv>>
*/
final class Http implements Implementation
{
Expand Down Expand Up @@ -168,15 +170,35 @@ public function mapCommand(callable $map): self
#[\Override]
public function route(string $pattern, callable $handle): self
{
/**
* @psalm-suppress PossiblyUndefinedArrayOffset Todo better typing
* @var literal-string $path
*/
[$method, $path] = \explode(' ', $pattern, 2);

$method = match ($method) {
'get', 'GET' => Method::get(),
'post', 'POST' => Method::post(),
'put', 'PUT' => Method::put(),
'patch', 'PATCH' => Method::patch(),
'delete', 'DELETE' => Method::delete(),
'options', 'OPTIONS' => Method::options(),
'trace', 'TRACE' => Method::trace(),
'connect', 'CONNECT' => Method::connect(),
'head', 'HEAD' => Method::head(),
'link', 'LINK' => Method::link(),
'unlink', 'UNLINK' => Method::unlink(),
};

/**
* @psalm-suppress MixedArgumentTypeCoercion
* @psalm-suppress InvalidArgument
*/
return $this->appendRoutes(
static fn($routes, $container, $os, $env) => $routes->add(
Route::literal($pattern)->handle(static fn($request, $variables) => $handle(
$request,
$variables,
$container,
$os,
$env,
)),
$method
->pipe(Endpoint::of($path))
->pipe($handle($container, $os, $env)),
),
);
}
Expand Down
3 changes: 2 additions & 1 deletion src/Application/Cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@
use Innmind\Immutable\{
Sequence,
Str,
Attempt,
};

/**
* @internal
* @implements Implementation<CliEnv, CliEnv>
* @implements Implementation<CliEnv, Attempt<CliEnv>>
*/
final class Cli implements Implementation
{
Expand Down
39 changes: 31 additions & 8 deletions src/Application/Http.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@
Builder,
Service,
};
use Innmind\Router\{
Method,
Endpoint,
};
use Innmind\Http\{
ServerRequest,
Response,
};
use Innmind\Router\Route;
use Innmind\Immutable\{
Maybe,
Sequence,
Expand Down Expand Up @@ -146,15 +149,35 @@ public function mapCommand(callable $map): self
#[\Override]
public function route(string $pattern, callable $handle): self
{
/**
* @psalm-suppress PossiblyUndefinedArrayOffset Todo better typing
* @var literal-string $path
*/
[$method, $path] = \explode(' ', $pattern, 2);

$method = match ($method) {
'get', 'GET' => Method::get(),
'post', 'POST' => Method::post(),
'put', 'PUT' => Method::put(),
'patch', 'PATCH' => Method::patch(),
'delete', 'DELETE' => Method::delete(),
'options', 'OPTIONS' => Method::options(),
'trace', 'TRACE' => Method::trace(),
'connect', 'CONNECT' => Method::connect(),
'head', 'HEAD' => Method::head(),
'link', 'LINK' => Method::link(),
'unlink', 'UNLINK' => Method::unlink(),
};

/**
* @psalm-suppress MixedArgumentTypeCoercion
* @psalm-suppress InvalidArgument
*/
return $this->appendRoutes(
static fn($routes, $container, $os, $env) => $routes->add(
Route::literal($pattern)->handle(static fn($request, $variables) => $handle(
$request,
$variables,
$container,
$os,
$env,
)),
$method
->pipe(Endpoint::of($path))
->pipe($handle($container, $os, $env)),
),
);
}
Expand Down
10 changes: 7 additions & 3 deletions src/Application/Implementation.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,20 @@
Container,
Service,
};
use Innmind\Router\Component;
use Innmind\Http\{
ServerRequest,
Response,
};
use Innmind\Router\Route\Variables;
use Innmind\Immutable\{
Attempt,
SideEffect,
};

/**
* @internal
* @template I of ServerRequest|CliEnv
* @template O of Response|CliEnv
* @template O of Response|Attempt<CliEnv>
*/
interface Implementation
{
Expand Down Expand Up @@ -79,7 +83,7 @@ public function mapCommand(callable $map): self;
* @psalm-mutation-free
*
* @param literal-string $pattern
* @param callable(ServerRequest, Variables, Container, OperatingSystem, Environment): Response $handle
* @param callable(Container, OperatingSystem, Environment): Component<SideEffect, Response> $handle
*
* @return self<I, O>
*/
Expand Down
6 changes: 4 additions & 2 deletions src/Cli/Command/Defer.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
use Innmind\Framework\Environment;
use Innmind\CLI\{
Command,
Command\Usage,
Console,
};
use Innmind\OperatingSystem\OperatingSystem;
use Innmind\DI\Container;
use Innmind\Immutable\Attempt;

/**
* @internal
Expand All @@ -32,7 +34,7 @@ public function __construct(
}

#[\Override]
public function __invoke(Console $console): Console
public function __invoke(Console $console): Attempt
{
// we map the command when running it instead of when loading it to
// avoid loading the decorator multiple times for a same script as
Expand All @@ -47,7 +49,7 @@ public function __invoke(Console $console): Console
* @psalm-mutation-free
*/
#[\Override]
public function usage(): string
public function usage(): Usage
{
/** @psalm-suppress ImpureMethodCall */
return $this->command()->usage();
Expand Down
Loading
Loading