8000 [DependencyInjection] Decorate synthetic service · Issue #40784 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[DependencyInjection] Decorate synthetic service #40784

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

Closed
Firtzberg opened this issue Apr 12, 2021 · 6 comments
Closed

[DependencyInjection] Decorate synthetic service #40784

Firtzberg opened this issue Apr 12, 2021 · 6 comments

Comments

@Firtzberg
Copy link

Is it possible to decorate a synthetic service (set at runtime)?
When I try to do so I get the error message A synthetic service ("Vendor\Product\Decorator.inner") must be public.

Here's the YAML file

services:
  Vendor\Product\Synthetic:
    public: true
    synthetic: true
  Vendor\Product\Decorator:
    class: stdClass
    decorates: Vendor\Product\Synthetic
    public: true
@stof
Copy link
Member
stof commented Apr 13, 2021

I don't think it is, as setting the service would then have to actually set the inner one (while the call does not know the inner id).
Synthetic services are not managed by the container but set externally (and so they should be quite rare), so this limits which features can be done with them.

@nicolas-grekas should we add a dedicated error in the resolution of service decoration when trying to decorate a synthetic service to give a better error message ?

@nicolas-grekas
Copy link
Member

A more specific error message would be nice yes. Up to give it a try @Firtzberg?

@Firtzberg
Copy link
Author
Firtzberg commented Apr 13, 2021

I'm fine with a more detailed error message or a compiler pass which prevents this. Whatever you consider best.
I would like this limitation of synthetic services to be documented. You could possibly mention the workaround below.
Please close the issue when you please.

In case someone stumbles upon this, I am using a workaround.

services:
  RuntimeInjection:
    synthetic: true
  Vendor\Product\Interface:
    class: Vendor\Product\Delegator
    arguments: ['@RuntimeInjection']
  Vendor\Product\Decorator:
    decorates: Vendor\Product\Interface

The Delegator just forwards all method calls to the instance provided at runtime. Vendor\Product\Interface can be used anywhere and decorated as needed.

After bulding the container:

$container->set('RuntimeInjection', $instance);

@nicolas-grekas
Copy link
Member

@Firtzberg would you be up to work on a PR to improve the error message?

@Firtzberg
Copy link
Author

@nicolas-grekas I'm honored, but I don't think I'm up to the task.

@nicolas-grekas
Copy link
Member

No worries, see #41779

derrabus added a commit that referenced this issue Jun 22, 2021
…ng a synthetic service (nicolas-grekas)

This PR was merged into the 4.4 branch.

Discussion
----------

[DependencyInjection] throw proper exception when decorating a synthetic service

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

Commits
-------

b763a29 [DependencyInjection] throw proper exception when decorating a synthetic service
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

< 332F /div>
5 participants
0