Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Docs for referencing tagged services in config #8404

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Oct 13, 2017
Merged

Docs for referencing tagged services in config #8404

merged 9 commits into from
Oct 13, 2017

Conversation

ro0NL
Copy link
Contributor

@ro0NL ro0NL commented Sep 17, 2017

See symfony/symfony#22200

Curious how it looks :) also a bit related to #8403

Copy link
Member

@wouterj wouterj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for writing the docs @ro0NL! I've left some comments, can you have a look at them (feel free to change or comment on all my suggestions)

@@ -405,3 +405,92 @@ The double loop may be confusing. This is because a service can have more
than one tag. You tag a service twice or more with the ``app.mail_transport``
tag. The second foreach loop iterates over the ``app.mail_transport``
tags set for the current service and gives you the attributes.

Reference tagged services
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Reference Tagged Services" (title case uppercases almost every word, except when there are closed words)


ThereBecause this task is so generic and common to do, Symfony provides a way to achieve this
directly in your service container confguration. This enables to inject services tagged
with e.g. `app.handler` into another service that collects all handlers.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inline code blocks should be surrounded by double backticks (single backticks are used for references)

tags: [app.handler]

App\HandlerCollection:
arguments: [!tagged app.handler]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's put a comment here describing that this is passing all services tagged with app.handler as an array argument to this class. (Same for the other formats)

public function __construct(iterable $handlers)
{
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This block should be moved 4 spaces to the left (the .. code-block:: php part should be in column 0)

- { name: app.handler, priority: 20 }

.. versionadded:: 3.4

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line should be removed (versionadded is a really strange directive). And let's move this to just below the section title

In case your tag doesn't require any further additional attributes writing compiler
passes per tag might become tedious. A way to overcome this is is to make your compiler
pass more generic. The downside of this approach is you have to write and maintain
additional code, considering you want to reuse it over multiple projects.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need so much information and we're kinda missing the complete feature here: Using tags to inject a list of services to another service. Tags can do non-injecting stuff as well. What about saying something like: (change at your free will 😄 )

If you use tags to inject a list of services as an argument, writing a compiler pass is a bit tedious. As this is a very common case, Symfony provides a way to inject all services tagged with a specific tag. The downside of this feature is that you can't have any custom attributes. In the example below, all services tagged with app.handler are passed in an array as the first constructor argument to the App\\HandlerCollection service:

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agree. this text is good as a blog post introducing this new feature as it explains the thought process. but it does not fit as a feature documention

nicolas-grekas added a commit to symfony/symfony that referenced this pull request Sep 28, 2017
This PR was merged into the 3.4 branch.

Discussion
----------

[DI] Reference tagged services in config

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #12269
| License       | MIT
| Doc PR        | symfony/symfony-docs#8404

This is a proof of concept to reference a sequence of tagged services.

The problem bugs me for some time, and at first i thought the solution was to have some super generic compiler pass. If it could replace a lot of compilers in core.. perhaps worth it, but eventually each tag comes with it's own logic, including how to deal with tag attributes.

However, writing the passes over and over again becomes tedious for the most basic usecase. So given the recent developments, this idea came to mind.

```yml
services:
    a:
        class: stdClass
        properties: { a: true }
        tags: [foo]

    b:
        class: stdClass
        properties: { b: true }
        tags: [foo]

    c:
        class: stdClass
        properties:
            #stds: !tagged_services foo (see #22198)
            stds: !tagged_services
                foo
```

```
dump(iterator_to_array($this->get('c')->stds));
```

```
array:2 [▼
  0 => {#5052 ▼
    +"a": true
  }
  1 => {#4667 ▼
    +"b": true
  }
]
```

Given the _basic_ example at https://symfony.com/doc/current/service_container/tags.html, this could replace that.

Any thoughts?

Commits
-------

979e58f [DI] Reference tagged services in config
symfony-splitter pushed a commit to symfony/yaml that referenced this pull request Sep 28, 2017
This PR was merged into the 3.4 branch.

Discussion
----------

[DI] Reference tagged services in config

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #12269
| License       | MIT
| Doc PR        | symfony/symfony-docs#8404

This is a proof of concept to reference a sequence of tagged services.

The problem bugs me for some time, and at first i thought the solution was to have some super generic compiler pass. If it could replace a lot of compilers in core.. perhaps worth it, but eventually each tag comes with it's own logic, including how to deal with tag attributes.

However, writing the passes over and over again becomes tedious for the most basic usecase. So given the recent developments, this idea came to mind.

```yml
services:
    a:
        class: stdClass
        properties: { a: true }
        tags: [foo]

    b:
        class: stdClass
        properties: { b: true }
        tags: [foo]

    c:
        class: stdClass
        properties:
            #stds: !tagged_services foo (see #22198)
            stds: !tagged_services
                foo
```

```
dump(iterator_to_array($this->get('c')->stds));
```

```
array:2 [▼
  0 => {#5052 ▼
    +"a": true
  }
  1 => {#4667 ▼
    +"b": true
  }
]
```

Given the _basic_ example at https://symfony.com/doc/current/service_container/tags.html, this could replace that.

Any thoughts?

Commits
-------

979e58f [DI] Reference tagged services in config
symfony-splitter pushed a commit to symfony/dependency-injection that referenced this pull request Sep 28, 2017
This PR was merged into the 3.4 branch.

Discussion
----------

[DI] Reference tagged services in config

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #12269
| License       | MIT
| Doc PR        | symfony/symfony-docs#8404

This is a proof of concept to reference a sequence of tagged services.

The problem bugs me for some time, and at first i thought the solution was to have some super generic compiler pass. If it could replace a lot of compilers in core.. perhaps worth it, but eventually each tag comes with it's own logic, including how to deal with tag attributes.

However, writing the passes over and over again becomes tedious for the most basic usecase. So given the recent developments, this idea came to mind.

```yml
services:
    a:
        class: stdClass
        properties: { a: true }
        tags: [foo]

    b:
        class: stdClass
        properties: { b: true }
        tags: [foo]

    c:
        class: stdClass
        properties:
            #stds: !tagged_services foo (see #22198)
            stds: !tagged_services
                foo
```

```
dump(iterator_to_array($this->get('c')->stds));
```

```
array:2 [▼
  0 => {#5052 ▼
    +"a": true
  }
  1 => {#4667 ▼
    +"b": true
  }
]
```

Given the _basic_ example at https://symfony.com/doc/current/service_container/tags.html, this could replace that.

Any thoughts?

Commits
-------

979e58f [DI] Reference tagged services in config
@ro0NL
Copy link
Contributor Author

ro0NL commented Oct 3, 2017

ping @wouterj comments addressed :)

@Tobion
Copy link
Contributor

Tobion commented Oct 4, 2017

IMO it should be mentioned the passed iterable is a generator that loads each service lazily when iterating.

Copy link
Member

@wouterj wouterj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

~~~~~~~~~~~~~~~~~~~~~~~~~

.. versionadded:: 3.4

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This empty line should be removed, otherwise the directive doesn't work (versionadded is weird)


The downside of this feature is that you can't have any custom attributes. In the
example below, all services tagged with ``app.handler`` are passed as first
constructor argument to the ``App\HandlerCollection`` service:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'll have to look at platformsh, but I think we need a double \\ here.

Copy link
Member

@javiereguiluz javiereguiluz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a question about this phrase:

The only downside of this feature is that you can't
have any custom attributes.

Could you please explain a bit more what does it mean? Thanks!

@ro0NL
Copy link
Contributor Author

ro0NL commented Oct 4, 2017

Agree. You can have them, however you cant leverage them with this specific feature. That needs to be clarified yes.

Status: needs work


The collected services can be prioritized using the ``priority`` attribute.

.. code-block:: yaml
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are missing XML and PHP examples. :)


.. tip::

The collected services can be prioritized using the ``priority`` attribute.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the sentence could end with a colon

$container->register(\App\Handler\Two::class)
->addTag('app.handler');

$container->register(\App\HandlerCollection::class)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the leading backslash should be removed eveyrwhere


.. code-block:: yaml

services:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing filename comment (same for the other formats):

# app/config/services.yml

@ro0NL
Copy link
Contributor Author

ro0NL commented Oct 5, 2017

Last commit 2e5c87f addresses @javiereguiluz comment.

->addTag('app.handler', array('priority' => 20));

Note that any other custom attributes will be ignored by this feature.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

trailing blank line should be removed


# app/config/services.yml
services:
App\Handler\One:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should still use examples from the AppBundle namespace

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's right. We'll remove AppBundle in 4.0, but we must still use it in 3.4.


.. code-block:: php

class HandlerCollection
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing namespace and filename comment

Copy link
Member

@xabbuh xabbuh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just left a few minor comments, but apart from that this looks good to me.

@xabbuh xabbuh added this to the 3.4 milestone Oct 6, 2017
@xabbuh
Copy link
Member

xabbuh commented Oct 13, 2017

Thank you @ro0NL.

@xabbuh xabbuh merged commit ed70659 into symfony:3.4 Oct 13, 2017
xabbuh added a commit that referenced this pull request Oct 13, 2017
…javiereguiluz)

This PR was merged into the 3.4 branch.

Discussion
----------

Docs for referencing tagged services in config

See symfony/symfony#22200

Curious how it looks :) also a bit related to #8403

Commits
-------

ed70659 Update tags.rst
2e5c87f Update tags.rst
564b5ea Update tags.rst
a2fd23f Update tags.rst
000b801 Minor reword and fixes
0aaf48b Update tags.rst
71158f8 Update tags.rst
61c74da Update tags.rst
da034d2 Docs for referencing tagged services in config
@ro0NL ro0NL deleted the patch-2 branch October 13, 2017 07:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants