Download the PHP package xphere/tag-bundle without Composer
On this page you can find all versions of the php package xphere/tag-bundle. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download xphere/tag-bundle
More information about xphere/tag-bundle
Files in xphere/tag-bundle
Package tag-bundle
Short Description Helps collecting tagged services to inject them into others
License MIT
Homepage https://github.com/xphere/tag-bundle
Informations about the package tag-bundle
xphere/tag-bundle
Are you tired to add CompilerPasses just to collect some services tagged on your container?
Say NO to most of them with this bundle!
⚠ Note ⚠
Mind the namespace change
- Before
0.4.0:Berny\Bundle\TagBundle - After
0.4.0:xPheRe\Bundle\TagBundle
Why I would want that?
More than often you want to search for services tagged with a particular tag and call some method in your service with them. This can be done with a custom CompilerPass.
Another use case is to inject a service to every other that is tagged with a particular tag. An example:
This boilerplate is repeated once and again in every project I've seen. With this bundle you can say goodbye to most of this compiler passes.
Features
With this bundle you can:
- Tag a service as a consumer of another tag.
- Tag a service as injectable into others.
Compatibility
Tested under Symfony2, from 2.0.10 to 2.6.3
Installation
From composer/packagist
- Require
xphere/tag-bundlepackage in your composer - Add the bundle to your
AppKernel.php
Usage
Consumer
You can define a service as a "tag consumer" of another tag, and let the bundle make the relationship between them.
Just tag your service as a tag.consumer and specify which tag to collect and which method to call.
The first example using this bundle is just configuration:
The only change is the tag in my_plugin_enumerator. The CompilerPass boilerplate is gone.
This calls PluginEnumerator::addPlugin with each my_plugin, but you can also call this once with all services using the bulk parameter.
This is calling PluginEnumerator::addPlugins just once, with an array of the services.
To make the service consume its dependencies through it's constructor, just omit the method attribute in the tag.
Injectable
You can define a service as a "tag injectable" from another tag, and let the bundle do the hard work.
Just tag your service as a tag.injectable and specify which tag to collect and which method to call in each service.
The second example in the introduction will be like this:
The only change is the tag in my_event_dispatcher. The CompilerPass boilerplate is also gone.
This forces all my_event_dispatcher.aware to have a setMyEventDispatcher method. But you can change that for a particular service with the method parameter.
Now it's calling setEventDispatcher for UselessService, and the default method for the others.
Advanced usage
That's all about the basics, there are more options available for major control over your dependencies, though.
Order
You can specify the order in which services will be injected into the consumer with the order field in each tag.
Lower orderings have priority over higher orders. Tagged services with no order will be injected after ordered ones.
In case of a tie between orders, keeps symfony declaration order.
Indexing bulk services
When bulk is active, you can specify a key which will be used to index each tag, instead of a plain array.
This results in the next injection:
You can also specify that multiple elements will collide with same index and needs to collect arrays instead of single services with the multiple field in your consumer definition.
This results in the next injection:
Multiple also honors ordering, if specified.
Reference
As usual, dependencies are injected directly to your service, but you can inject your dependencies as service ids instead by setting the field reference to false in your consumer definition.
InstanceOf
You can force your dependencies to be an instance of a class or interface with the field instanceof in your consumer definition.
No bundle
You can add manually TagConsumerPass or TagInjectablePass (or both) without adding the "whole" bundle to your Kernel, even customize the tag names used to apply them.
In your Kernel:
All versions of tag-bundle with dependencies
symfony/http-kernel Version >=2.0.10
symfony/config Version >=2.0.10
