Skip to content

Commit c36a638

Browse files
committed
Merge branch 'develop'
* develop: specify next release allow to use enums as a service name
2 parents cd84504 + 2ffcb0c commit c36a638

8 files changed

Lines changed: 78 additions & 20 deletions

File tree

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## 2.2.0 - 2024-03-24
4+
5+
### Added
6+
7+
- Support for using enums as a service name
8+
39
## 2.1.0 - 2024-03-10
410

511
### Added

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"innmind/operating-system": "~4.1|~5.0",
2020
"innmind/cli": "^3.1",
2121
"innmind/immutable": "~5.2",
22-
"innmind/di": "^2.0",
22+
"innmind/di": "~2.1",
2323
"ramsey/uuid": "^4.7",
2424
"innmind/url": "^4.1",
2525
"innmind/filesystem": "~7.0",

docs/services.md

Lines changed: 53 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,54 @@
22

33
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).
44

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

7+
```php
8+
use Innmind\DI\Service;
9+
use Innmind\AMQP\Client;
10+
11+
/**
12+
* @template S of object
13+
* @implements Service<S>
14+
*/
15+
enum Services implements Service
16+
{
17+
case amqpClient;
18+
case producerClient;
19+
case consumerClient;
20+
21+
/**
22+
* @return self<Client>
23+
*/
24+
public static function amqpClient(): self
25+
{
26+
/** @var self<Client> */
27+
return self::amqpClient;
28+
}
29+
30+
/**
31+
* @return self<Client>
32+
*/
33+
public static function producerClient(): self
34+
{
35+
/** @var self<Client> */
36+
return self::producerClient;
37+
}
38+
39+
/**
40+
* @return self<Client>
41+
*/
42+
public static function consumerClient(): self
43+
{
44+
/** @var self<Client> */
45+
return self::consumerClient;
46+
}
47+
}
48+
```
49+
50+
> [!TIP]
51+
> 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.
52+
1053
```php
1154
use Innmind\Framework\{
1255
Main\Http,
@@ -23,7 +66,7 @@ new class extends Http|Cli {
2366
protected function configure(Application $app): Application
2467
{
2568
return $app->service(
26-
'amqp-client',
69+
Services::amqpClient,
2770
static fn($_, OperatingSystem $os) => Factory::of($os)->make(
2871
Transport::tcp(),
2972
Url::of('amqp://guest:guest@localhost:5672/'),
@@ -34,7 +77,7 @@ new class extends Http|Cli {
3477
};
3578
```
3679

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

3982
> [!NOTE]
4083
> this example uses [`innmind/amqp`](https://github.com/innmind/amqp)
@@ -60,7 +103,7 @@ new class extends Http|Cli {
60103
protected function configure(Application $app): Application
61104
{
62105
return $app->service(
63-
'amqp-client',
106+
Services::amqpClient,
64107
static fn($_, OperatingSystem $os, Environment $env) => Factory::of($os)->make(
65108
Transport::tcp(),
66109
Url::of($env->get('AMQP_URL')), // this will throw if the variable is not defined
@@ -98,17 +141,17 @@ new class extends Http|Cli {
98141
{
99142
return $app
100143
->service(
101-
'producer-client',
144+
Services::producerClient,
102145
static fn($_, OperatingSystem $os) => Factory::of($os)->make(/* like above */),
103146
)
104147
->service(
105-
'consumer-client',
106-
static fn(Container $container) => $container('producer-client')->with(
148+
Services::consumerClient,
149+
static fn(Container $container) => $container(Services::producerClient)->with(
107150
Qos::of(10), // prefetch 10 messages
108151
),
109152
);
110153
}
111154
};
112155
```
113156

114-
Now every other service that relies on `consumer-client` will always have a configuration to prefetch 10 messages.
157+
Now every other service that relies on `consumerClient` will always have a configuration to prefetch 10 messages.

src/Application.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@
1212
Environment as CliEnv,
1313
Command,
1414
};
15-
use Innmind\DI\Container;
15+
use Innmind\DI\{
16+
Container,
17+
Service,
18+
};
1619
use Innmind\Http\{
1720
ServerRequest,
1821
Response,
@@ -107,12 +110,12 @@ public function map(Middleware $map): self
107110
/**
108111
* @psalm-mutation-free
109112
*
110-
* @param non-empty-string $name
113+
* @param non-empty-string|Service $name
111114
* @param callable(Container, OperatingSystem, Environment): object $definition
112115
*
113116
* @return self<I, O>
114117
*/
115-
public function service(string $name, callable $definition): self
118+
public function service(string|Service $name, callable $definition): self
116119
{
117120
return new self($this->app->service($name, $definition));
118121
}

src/Application/Async/Http.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Innmind\DI\{
2020
Container,
2121
Builder,
22+
Service,
2223
};
2324
use Innmind\Http\{
2425
ServerRequest,
@@ -137,7 +138,7 @@ function(OperatingSystem $os, Environment $env) use ($map): array {
137138
/**
138139
* @psalm-mutation-free
139140
*/
140-
public function service(string $name, callable $definition): self
141+
public function service(string|Service $name, callable $definition): self
141142
{
142143
return new self(
143144
$this->os,

src/Application/Cli.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Innmind\DI\{
1717
Builder,
1818
Container,
19+
Service,
1920
};
2021
use Innmind\Immutable\{
2122
Sequence,
@@ -106,7 +107,7 @@ public function mapOperatingSystem(callable $map): self
106107
/**
107108
* @psalm-mutation-free
108109
*/
109-
public function service(string $name, callable $definition): self
110+
public function service(string|Service $name, callable $definition): self
110111
{
111112
return new self(
112113
$this->os,

src/Application/Http.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Innmind\DI\{
1414
Container,
1515
Builder,
16+
Service,
1617
};
1718
use Innmind\Http\{
1819
ServerRequest,
@@ -118,7 +119,7 @@ public function mapOperatingSystem(callable $map): self
118119
/**
119120
* @psalm-mutation-free
120121
*/
121-
public function service(string $name, callable $definition): self
122+
public function service(string|Service $name, callable $definition): self
122123
{
123124
return new self(
124125
$this->os,

src/Application/Implementation.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@
1313
Environment as CliEnv,
1414
Command,
1515
};
16-
use Innmind\DI\Container;
16+
use Innmind\DI\{
17+
Container,
18+
Service,
19+
};
1720
use Innmind\Http\{
1821
ServerRequest,
1922
Response,
@@ -48,12 +51,12 @@ public function mapOperatingSystem(callable $map): self;
4851
/**
4952
* @psalm-mutation-free
5053
*
51-
* @param non-empty-string $name
54+
* @param non-empty-string|Service $name
5255
* @param callable(Container, OperatingSystem, Environment): object $definition
5356
*
5457
* @return self<I, O>
5558
*/
56-
public function service(string $name, callable $definition): self;
59+
public function service(string|Service $name, callable $definition): self;
5760

5861
/**
5962
* @psalm-mutation-free

0 commit comments

Comments
 (0)