Setting service tags dynamically
- Mikulas Dite
- Member | 756
Hi, is it possible to register new service with tags?
Container::addService
does
not seem to accept meta arguments in current stable.
I would like to simplify the following definition:
services:
-
class: App\Rme\Badges\VideoWatched
tags: [kdyby.subscriber]
-
class: App\Rme\Badges\UserOldWeek
tags: [kdyby.subscriber]
-
class: App\Rme\Badges\ExerciseMastery
tags: [kdyby.subscriber]
# ...
would be rewritten to
services:
- App\Rme\Badges\VideoWatched
- App\Rme\Badges\UserOldWeek
- App\Rme\Badges\ExerciseMastery
# ... + add tags dynamically (eg in custom configurator)
Last edited by Mikulas Dite (2014-05-16 10:51)
- Tomáš Votruba
- Moderator | 1114
Hi, I'm not sure I understand. You can tag services later in your own CompilerExtension
And maybe not Container, but ContainerBuilder::addDefinition
Could you describe, what exactly do you mean by add tags dynamically (eg in custom configurator)?
Last edited by Tomáš Votruba (2014-05-16 12:12)
- Mikulas Dite
- Member | 756
Well all I have is custom Nette\Configurator
and the compiled
Container
. In order to get ContainerBuilder, I would have to
override Configurator::createCompiler()
and
Compiler::getContainerBuilder()
. That is a lot of hassle for adding
tag to a service (adds 2 classes).
Rephrased question: what is the shortest way to add tags to existing service?
- Filip Procházka
- Moderator | 4668
For kdyby/event subscribers you can use this
events:
subscribers:
- App\Rme\Badges\VideoWatched
- App\Rme\Badges\UserOldWeek
- App\Rme\Badges\ExerciseMastery
Or if you preffer services
services:
- {class: App\Rme\Badges\VideoWatched(), tags: [kdyby.subscriber]}
- {class: App\Rme\Badges\UserOldWeek(), tags: [kdyby.subscriber]}
- {class: App\Rme\Badges\ExerciseMastery(), tags: [kdyby.subscriber]}
Last edited by Filip Procházka (2014-05-17 01:39)
- Mikulas Dite
- Member | 756
Alright, thanks :) I don't need those as services so specifying
events.subscribers
works for me in this case.
Anyway, why can't Container::addService
have an optional
$tags = []
parameter?
- Filip Procházka
- Moderator | 4668
Event the events.subsribers is trasnslated to services, but I probably still don't understand what and why you need.
- jiri.pudil
- Nette Blogger | 1029
what is the shortest way to add tags to existing service?
Anyway, why can't
Container::addService
have an optional$tags = []
parameter?
What for, exactly? If Container is available, it must have already been compiled and built, and therefore extensions that might somehow process tagged services have already done their thing.
- Mikulas Dite
- Member | 756
In that case, Container
should be immutable and not able to be
edited with addService
, isn't that so?
This problem is rather straightforward: after the container is compiled, new
services can be added with addService
. Why can't these newly added
services have tags?
I have already refactored the code I needed this for, but in theory this still stands; I think tags should be modifiable after compile. Or BOTH tags and services should NOT be modifiable. The inconsistency is confusing.
Last edited by Mikulas Dite (2014-05-18 22:13)
- Filip Procházka
- Moderator | 4668
You have a valid point. Do you wanna send a pull request?
You should carefully add API for adding the tags and possibly also think
about fixing the fact that when you add service that wasn't compiled in, it
cannot be resolved using ->getByType()
method. That should be
two separate pulls though. Maybe someone else would want to do it?
Last edited by Filip Procházka (2014-05-19 00:23)
- Mikulas Dite
- Member | 756
Turns out this feature was removed
https://github.com/…f4d61343dd99
Seems like quite an arbitrary bc break.
- Mikulas Dite
- Member | 756
ping @DavidGrudl, proč byl z
addService($name, $service, array $meta = NULL)
smazán
prosím? Dá se to teď řešit nějak jinak než vlastním compilerem builderem
a buhvíčím?
Last edited by Mikulas Dite (2014-06-24 13:40)
- David Grudl
- Nette Core | 8218
Protože cílem je statická kompilace a dynamicky lze měnit jen konkrétní instance služeb. Tedy meta-data by měly být známy již při kompilaci.