Skip to content

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

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
Nov 3, 2019
Merged

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

merged 1 commit into from
Nov 3, 2019

Conversation

mtarld
Copy link
Contributor

@mtarld mtarld commented Oct 4, 2019

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

decorator:
    decorates: decorated
    decoration_on_invalid: ignore

Available values: exception, ignore, null. exception if nothing is specified.

Xml configuration

<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

@nicolas-grekas nicolas-grekas added this to the next milestone Oct 4, 2019
@mtarld mtarld changed the title [WIP] Feature/behavior on decorated invalid Add ability to choose behavior of decorations on non existent decorated services Oct 7, 2019
@mtarld mtarld marked this pull request as ready for review October 7, 2019 16:35
@nicolas-grekas nicolas-grekas changed the title Add ability to choose behavior of decorations on non existent decorated services [DI] Add ability to choose behavior of decorations on non existent decorated services Oct 13, 2019
Copy link
Member

@nicolas-grekas nicolas-grekas left a comment

Choose a reason for hiding this comment

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

(FYI, I made some tweaks, esp. in ResolveInvalidReferencesPass)

@nicolas-grekas
Copy link
Member

There is a force-push collision here, the changes from https://github.com/symfony/symfony/compare/8bee11a60198ecb0729b451165f3e6036911d7db..33098d2ba8f5fbea8229ff4fc64a6215e138ea5c are lost. Please recover them :)

@OskarStark
Copy link
Contributor

Good to go @nicolas-grekas

@fabpot
Copy link
Member

fabpot commented Nov 3, 2019

Thank you @mtarld.

fabpot added a commit that referenced this pull request 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
@fabpot fabpot merged commit f167c77 into symfony:4.4 Nov 3, 2019
This was referenced Nov 12, 2019
OskarStark added a commit to symfony/symfony-docs that referenced this pull request Feb 15, 2020
…ce" (mtarld)

This PR was merged into the 4.4 branch.

Discussion
----------

Documented "Handling decorations on non existent service"

Documentation part for the PR:
symfony/symfony#33854

Commits
-------

cb07e30 Add "Handling decorations on non existent service"
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