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
[DI] Decorate the service directly instead of override #30599
Comments
At first i liked it :) but we introduce a new (less flexible) concept i believe. In the first case you are free to modify each decorator as needed. In your proposal we lose this possibility. If we make e.g. TLDR; im not sure how much it increases the complexity of service configuration or what traps we create, given it opens multiple paths. |
your proposal forbids configuring the decorator services. So it is inferior to the current solution. |
I don't understand what forbids configuring the decorator services either what is less flexible services:
ham_burger:
class: Burger
autowire: true
decorators:
- HamDecorator
cheese_burger:
class: Burger
decorators:
- CheeseDecorator
- HamDecorator
arguments:
- 'foo'
CheeseDecorator:
arguments:
- '@CheeseDecorator.inner'
- 'cheddar'
ketchupDecorator:
decorate: '@ham_burger' |
it shouldnt be required to use class named service IDs My worry is What if i start adding |
It's ok, CheeseDecorator definition is ugly Class named service IDs is mandatory if you want a lot of configuration for the same class and this is probably in case you implement decorator pattern Thanks for your feedback |
not sure i understand that, the same class can be used as a decorator N times. |
and it also requires the service being decorated to know about its decorators, so it is also less flexible as an extension point. |
Well, in your proposal, things indeed break if 2 services declare |
What if |
Well, the fact that this would have to create other services would make it a nightmare to configure them if there is any need for an explicit configuration. |
Is your proposal just syntactical sugar for smth like that (which is already possible)? services:
burger:
class: Burger
arguments:
- !service
class: Ham
arguments:
- !service
class: Cheese |
Exactly. In my case i have something like 30 decorator service with something like 15 decorated services |
This could work and would be pretty neat to me: services:
burger:
stack:
- SomeClass: ['@stack.next']
- AnotherClass:
calls:
- setDecorated: ['@stack.next']
- TheLastClass # string <=> TheLastClass: [] This would be turned into actual decorators by loaders. |
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
Description
Add possibility to decorate the service directly instead of override.
Example
override:
decorate :
The text was updated successfully, but these errors were encountered: