Skip to content

[DependencyInjection] Added a way to define the priority of service decoration #15416

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 1 commit into from
Aug 5, 2015
Merged

[DependencyInjection] Added a way to define the priority of service decoration #15416

merged 1 commit into from
Aug 5, 2015

Conversation

dosten
Copy link
Contributor

@dosten dosten commented Jul 31, 2015

Q A
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets #10634
License MIT
Doc PR symfony/symfony-docs#5600

This PR adds a way to define the priority of service decoration, so, the service with the highest priority will be applied first (the default priority is zero).

services:
    foo:
        class: Foo

    bar:
        class: Bar
        arguments: ['@bar.inner']
        decorates: foo
        public: false

    foobar:
        class: Foobar
        arguments: ['@foobar.inner']
        decorates: foo
        decoration_priority: 1
        public: false

This will result in this code:

$this->services['foo'] = new Bar(new Foobar(new Foo)));

@dupuchba
Copy link
Contributor

@dosten can you give a use case for prioritizing service's decorator ? I fail to understand :-)

@stof
Copy link
Member

stof commented Jul 31, 2015

@dupuchba controlling the order in which multiple decorators for the same service are applied. new Bar(new Foobar(new Foo))) and new Foobar(new Bar(new Foo))) can lead to a totally different behavior at runtime. And without that, the order in which decorators are applied is undetermined (deterministic but not controllable)

@stof
Copy link
Member

stof commented Jul 31, 2015

btw, the issue requesting the change explains it already

*/
class DecoratorServicePass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
{
$definitions = new \SplPriorityQueue();
Copy link
Member

Choose a reason for hiding this comment

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

The usage of a priority query is a BC break here. Currently, decorators with the same priority (i.e. all of them for now) are applied in the order in which they appear in $container->getDefinitions(), which is more or less the order in which services have been defined (it is a bit more complex when a service gets overridden actually). but a SplPriorityQueue does not retain the order of insertion for the same priority.

And this is inconsistent with all other places using priorities in Symfony as we preserve the insertion order when the priority is the same.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@stof Thanks for your feedback, I missed up that part of the documentation. Using arrays and krsort is the way to go?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@stof Can you check again? I've implemented the same solution of ProfilerPass.

@dosten
Copy link
Contributor Author

dosten commented Aug 4, 2015

ping

@xabbuh
Copy link
Member

xabbuh commented Aug 4, 2015

👍 looks good to me

@stof
Copy link
Member

stof commented Aug 5, 2015

👍

Please submit a PR to the documentation repo to document this new feature for the 2.8 branch

@dosten
Copy link
Contributor Author

dosten commented Aug 5, 2015

IMO this can be labeled as reviewed, right?

@stof 👍

@dosten
Copy link
Contributor Author

dosten commented Aug 5, 2015

The docs PR can be found here.

@stof stof added the Ready label Aug 5, 2015
@stof
Copy link
Member

stof commented Aug 5, 2015

@dosten indeed. note that anyone can trigger the status change thanks to @carsonbot (the update of the doc to describe our goals with it is not yet ready unfortunately)

status: reviewed

@dosten
Copy link
Contributor Author

dosten commented Aug 5, 2015

@stof Yes, I know, but I feel unconfortable changing the status by myself.

@timglabisch
Copy link

👍

@stof
Copy link
Member

stof commented Aug 5, 2015

@dosten ah yeah, being the PR author, this seems fair to let someone else change. We have plans to implement a restriction about that in the bot itself in the future. I missed the fact you are the author when I commented

@fabpot
Copy link
Member

fabpot commented Aug 5, 2015

Thank you @dosten.

@fabpot fabpot merged commit 75c98cb into symfony:2.8 Aug 5, 2015
fabpot added a commit that referenced this pull request Aug 5, 2015
…ty of service decoration (dosten)

This PR was merged into the 2.8 branch.

Discussion
----------

[DependencyInjection] Added a way to define the priority of service decoration

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

This PR adds a way to define the priority of service decoration, so, the service with the highest priority will be applied first (the default priority is zero).

```yaml
services:
    foo:
        class: Foo

    bar:
        class: Bar
        arguments: ['@bar.inner']
        decorates: foo
        public: false

    foobar:
        class: Foobar
        arguments: ['@foobar.inner']
        decorates: foo
        decoration_priority: 1
        public: false
```

This will result in this code:

```php
$this->services['foo'] = new Bar(new Foobar(new Foo)));
```

Commits
-------

75c98cb Added a way to define the priority of service decoration
@dosten dosten deleted the dependency-injection/service-decoration-priority branch August 5, 2015 16:40
@fabpot fabpot mentioned this pull request Nov 16, 2015
xabbuh added a commit to symfony/symfony-docs that referenced this pull request Jan 13, 2016
…the service decoration priority (dosten)

This PR was merged into the 2.8 branch.

Discussion
----------

[DependencyInjection] Documented the ability of define the service decoration priority

| Q             | A
| ------------- | ---
| Doc fix?      | no
| New docs?     | yes (symfony/symfony#15416)
| Applies to    | 2.8+

Commits
-------

5868190 Documented the ability of define service decoration priority
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.

7 participants