Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[DI] Check for privates before shared services #22866

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
May 24, 2017
Merged

[DI] Check for privates before shared services #22866

merged 1 commit into from
May 24, 2017

Conversation

ro0NL
Copy link
Contributor

@ro0NL ro0NL commented May 23, 2017

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

cc @stof

@nicolas-grekas
Copy link
Member

CI is red :)

@nicolas-grekas nicolas-grekas added this to the 3.2 milestone May 23, 2017
@stof
Copy link
Member

stof commented May 23, 2017

@nicolas-grekas this is because of composer/composer#6342

@ro0NL
Copy link
Contributor Author

ro0NL commented May 23, 2017

Kinda clueless here... trying to fix WebProfilerExtensionTest::testToolbarConfig

patched assertSaneContainer so it skips private id's (hardcoded locally to test :)

however these 2 assertions pass for all provided debug modes, indiviudally;

        $this->assertSaneContainer($this->getDumpedContainer(), '', array('web_profiler.csp.handler'));

        if ($listenerInjected) {
            $this->assertSame($listenerEnabled, $this->container->get('web_profiler.debug_toolbar')->isEnabled());
        }

the combination seems to trigger a deprecation 😕

@ro0NL
Copy link
Contributor Author

ro0NL commented May 23, 2017

first patch.. lets see what happens :)

@ro0NL
Copy link
Contributor Author

ro0NL commented May 23, 2017

testStopwatchExtensionAvailability actually gets a private service 😠

$container->get('twig.extension.debug.stopwatch')->getTokenParsers()

is there a clean way to silence expected deprecations.. or should we do something else here.

@stof see deprecations at appveyor ;)

@stof
Copy link
Member

stof commented May 23, 2017

well, the test should either not access a private service, or be marked as legacy. If the test is not meant to be removed in 4.0, marking it as legacy is not an option, and the test should be rewritten instead.
This is good that I catched the mistake in the deprecation trigger in the other PR, as this allowed us to catch mistakes in our own testsuite which was using deprecated APIs without getting the notice...

@ro0NL
Copy link
Contributor Author

ro0NL commented May 23, 2017

Updated.. this should do. But i dont like it really :) but i guess we have no choice as we never decided how privates reflects a compiled containerbuilder.

@stof to clarify.. the root issue is ContainerBuilder::get() calling parent::get

$this->compileContainer($container);

$tokenParsers = $container->get('twig.extension.debug.stopwatch')->getTokenParsers();
$tokenParsers = $container->get('test_debug_stopwatch')->stopwatch->getTokenParsers();
Copy link
Member

Choose a reason for hiding this comment

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

a better solution would be to use $container->get('twig')->getExtension(StopwatchExtension::class) to access the extension, which avoids registering an extra service

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed

Copy link
Contributor Author

@ro0NL ro0NL May 23, 2017

Choose a reason for hiding this comment

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

seems to break stuff :/

1) Symfony\Bundle\TwigBundle\Tests\DependencyInjection\TwigExtensionTest::testStopwatchExtensionAvailability with data set "debug-and-stopwatch-enabled" (true, true, true)
Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException: You have requested a non-existent service "twig.loader".

Copy link
Contributor Author

Choose a reason for hiding this comment

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

reverted the whole change, as with privates being empty it keeps working :)

this test should fail whenever we decide to change behavior.

@@ -262,6 +262,10 @@ public function get($id, $invalidBehavior = self::EXCEPTION_ON_INVALID_REFERENCE
if (isset($this->aliases[$id])) {
$id = $this->aliases[$id];
}
if (isset($this->privates[$id]) && !$this instanceof ContainerBuilder) {
Copy link
Member

Choose a reason for hiding this comment

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

why the check on ContainerBuilder ? this is useless ($this->privates will be empty for them anyway)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

i think ContainerBuilder needs to behave the same as Container, after compilation. But that could be a separate PR i guess. The changelog doesnt mention ContainerBuilder really...

Copy link
Member

Choose a reason for hiding this comment

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

well, once compiled, the array is not empty anymore, but we do want the deprecation in this case. So please remove the extra check you added there (which was not there before moving the notice btw, so you are the one adding a difference in the behavior)

@nicolas-grekas
Copy link
Member

I think we should move this to 3.3: it will trigger more notices in 3.2 apps and break some strict CI for no real benefit.

@nicolas-grekas nicolas-grekas modified the milestones: 3.3, 3.2 May 23, 2017
@ro0NL
Copy link
Contributor Author

ro0NL commented May 23, 2017

3.2 is truly buggy though.. (@stof's comment in previous PR) i think the ContainerBuilder check will prevent those unexpected notices actually; while fixing Container.

otherwise, could you just commit the changelog against 3.2 :)) but you probably prefer a separate PR for that :P

@stof
Copy link
Member

stof commented May 23, 2017

@ro0NL the ContainerBuilder check does not solve anything in 3.2.
The issue was that we were missing deprecations when getting a private service which was already instantiated previously (due to being a dependency of another service you got earlier). This happens both for a ContainerBuilder and the dumped container.
Your check fixes the testsuite only because the testsuite does not do any such bad get calls on a dumped container (as most tests don't bother about dumping the container and then loading the dumped container to run assertions)

@ro0NL
Copy link
Contributor Author

ro0NL commented May 23, 2017

@stof im still a bit confused.. (ive reverted the containerbuilder checks).

moving the private check up (above checking for shared services), truly fixes your comments from #22801, i.e. https://github.com/symfony/symfony/pull/22866/files#diff-8aeba2b88d0347062017ee160a6beb49R408 fails otherwise.

imo. it needs a fix for 3.2 (properly deprecating private services), but im missing something regarding the container builder. AFAIK the issue is ContainerBuilder::get() => parent::get().

Updating this call to get() still triggers the deprecation for private service twig.extension.debug.stopwatch. Im curious whats the root issue here, or at least what the proper fix is for either 3.2 or 3.3.

@ro0NL
Copy link
Contributor Author

ro0NL commented May 23, 2017

Think this approach https://github.com/symfony/symfony/blob/v3.2.8/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php#L1488 needs to be done in container builder after compilation... ie. bypasssing get() internally for privates.

@nicolas-grekas
Copy link
Member

My 2 cts: ContainerBuilder should not care about private or not. Thus, calling parent::get() shouldn't matter because $this->privates should not be populated.
I don't know where things fail right now but if we stick to this policy and tests fail, then I'd say these tests are broken, nothing else.

@nicolas-grekas
Copy link
Member

For me,

should be removed, that's the bug.
ContainerBuilder is a bootstrap-stage thing, and private/public is a "compiled-container" only thing.

@ro0NL
Copy link
Contributor Author

ro0NL commented May 23, 2017

Agree. I figured it out ;) i tend to qualify it a bug though.. as my perception is compiled containerbuilder === compiled container === dumped compiled container, but for now we're good.

would be nice if ContainerBuilder::compile() returns a new (prepped) Container, that would allow to even further simplify (killing isCompiled and such), but that's quite a step though.

Squahsed&rebased. Ready for review :)

Sorry for the inconvenience, but this should do.

@stof
Copy link
Member

stof commented May 23, 2017

@nicolas-grekas currently, you can keep using a ContainerBuilder after compilation (which is what some projects do as they don't dump the container to the cache, for instance Behat)

@ro0NL
Copy link
Contributor Author

ro0NL commented May 23, 2017

may i recall #20643 :) as containerbuilder is open for anything keeping privates empty is in line with that, and so a bugfix.

but the behavior is still wrong, however currently it's half right, half wrong.

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.

👍

@stof
Copy link
Member

stof commented May 24, 2017

We discussed this with @nicolas-grekas and here is the summary of changes we would want to get:

  • add a hidden argument in getServiceIds to allow getting only public ids (i.e. opting in for the new behavior), and trigger a deprecation notice when getting the list with private ids in it (we have some existing code adding similar hidden arguments)
  • retarget the PR to 3.3 (due to the above previous deprecation)
  • try to keep the existing code filling the privates array in the ContainerBuilder, and debug test failures instead. As the 3.4 branch will allow using 4.0 deps, accessing private services in tests will be forbidden anyway. I suspect several failures are related to getServiceIds and so could be solved by the first point.

@ro0NL can you work on it ? Otherwise, I can take this PR over this evening to finish it

@ro0NL
Copy link
Contributor Author

ro0NL commented May 24, 2017

@stof i will finish it tonight :) so no changes for 3.2 right?

try to keep the existing code filling the privates array in the ContainerBuilder

so we dont allow fetching privates using containerbuilder right (after compiling). getServiceIds will be the least problem :) it needs to bypass get() for references etc. internally. ie. what the phpdumper does.

@ro0NL
Copy link
Contributor Author

ro0NL commented May 24, 2017

@stof Allow edits from maintainers is enabled.. so feel free to add commits though :)

@fabpot
Copy link
Member

fabpot commented May 24, 2017

Thank you @ro0NL.

@fabpot fabpot merged commit 4f683a9 into symfony:3.2 May 24, 2017
fabpot added a commit that referenced this pull request May 24, 2017
This PR was merged into the 3.2 branch.

Discussion
----------

[DI] Check for privates before shared services

| Q             | A
| ------------- | ---
| Branch?       | 3.2
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #22801 (comment), #22801 (comment)
| License       | MIT
| Doc PR        | symfony/symfony-docs#... <!--highly recommended for new features-->

cc @stof

Commits
-------

4f683a9 [DI] Check for privates before shared services
@nicolas-grekas
Copy link
Member

3.2 now merged into 3.3 so that next steps can happen on 3.3

@ro0NL ro0NL deleted the di/privates-3.2 branch May 24, 2017 14:48
This was referenced May 29, 2017
ostrolucky pushed a commit to ostrolucky/symfony that referenced this pull request Mar 25, 2018
This PR was merged into the 3.2 branch.

Discussion
----------

[DI] Check for privates before shared services

| Q             | A
| ------------- | ---
| Branch?       | 3.2
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | symfony#22801 (comment), symfony#22801 (comment)
| License       | MIT
| Doc PR        | symfony/symfony-docs#... <!--highly recommended for new features-->

cc @stof

Commits
-------

4f683a9 [DI] Check for privates before shared services
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants