Skip to content

Commit 1568cb2

Browse files
authored
Merge pull request #28 from Dhii/feature/service-tagging
Add Service Tagging Capability
2 parents f1d1470 + a186797 commit 1568cb2

File tree

14 files changed

+622
-262
lines changed

14 files changed

+622
-262
lines changed

.github/workflows/ci.yml

Lines changed: 39 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,43 @@
11
name: Continuous Integration
22

3-
on: [push]
3+
on:
4+
- push
45

56
jobs:
6-
run:
7-
runs-on: ubuntu-latest
8-
strategy:
9-
matrix:
10-
php-versions:
11-
- '7.4'
12-
- '8.0'
13-
- '8.1'
14-
- '8.2'
15-
name: PHP ${{ matrix.php-versions }}
16-
steps:
17-
- uses: actions/checkout@v2
18-
19-
- name: Setup PHP
20-
uses: shivammathur/setup-php@v2
21-
with:
22-
php-version: ${{ matrix.php-versions }}
23-
coverage: none
24-
tools: composer:v1
25-
26-
- name: Validate composer.json and composer.lock
27-
run: composer validate
28-
29-
- name: Install dependencies
30-
run: composer update --prefer-dist --no-progress --no-suggest
31-
32-
- name: Run PHPUnit
33-
run: vendor/bin/phpunit
34-
35-
- name: Run PHPCS
36-
run: ./vendor/bin/phpcs -s --runtime-set ignore_warnings_on_exit 1
37-
38-
- name: Run Psalm
39-
run: vendor/bin/psalm
7+
run:
8+
runs-on: ubuntu-latest
9+
strategy:
10+
matrix:
11+
php-versions:
12+
- '7.4'
13+
- '8.0'
14+
- '8.1'
15+
- '8.2'
16+
- '8.3'
17+
name: PHP ${{ matrix.php-versions }}
18+
steps:
19+
- uses: actions/checkout@v2
20+
21+
- name: Setup PHP
22+
uses: shivammathur/setup-php@v2
23+
with:
24+
php-version: ${{ matrix.php-versions }}
25+
coverage: none
26+
27+
- name: Validate composer.json and composer.lock
28+
run: composer validate
29+
30+
- name: Install dependencies
31+
uses: ramsey/composer-install@v3
32+
with:
33+
dependency-versions: highest
34+
composer-options: "--prefer-dist"
35+
36+
- name: Run PHPUnit
37+
run: vendor/bin/phpunit
38+
39+
- name: Run PHPCS
40+
run: ./vendor/bin/phpcs -s --runtime-set ignore_warnings_on_exit 1
41+
42+
- name: Run Psalm
43+
run: vendor/bin/psalm

.idea/codeception.xml

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/php.xml

Lines changed: 14 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/phpspec.xml

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
99
- Missing return types causing warnings with PHP 8.1 and higher.
1010
- Static check issues.
1111

12+
### Added
13+
- Service tagging capability (#28).
14+
1215
## [0.1.4] -2021-10-06
1316
Stable release.
1417

README.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ A selection of [PSR-11][] containers for utility, simplicity, and ease.
2727
### DI
2828
- [`ServiceProvider`][] - A super-simple implementation that allows quick creation of [service providers][Service Provider] from known maps of factories and extensions.
2929
- [`CompositeCachingServiceProvider`][] - A service provider that aggregates factories and extensions of other service providers. The results of this aggregation will be cached, meaing that it is only performed at most once per instance - when retrieving said factories or extensions.
30+
- [`TaggingServiceProvider`][] - A service provider that aggregates tagged services into a service with the tag's name.
3031
- [`DelegatingContainer`][] - A container that will invoke the factories and extensions of its configured service provider before returning values. If a parent container is specified, it will be passed to the service definitions instead of this container. This allows [dependency lookup delegation][DDL], which is especially useful when composing a container out of other containers.
3132

3233
## Examples
@@ -56,6 +57,43 @@ Most modern applications use some kind of DI container setup. The below example
5657
$appContainer->get('my-service');
5758
```
5859

60+
### Service Tagging
61+
You can tag your services into a collection. This adds a service with the same name as the tag,
62+
which will return a list of services tagged with it.
63+
64+
Since a service name can theoretically be any legal string,
65+
while some limitations need to be set for it to remain a tag,
66+
the tag name can contain any character besides whitespace (anything that matches `\s`).
67+
68+
```php
69+
[
70+
'serviceA' =>
71+
/** @tag letters */
72+
fn (): string => 'A',
73+
'serviceB' =>
74+
/**
75+
* @tag letters
76+
*/
77+
function (): string {
78+
return 'B';
79+
},
80+
'serviceC' => function (ContainerInterface $c): string {
81+
var_dump($c->get('letters'));
82+
},
83+
];
84+
```
85+
86+
The above example results in the following `var_dump()`:
87+
88+
```
89+
array(2) {
90+
[0]=>
91+
string(1) "A"
92+
[1]=>
93+
string(1) "B"
94+
}
95+
```
96+
5997
### Fun Things With Maps
6098
Maps are very commonly used in applications to represent some key-value relationships. We decided that PSR-11 containers are a great way to represent maps in an interop way. Here are some of the things you can do.
6199

@@ -130,6 +168,7 @@ echo $productionConfig->get('password'); // NotFoundException: This key does not
130168

131169
[`ServiceProvider`]: src/ServiceProvider.php
132170
[`CompositeCachingServiceProvider`]: src/CompositeCachingServiceProvider.php
171+
[`TaggingServiceProvider`]: src/TaggingServiceProvider.php
133172
[`DelegatingContainer`]: src/DelegatingContainer.php
134173
[`CachingContainer`]: src/CachingContainer.php
135174
[`CompositeContainer`]: src/CompositeContainer.php

0 commit comments

Comments
 (0)