Skip to content

Commit

Permalink
Merge pull request #28 from Dhii/feature/service-tagging
Browse files Browse the repository at this point in the history
Add Service Tagging Capability
  • Loading branch information
XedinUnknown authored Apr 27, 2024
2 parents f1d1470 + a186797 commit 1568cb2
Show file tree
Hide file tree
Showing 14 changed files with 622 additions and 262 deletions.
74 changes: 39 additions & 35 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,39 +1,43 @@
name: Continuous Integration

on: [push]
on:
- push

jobs:
run:
runs-on: ubuntu-latest
strategy:
matrix:
php-versions:
- '7.4'
- '8.0'
- '8.1'
- '8.2'
name: PHP ${{ matrix.php-versions }}
steps:
- uses: actions/checkout@v2

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-versions }}
coverage: none
tools: composer:v1

- name: Validate composer.json and composer.lock
run: composer validate

- name: Install dependencies
run: composer update --prefer-dist --no-progress --no-suggest

- name: Run PHPUnit
run: vendor/bin/phpunit

- name: Run PHPCS
run: ./vendor/bin/phpcs -s --runtime-set ignore_warnings_on_exit 1

- name: Run Psalm
run: vendor/bin/psalm
run:
runs-on: ubuntu-latest
strategy:
matrix:
php-versions:
- '7.4'
- '8.0'
- '8.1'
- '8.2'
- '8.3'
name: PHP ${{ matrix.php-versions }}
steps:
- uses: actions/checkout@v2

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-versions }}
coverage: none

- name: Validate composer.json and composer.lock
run: composer validate

- name: Install dependencies
uses: ramsey/composer-install@v3
with:
dependency-versions: highest
composer-options: "--prefer-dist"

- name: Run PHPUnit
run: vendor/bin/phpunit

- name: Run PHPCS
run: ./vendor/bin/phpcs -s --runtime-set ignore_warnings_on_exit 1

- name: Run Psalm
run: vendor/bin/psalm
12 changes: 12 additions & 0 deletions .idea/codeception.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 14 additions & 1 deletion .idea/php.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions .idea/phpspec.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Missing return types causing warnings with PHP 8.1 and higher.
- Static check issues.

### Added
- Service tagging capability (#28).

## [0.1.4] -2021-10-06
Stable release.

Expand Down
39 changes: 39 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ A selection of [PSR-11][] containers for utility, simplicity, and ease.
### DI
- [`ServiceProvider`][] - A super-simple implementation that allows quick creation of [service providers][Service Provider] from known maps of factories and extensions.
- [`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.
- [`TaggingServiceProvider`][] - A service provider that aggregates tagged services into a service with the tag's name.
- [`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.

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

### Service Tagging
You can tag your services into a collection. This adds a service with the same name as the tag,
which will return a list of services tagged with it.

Since a service name can theoretically be any legal string,
while some limitations need to be set for it to remain a tag,
the tag name can contain any character besides whitespace (anything that matches `\s`).

```php
[
'serviceA' =>
/** @tag letters */
fn (): string => 'A',
'serviceB' =>
/**
* @tag letters
*/
function (): string {
return 'B';
},
'serviceC' => function (ContainerInterface $c): string {
var_dump($c->get('letters'));
},
];
```

The above example results in the following `var_dump()`:

```
array(2) {
[0]=>
string(1) "A"
[1]=>
string(1) "B"
}
```

### Fun Things With Maps
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.

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

[`ServiceProvider`]: src/ServiceProvider.php
[`CompositeCachingServiceProvider`]: src/CompositeCachingServiceProvider.php
[`TaggingServiceProvider`]: src/TaggingServiceProvider.php
[`DelegatingContainer`]: src/DelegatingContainer.php
[`CachingContainer`]: src/CachingContainer.php
[`CompositeContainer`]: src/CompositeContainer.php
Expand Down
Loading

0 comments on commit 1568cb2

Please sign in to comment.