From cecfd425ac5694c2e0644735364c58d6b4468bf7 Mon Sep 17 00:00:00 2001 From: Anton Ukhanev Date: Sat, 27 Apr 2024 02:52:12 +0200 Subject: [PATCH] Update readme Added description of Service Tagging feature. --- README.md | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/README.md b/README.md index a8996d6..d393343 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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. @@ -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