Setting service tags dynamically

5 years ago

Mikulas Dite
Member | 756
+
0
-

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)

5 years ago

Tomáš Votruba
Moderator | 1154
+
0
-

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)

5 years ago

Mikulas Dite
Member | 756
+
0
-

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?

5 years ago

Filip Procházka
Moderator | 4693
+
+1
-

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)

5 years ago

Mikulas Dite
Member | 756
+
0
-

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?

5 years ago

Filip Procházka
Moderator | 4693
+
0
-

Event the events.subsribers is trasnslated to services, but I probably still don't understand what and why you need.

5 years ago

jiri.pudil
Member | 861
+
0
-

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.

5 years ago

Mikulas Dite
Member | 756
+
0
-

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)

5 years ago

Filip Procházka
Moderator | 4693
+
0
-

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)

5 years ago

Mikulas Dite
Member | 756
+
0
-

Alright, will try. I suppose setting tags with addService should suffice.

5 years ago

Mikulas Dite
Member | 756
+
0
-

Turns out this feature was removed
https://github.com/…f4d61343dd99

Seems like quite an arbitrary bc break.

5 years ago

Filip Procházka
Moderator | 4693
+
0
-

I don't remember that, maybe @David Grudl might give some insight?

5 years ago

Mikulas Dite
Member | 756
+
0
-

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)

5 years ago

David Grudl
Nette Core | 6849
+
0
-

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.