Skip to content

[DI] allow decorators to reference their decorated service using the special .inner id #36389

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
Apr 12, 2020

Conversation

nicolas-grekas
Copy link
Member

@nicolas-grekas nicolas-grekas commented Apr 8, 2020

Q A
Branch? master
Bug fix? no
New feature? yes
Deprecations? no
Tickets -
License MIT
Doc PR -

Right now, when one wants to decorate a service, one needs to reference the decorated service using foo.inner, where foo is the id of the decorating service.

There are two issues with this IMHO:

  • it's weird to have to repeat the name of declaring service inside of it;
  • it doesn't play well with child definitions.

In this PR, I propose to use .inner to fix both issues.

Before:

services:
  decorating_service:
    arguments: [@decorating_service.inner]
    decorates: decorated_service

After:

services:
  decorating_service:
    arguments: [@.inner]
    decorates: decorated_service

@fabpot
Copy link
Member

fabpot commented Apr 12, 2020

Thank you @nicolas-grekas.

@fabpot fabpot merged commit 9a6695c into symfony:master Apr 12, 2020
@nicolas-grekas nicolas-grekas deleted the di-inner branch April 12, 2020 12:06
fabpot added a commit that referenced this pull request Apr 24, 2020
This PR was merged into the 5.1-dev branch.

Discussion
----------

[DI] add syntax to stack decorators

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| Deprecations? | no
| Tickets       | Fix #30599
| License       | MIT
| Doc PR        | -

Declare this:
```yaml
services:
    my_stack_of_decorators:
        stack:
            - class: App\ExternalDecorator
            - class: App\InternalDecorator
            - class: App\DecoratoredClass
```

And get this:
![image](https://user-images.githubusercontent.com/243674/78615803-b8c8e580-7872-11ea-95c2-22cb78f88ca8.png)

The PR is now ready with support for Yaml, XML and the PHP-DSL. It needs #36388, #36392 and #36389 to pass, and relates to #36390 to be DX-friendly.

The new syntax now supports composable stacks - i.e stack you can reuse in the middle of another stack.

RIP middleware, simple decorators FTW :)

From the test cases:
```yaml
services:
    reusable_stack:
        stack:
            - class: stdClass
              properties:
                  label: A
                  inner: '@.inner'
            - class: stdClass
              properties:
                  label: B
                  inner: '@.inner'

    concrete_stack:
        stack:
            - parent: reusable_stack
            - class: stdClass
              properties:
                  label: C
```

This will create a service similar to:
```php
(object) [
    'label' => 'A',
    'inner' => (object) [
        'label' => 'B',
        'inner' => (object) [
             'label' => 'C',
        ]
    ],
];
```

When used together with autowiring, this is enough to declare a stack of decorators:
```yaml
services:
    my_processing_stack:
        stack:
            - App\ExternalDecorator: ~
            - App\InternalDecorator: ~
            - App\TheDecoratedClass: ~
```

See fixtures for the other configuration formats.

See also https://twitter.com/nicolasgrekas/status/1248198573998604288

Todo:
- [x] rebase on top of #36388, #36392 and #36389 once they are merged
- [x] test declaring deeper nested stacks

Commits
-------

98eeeae [DI] add syntax to stack decorators
@nicolas-grekas nicolas-grekas modified the milestones: next, 5.1 May 4, 2020
@fabpot fabpot mentioned this pull request May 5, 2020
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.

3 participants