8000 [DI] Handling decorations on non existent services · Issue #33522 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content
8000

[DI] Handling decorations on non existent services #33522

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
pascalheidmann-bedarf opened this issue Sep 9, 2019 · 3 comments
Closed

[DI] Handling decorations on non existent services #33522

pascalheidmann-bedarf opened this issue Sep 9, 2019 · 3 comments
Labels
DependencyInjection Feature Help wanted Issues and PRs which are looking for volunteers to complete them.

Comments

@pascalheidmann-bedarf
Copy link
pascalheidmann-bedarf commented Sep 9, 2019

Description
It would be nice to handle decorations on non existent services by either throwing the service not found exception or just silently ignoring the service all together, similar to how missing services as parameters are handles.

Example
Imagine having an OptionalBundle with a service OptionalBundle\Services\OptionalService and another Bundle DecoratorBundle that has to decorate the OptionalService if present:

<service id="OptionalBundle\Services\OptionalService" class="OptionalBundle\Services\OptionalService" />
<!-- ... -->
<service id="DecoratorBundle\Service\Decorator\OptionalServiceDecorator" decorates="OptionalBundle\Services\OptionalService" class="DecoratorBundle\Service\Decorator\OptionalServiceDecorator" on-invalid="ignore" />

Now in my example I don't have OptionalBundle installed and the DecoratorBundle would be broken. With the new attribute on-invalid with values exception, ignore, null you can control the handling:

  • exception: default, throw exception
  • ignore: don't create decoration service at all
  • null: create decoration service but if service.inner gets passed as a parameter it will be null -> might need explicit on-invalid attribute on parameter itself.

For YAML declaration I would suggest using the @? syntax as for arguments.

@javiereguiluz javiereguiluz changed the title [DI] Handling decorations on non existent servics [DI] Handling decorations on non existent services Sep 10, 2019
@eschultz-magix
Copy link
Contributor

Just an idea:

  • make the decorator service abstract and give it a suffix (f.ex. ".abstract")
  • write an extension that will
    -- check if the optional bundle is installed
    -- create the decorator service with an alias to the abstract service

@xabbuh xabbuh added the Feature label Sep 10, 2019
@nicolas-grekas
Copy link
Member

That would make sense to me. Anyone for giving it a try?

@nicolas-grekas nicolas-grekas added the Help wanted Issues and PRs which are looking for volunteers to complete them. label Sep 26, 2019
@mtarld
Copy link
Contributor
mtarld commented Oct 4, 2019

Yes I'll :)

@fabpot fabpot closed this as completed Nov 3, 2019
fabpot added a commit that referenced this issue Nov 3, 2019
…non existent decorated services (mtarld)

This PR was merged into the 4.4 branch.

Discussion
----------

[DI] Add ability to choose behavior of decorations on non existent decorated services

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | no
| New feature?  | yes
| Deprecations? | no
| Tickets       | #33522
| License       | MIT
| Doc PR        | symfony/symfony-docs#12442

# Handling decorations on non existent decorated services
Handle decorations on non existent decorated services by either throwing the service not found exception, silently ignoring services (decorator & decorated) all together or leave the decorated service to null (current behavior)

Something almost similar to how missing services as parameters are handles.

## Yaml configuration
```yaml
decorator:
    decorates: decorated
    decoration_on_invalid: ignore
```
Available values: `exception`, `ignore`, `null`. `exception` if nothing is specified.

## Xml configuration
```xml
<service id="decorator" decorates="decorated" decoration-on-invalid="ignore" />
```
Available values: `exception`, `ignore`, `null`. `exception` if nothing is specified.

## Behavior
- `exception`: Throws a `ServiceNotFoundException` telling that the decorator's dependency is missing
- `ignore`: Remove decorator definition. Decorator and decorated will not be available at all.
- `null`: Keep decorator but set decorated to null. Therefore, decorator `__construct` should be written with a nullable decorated dependency (`public function __contruct(?DecoratedInterface $decorated) {}`) and check should be done in other methods

Commits
-------

f167c77 Handle non existent decorated services
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
DependencyInjection Feature Help wanted Issues and PRs which are looking for volunteers to complete them.
Projects
None yet
Development

No branches or pull requests

7 participants
0