8000 deprecate get() for uncompiled container builders by xabbuh · Pull Request #18728 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

deprecate get() for uncompiled container builders #18728

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
wants to merge 3 commits into from

Conversation

xabbuh
Copy link
Member
@xabbuh xabbuh commented May 9, 2016
Q A
Branch? 3.2
Bug fix? no
New feature? no
BC breaks? no
Deprecations? yes
Tests pass? yes
Fixed tickets #18673 (comment)
License MIT
Doc PR

@@ -102,6 +105,9 @@ public function testHas()
$this->assertTrue($builder->has('bar'), '->has() returns true if a service exists');
}

/**
* @group legacy
Copy link
Member

Choose a reason for hiding this comment

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

not all this test is legacy. It would be a great time to split the test into multiple smaller tests to mark only the relevant one as legacy

Copy link
Member Author

Choose a reason for hiding this comment

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

You are right. I split this into several small tests with the great effect that none of them needed to be marked as legacy.

@javiereguiluz
Copy link
Member

👍 I like this proposal a lot (I suffered some problems related to this recently)

@xabbuh
Copy link
Member Author
xabbuh commented May 9, 2016

#18729 and #18730 need to be considered to make sure that tests on lower branches will not fail in the future if this one gets merged.

I also split the big testGet() test into several small tests to prevent marking it as legacy.

@xabbuh xabbuh force-pushed the deprecate-uncompiled-get branch from 4adf349 to 42860eb Compare May 9, 2016 15:41
@@ -409,6 +411,10 @@ public function has($id)
*/
public function get($id, $invalidBehavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE)
{
if (!$this->compiled) {
@trigger_error(sprintf('Calling %s before compiling the container is deprecated since version 3.2 and will throw an exception in 4.0.', __METHOD__), E_USER_DEPRECATED);
}
Copy link
Member Author

Choose a reason for hiding this comment

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

We may want to think about moving this down after the call to the parent class' get() method (so you would be able to retrieve services that have been added using set() before). Alternatively, deprecating set() too might be another way to go.

Copy link
Member

Choose a reason for hiding this comment

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

See #19192

fabpot added a commit that referenced this pull request May 9, 2016
This PR was merged into the 2.3 branch.

Discussion
----------

call get() after the container was compiled

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

This will prevent future issues when calling `ContainerBuilder::get()` before compiling the container will be deprecated (see #18728).

Commits
-------

954126b call get() after the container was compiled
fabpot added a commit that referenced this pull request May 9, 2016
…ner service (xabbuh)

This PR was merged into the 2.7 branch.

Discussion
----------

[FrameworkBundle] prevent calling get() for service_container service

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

This change will simply fix the tests once #18728 gets merged. An alternative approach would be to compile the container so that the code would still work even for services that have been set directly using `set()`. However, compiling the container in a descriptor imo is an unexpected side effect which I tried to avoid here.

Commits
-------

2d46bd4 prevent calling get() for service_container service
@fabpot
Copy link
Member
fabpot commented May 9, 2016

Both related PRs have been merged now.

@xabbuh
Copy link
Member Author
xabbuh commented May 9, 2016

@fabpot Thanks. This will need to be rebased then when lower branches are merged up.

@@ -409,6 +411,10 @@ public function has($id)
*/
public function get($id, $invalidBehavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE)
{
if (!$this->compiled) {
@trigger_error(sprintf('Calling %s before compiling the container is deprecated since version 3.2 and will throw an exception in 4.0.', __METHOD__), E_USER_DEPRECATED);
Copy link
Member

Choose a reason for hiding this comment

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

This error message will be displayed like this:

Calling get before compiling the container is ...

get here may be confusing. What if we add the ( and ) to the method name?

Calling get() before compiling the container is ...

Or wrap the method name with quotes:

Calling "get" before compiling the container is ...

Copy link
Member Author

Choose a reason for hiding this comment

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

good catch, I added parentheses after the method name

@javiereguiluz
Copy link
Member

👍

@@ -4,6 +4,9 @@ UPGRADE FROM 3.x to 4.0
DependencyInjection
-------------------

* Calling `get()` on a `ContainerBuilder` instance before compiling the
container is not supported anymore and will throw an exception.
Copy link
Contributor

Choose a reason for hiding this comment

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

wdyt about replacing and will throw by and throws ?

@sstok
Copy link
Contributor
sstok commented Jun 7, 2016

Is there a better alternative for this? I'm actually using this.
https://github.com/rollerworks/RouteAutowiringBundle/blob/master/src/DependencyInjection/Compiler/RouteAutowiringPass.php#L59

Note. I'm only doing this ease the parameters resolving, else I need to do this manually and this is easier.

@stof
Copy link
Member
stof commented Jun 7, 2016

@sstok the issue is that you actually have no guarantee at all that your own code will work. The container is not yet ready to be used at this point, so your code works only if you are lucky (and any bundle could put the tag on a service requiring more configuration work done after your compiler pass and make your own compiler pass break badly, potentially with a fatal error)

@sstok
Copy link
Contributor
sstok commented Jun 7, 2016

@stof OK, In my case it's only about the parameters so that's something I can change.
And the parameters must be static, changing them in the CompilerPass is an edge-case and not supported (in the Bundle Extension class one can use the registering system). Also found another bundle where I need to change this.

@fabpot
Copy link
Member
fabpot commented Jun 8, 2016

Thank you @xabbuh.

@fabpot fabpot closed this Jun 8, 2016
fabpot added a commit that referenced this pull request Jun 8, 2016
…buh)

This PR was squashed before being merged into the 3.2-dev branch (closes #18728).

Discussion
----------

deprecate get() for uncompiled container builders

| Q             | A
| ------------- | ---
| Branch?       | 3.2
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | yes
| Tests pass?   | yes
| Fixed tickets | #18673 (comment)
| License       | MIT
| Doc PR        |

Commits
-------

f2e35cb deprecate get() for uncompiled container builders
@xabbuh xabbuh deleted the deprecate-uncompiled-get branch September 29, 2016 17:49
@fabpot fabpot mentioned this pull request Oct 27, 2016
nicolas-grekas added a commit to nicolas-grekas/symfony that referenced this pull request Nov 16, 2016
…r builders (xabbuh)"

This reverts commit 27f4680, reversing
changes made to e4177a0.
nicolas-grekas added a commit to nicolas-grekas/symfony that referenced this pull request Nov 16, 2016
…r builders (xabbuh)"

This reverts commit 27f4680, reversing
changes made to e4177a0.
nicolas-grekas added a commit to nicolas-grekas/symfony that referenced this pull request Nov 16, 2016
…r builders (xabbuh)"

This reverts commit 27f4680, reversing
changes made to e4177a0.
fabpot added a commit that referenced this pull request Nov 16, 2016
…builders" (nicolas-grekas)

This PR was merged into the 3.2-dev branch.

Discussion
----------

[DI] Revert "deprecate get() for uncompiled container builders"

| Q             | A
| ------------- | ---
| Branch?       | 3.2
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #18728
| License       | MIT
| Doc PR        | -

ping @xabbuh

This reverts commit 27f4680, reversing
changes made to e4177a0.

While upgrading a few projects to 3.2, I found it impossible to remove this deprecation without duplicating "by hand" the instantiation logic of some service needed in a compiler pass.

See https://github.com/dustin10/VichUploaderBundle/blob/master/DependencyInjection/Compiler/RegisterPropelModelsPass.php#L30 for such an example into the wild.

I think we should revert this deprecation. Using the container builder before it is compiled is tricky. Yet, bootstrapping in general is tricky and full of chicken-and-egg issues. In this case, using some services while bootstrapping the container works well. Deprecating would be fine if we were to provide some viable alternative. Duplicating the instantiation logic doesn't qualify as such to me, hence the proposed revert.

@xabbuh, if you'd like to keep some test cases that may be unrelated to the revert, please add comments on the PR, or better: push on my fork, branch revert-get-deprec.

Commits
-------

e449af0 Revert "feature #18728 deprecate get() for uncompiled container builders (xabbuh)"
ostrolucky pushed a commit to ostrolucky/symfony that referenced this pull request Mar 25, 2018
…rs (xabbuh)

This PR was squashed before being merged into the 3.2-dev branch (closes symfony#18728).

Discussion
----------

deprecate get() for uncompiled container builders

| Q             | A
| ------------- | ---
| Branch?       | 3.2
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | yes
| Tests pass?   | yes
| Fixed tickets | symfony#18673 (comment)
| License       | MIT
| Doc PR        |

Commits
-------

f2e35cb deprecate get() for uncompiled container builders
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.

8 participants
0