Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to use Service references everywhere #2

Merged
merged 1 commit into from
Aug 1, 2024
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## [Unreleased]

### Added

- `Innmind\DI\Service` can now be used everywhere a service can be referenced

### Fixed

- `Innmind\Framework\Http\To` no longer raise Psalm errors when used as argument to `Application::route()`
Expand Down
9 changes: 6 additions & 3 deletions src/Cli/Command/Defer.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@
Command,
Console,
};
use Innmind\DI\Container;
use Innmind\DI\{
Container,
Service,
};

/**
* @internal
*/
final class Defer implements Command
{
private string $service;
private string|Service $service;
private Container $locate;
/** @var callable(Command): Command */
private $map;
Expand All @@ -24,7 +27,7 @@ final class Defer implements Command
* @param callable(Command): Command $map
*/
public function __construct(
string $service,
string|Service $service,
Container $locate,
callable $map,
) {
Expand Down
11 changes: 7 additions & 4 deletions src/Http/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,18 @@
ServerRequest,
Response,
};
use Innmind\DI\Container;
use Innmind\DI\{
Container,
Service as Ref,
};
use Innmind\Router\Route\Variables;

final class Service
{
private Container $container;
private string $service;
private string|Ref $service;

private function __construct(Container $container, string $service)
private function __construct(Container $container, string|Ref $service)
{
$this->container = $container;
$this->service = $service;
Expand All @@ -30,7 +33,7 @@ public function __invoke(ServerRequest $request, Variables $variables): Response
return ($this->container)($this->service)($request, $variables);
}

public static function of(Container $container, string $service): self
public static function of(Container $container, string|Ref $service): self
{
return new self($container, $service);
}
Expand Down
11 changes: 7 additions & 4 deletions src/Http/To.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,18 @@
ServerRequest,
Response,
};
use Innmind\DI\Container;
use Innmind\DI\{
Container,
Service,
};
use Innmind\OperatingSystem\OperatingSystem;
use Innmind\Router\Route\Variables;

final class To
{
private string $service;
private string|Service $service;

private function __construct(string $service)
private function __construct(string|Service $service)
{
$this->service = $service;
}
Expand All @@ -35,7 +38,7 @@ public function __invoke(
return $container($this->service)($request, $variables);
}

public static function service(string $service): self
public static function service(string|Service $service): self
{
return new self($service);
}
Expand Down
Loading