Skip to content

[DependencyInjection] ContainerBuilder: Remove obsolete definitions #19608

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
Aug 15, 2016
Merged

[DependencyInjection] ContainerBuilder: Remove obsolete definitions #19608

merged 1 commit into from
Aug 15, 2016

Conversation

ogizanagi
Copy link
Contributor

@ogizanagi ogizanagi commented Aug 13, 2016

Q A
Branch? 3.1
Bug fix? yes
New feature? no
BC breaks? may be considered
Deprecations? may be considered
Tests pass? yes
Fixed tickets -
License MIT
Doc PR -

IIUC, the obsolete definitions thing was tied to scoped & sync services.
Thus, this code is not meant to be used since 3.0 and can be removed.

However, it may be considered as a BC break and would require introducing a new deprecation instead, because the current code allows to set a service on a frozen container if it has an obsolete synthetic definition...which is weird, isn't it ? (I doubt this feature was explicitly intended to allow setting a synthetic service more than once, and it wasn't before sync services introduction)

I suggest this as a patch for 3.1, under the pretext of forgotten code tied to a previous deprecation & feature removal, but let me know if it should be considered differently.

||
(isset($this->obsoleteDefinitions[$id]) && !$this->obsoleteDefinitions[$id]->isSynthetic())
) {
if (!isset($this->definitions[$id]) || !$this->definitions[$id]->isSynthetic()) {
throw new BadMethodCallException(sprintf('Setting service "%s" on a frozen container is not allowed.', $id));
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe this should be explicit: Setting service "%s" for an unknown or non-synthetic service definition is not allowed when the container is frozen.

Copy link
Contributor

Choose a reason for hiding this comment

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

What about a check if $service is an instance of $definition->getClass() for sanity. Or "synchronize" the definition here accordingly, ie $definition->setClass(get_class($service));

Copy link
Contributor Author

@ogizanagi ogizanagi Aug 13, 2016

Choose a reason for hiding this comment

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

Thanks @ro0NL . I've changed the message according to your proposition.

About your other suggestion, I think it should be considered as a new "DX" feature, as it has been done in the past for similar exceptions giving better hint to the end user.

@fabpot
Copy link
Member

fabpot commented Aug 15, 2016

Thank you @ogizanagi.

@fabpot fabpot merged commit daa7d00 into symfony:3.1 Aug 15, 2016
fabpot added a commit that referenced this pull request Aug 15, 2016
…definitions (ogizanagi)

This PR was merged into the 3.1 branch.

Discussion
----------

[DependencyInjection] ContainerBuilder: Remove obsolete definitions

| Q             | A
| ------------- | ---
| Branch?       | 3.1
| Bug fix?      | yes
| New feature?  |no
| BC breaks?    | may be considered
| Deprecations? | may be considered
| Tests pass?   | yes
| Fixed tickets | -
| License       | MIT
| Doc PR        | -

IIUC, [the obsolete definitions thing was tied to scoped & sync services](#7007).
Thus, this code [is not meant to be used since 3.0 and can be removed](#13289).

However, it may be considered as a BC break and would require introducing a new deprecation instead, because the current code allows to set a service on a frozen container if it has an obsolete synthetic definition...which is weird, isn't it ? (I doubt this feature was explicitly intended to allow setting a synthetic service more than once, and it wasn't before sync services introduction)

I suggest this as a patch for 3.1, under the pretext of forgotten code tied to a previous deprecation & feature removal, but let me know if it should be considered differently.

Commits
-------

daa7d00 [DependencyInjection] ContainerBuilder: Remove obsolete definitions
@ogizanagi ogizanagi deleted the dic/3.1/remove_obs_def branch August 16, 2016 05:18
@Revisor
Copy link

Revisor commented Sep 9, 2016

Hello,
I was relying on the possibility to set a synthetic service multiple times in my integration tests and this commit broke them.

I doubt this feature was explicitly intended to allow setting a synthetic service more than once

Yeah, me too, but here I was, trying to bend the container to my will and this solution worked. :)

Fortunately I was able to rewrite the tests so that they set the service only once.
I just wanted to share my anecdote regarding corner cases and a BC break.

@ogizanagi
Copy link
Contributor Author

ogizanagi commented Sep 9, 2016

@Revisor : Thanks for your feedback. I'm happy you were able to rewrite your tests.
However, did your tests rely on the ContainerBuilder class or the compiled container (instance of Container) ?
I've the feeling using the ContainerBuilder in your case is wrong and that you should set your synthetic service on a compiled container instance instead.

@Revisor
Copy link

Revisor commented Sep 9, 2016

The services I wanted to swap were originally not synthetic, so I thought I had to first switch their definitions to synthetic, then compile the ContainerBuilder. Then I would set some default mocks and swap the services themselves in the tests as needed (so two service swaps, this stopped working).

I managed to compress this into one step, just swap the services before compilation.
I confirmed it works both in 3.1 as well as 3.0.

So yeah, I was definitely doing it in a needlessly complicated way and this commit forced me to find a simpler solution. :]

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.

6 participants