Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
* develop:
  specify next release
  allow to use enums as a service name
  • Loading branch information
Baptouuuu committed Mar 24, 2024
2 parents cd84504 + 2ffcb0c commit c36a638
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 20 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## 2.2.0 - 2024-03-24

### Added

- Support for using enums as a service name

## 2.1.0 - 2024-03-10

### Added
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"innmind/operating-system": "~4.1|~5.0",
"innmind/cli": "^3.1",
"innmind/immutable": "~5.2",
"innmind/di": "^2.0",
"innmind/di": "~2.1",
"ramsey/uuid": "^4.7",
"innmind/url": "^4.1",
"innmind/filesystem": "~7.0",
Expand Down
63 changes: 53 additions & 10 deletions docs/services.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,54 @@

For both [HTTP](http.md) and [CLI](cli.md) applications a service is an object referenced by a name in a [`Container`](https://github.com/Innmind/DI).

> [!NOTE]
> since a container only deals with objects Psalm will complain of type mismatches, so you'll have to suppress those errors (for now).
## Defining a service

```php
use Innmind\DI\Service;
use Innmind\AMQP\Client;

/**
* @template S of object
* @implements Service<S>
*/
enum Services implements Service
{
case amqpClient;
case producerClient;
case consumerClient;

/**
* @return self<Client>
*/
public static function amqpClient(): self
{
/** @var self<Client> */
return self::amqpClient;
}

/**
* @return self<Client>
*/
public static function producerClient(): self
{
/** @var self<Client> */
return self::producerClient;
}

/**
* @return self<Client>
*/
public static function consumerClient(): self
{
/** @var self<Client> */
return self::consumerClient;
}
}
```

> [!TIP]
> If you publish a package you can add an `@internal` flag on the static methods to tell your users to not use the service. And when you plan to remove a service you can use the `@deprecated` flag.
```php
use Innmind\Framework\{
Main\Http,
Expand All @@ -23,7 +66,7 @@ new class extends Http|Cli {
protected function configure(Application $app): Application
{
return $app->service(
'amqp-client',
Services::amqpClient,
static fn($_, OperatingSystem $os) => Factory::of($os)->make(
Transport::tcp(),
Url::of('amqp://guest:guest@localhost:5672/'),
Expand All @@ -34,7 +77,7 @@ new class extends Http|Cli {
};
```

This example defines a single service named `amqp-client` that relies on the `OperatingSystem` in order to work.
This example defines a single service named `amqpClient` that relies on the `OperatingSystem` in order to work.

> [!NOTE]
> this example uses [`innmind/amqp`](https://github.com/innmind/amqp)
Expand All @@ -60,7 +103,7 @@ new class extends Http|Cli {
protected function configure(Application $app): Application
{
return $app->service(
'amqp-client',
Services::amqpClient,
static fn($_, OperatingSystem $os, Environment $env) => Factory::of($os)->make(
Transport::tcp(),
Url::of($env->get('AMQP_URL')), // this will throw if the variable is not defined
Expand Down Expand Up @@ -98,17 +141,17 @@ new class extends Http|Cli {
{
return $app
->service(
'producer-client',
Services::producerClient,
static fn($_, OperatingSystem $os) => Factory::of($os)->make(/* like above */),
)
->service(
'consumer-client',
static fn(Container $container) => $container('producer-client')->with(
Services::consumerClient,
static fn(Container $container) => $container(Services::producerClient)->with(
Qos::of(10), // prefetch 10 messages
),
);
}
};
```

Now every other service that relies on `consumer-client` will always have a configuration to prefetch 10 messages.
Now every other service that relies on `consumerClient` will always have a configuration to prefetch 10 messages.
9 changes: 6 additions & 3 deletions src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@
Environment as CliEnv,
Command,
};
use Innmind\DI\Container;
use Innmind\DI\{
Container,
Service,
};
use Innmind\Http\{
ServerRequest,
Response,
Expand Down Expand Up @@ -107,12 +110,12 @@ public function map(Middleware $map): self
/**
* @psalm-mutation-free
*
* @param non-empty-string $name
* @param non-empty-string|Service $name
* @param callable(Container, OperatingSystem, Environment): object $definition
*
* @return self<I, O>
*/
public function service(string $name, callable $definition): self
public function service(string|Service $name, callable $definition): self
{
return new self($this->app->service($name, $definition));
}
Expand Down
3 changes: 2 additions & 1 deletion src/Application/Async/Http.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Innmind\DI\{
Container,
Builder,
Service,
};
use Innmind\Http\{
ServerRequest,
Expand Down Expand Up @@ -137,7 +138,7 @@ function(OperatingSystem $os, Environment $env) use ($map): array {
/**
* @psalm-mutation-free
*/
public function service(string $name, callable $definition): self
public function service(string|Service $name, callable $definition): self
{
return new self(
$this->os,
Expand Down
3 changes: 2 additions & 1 deletion src/Application/Cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Innmind\DI\{
Builder,
Container,
Service,
};
use Innmind\Immutable\{
Sequence,
Expand Down Expand Up @@ -106,7 +107,7 @@ public function mapOperatingSystem(callable $map): self
/**
* @psalm-mutation-free
*/
public function service(string $name, callable $definition): self
public function service(string|Service $name, callable $definition): self
{
return new self(
$this->os,
Expand Down
3 changes: 2 additions & 1 deletion src/Application/Http.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Innmind\DI\{
Container,
Builder,
Service,
};
use Innmind\Http\{
ServerRequest,
Expand Down Expand Up @@ -118,7 +119,7 @@ public function mapOperatingSystem(callable $map): self
/**
* @psalm-mutation-free
*/
public function service(string $name, callable $definition): self
public function service(string|Service $name, callable $definition): self
{
return new self(
$this->os,
Expand Down
9 changes: 6 additions & 3 deletions src/Application/Implementation.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@
Environment as CliEnv,
Command,
};
use Innmind\DI\Container;
use Innmind\DI\{
Container,
Service,
};
use Innmind\Http\{
ServerRequest,
Response,
Expand Down Expand Up @@ -48,12 +51,12 @@ public function mapOperatingSystem(callable $map): self;
/**
* @psalm-mutation-free
*
* @param non-empty-string $name
* @param non-empty-string|Service $name
* @param callable(Container, OperatingSystem, Environment): object $definition
*
* @return self<I, O>
*/
public function service(string $name, callable $definition): self;
public function service(string|Service $name, callable $definition): self;

/**
* @psalm-mutation-free
Expand Down

0 comments on commit c36a638

Please sign in to comment.