Skip to content

[DI] Defaults to public=false in all service config files #22615

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 3, 2017

Conversation

nicolas-grekas
Copy link
Member

@nicolas-grekas nicolas-grekas commented May 2, 2017

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

This is what we call "eating your own dog food" :)
Made me realize that we need a tweak to the defaults<>ChildDefinition conflict we have now:
tags should be applied, and there should be no conflict when everything is set explicitly on the child definition.

}
if (!$service->hasAttribute($k)) {
throw new InvalidArgumentException(sprintf('Attribute "%s" on service "%s" cannot be inherited from "defaults" when a "parent" is set. Try moving your child definitions to a different file or defining this attribute explicitly.', $k, $service->getAttribute('id')));
}
Copy link
Member Author

@nicolas-grekas nicolas-grekas May 2, 2017

Choose a reason for hiding this comment

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

this block is the main and only behavior change of this PR (same on YamlFileLoader)

@@ -713,7 +713,7 @@ public function testAutoConfigureAndChildDefinitionNotAllowed()

/**
* @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
* @expectedExceptionMessage The service "child_service" cannot use the "parent" option in the same file where "defaults" configuration is defined as using both is not supported. Try moving your child definitions to a different file.
* @expectedExceptionMessage Attribute "autowire" on service "child_service" cannot be inherited from "defaults" when a "parent" is set. Try moving your child definitions to a different file or defining this attribute explicitly.
Copy link
Member

Choose a reason for hiding this comment

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

I would say: Move your child definitions to a different file or define this attribute explicitly.

Copy link
Member Author

Choose a reason for hiding this comment

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

updated (and test case added)

@@ -20,19 +22,19 @@
<argument>null</argument><!-- var_dumper.cli_dumper when debug.dump_destination is set -->
</service>

<service id="debug.dump_listener" class="Symfony\Component\HttpKernel\EventListener\DumpListener">
<service id="debug.dump_listener" class="Symfony\Component\HttpKernel\EventListener\DumpListener" public="true">
Copy link
Member

Choose a reason for hiding this comment

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

it would be great to be able to change this service to become private in 4.0 (as event listeners can now be private).
@nicolas-grekas I thnk a necessary feature for 3.4 would be a way to mark a service as "wanna-be-private", which would trigger a deprecation warning when getting it from the container through get, but not when injecting it inside another service (unlike deprecated services). Such feature would allow any bundle out there to provide gradual migration to private services (warning people if they try to access internal services that will become private)

Copy link
Member Author

Choose a reason for hiding this comment

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

100% agree, and this PR is on that path :)
No need for any new feature as there is already a trick to do the deprecation, see:

<service id="deprecated.form.registry" class="stdClass">

So, we should just turn these services as private, then register them in a public dummy registry, that will prevent inlining and will thus make them "public" (but in a deprecated manner).

Copy link
Member

Choose a reason for hiding this comment

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

no, the form types are deprecating the services entirely, because they won't be needed anymore in 4.0 (form types without any dependencies don't need to be registered explicitly anymore).

But we might have a case where we want to deprecate the runtime retrieval of services (to make them private) without deprecating the service itself (as you will want to keep it as a dependency of other services), which is what we need for listeners.

And making them public in a deprecated manner works for Symfony services in 3.4.x. but it does not work for the future of Symfony, because your proposal relies on the BC layer for accessing non-inlined private services. But Symfony 4 will not make such services public at all, even when not inlined, and so you cannot have a similar BC layer for the migration to private services.

Btw, if you want to be sure that the services stay public, you must create 2 dummy registries. If you create only one, the service may end up being inlined inside the dummy registry if other references get removed by some optimizations.

Copy link
Member Author

@nicolas-grekas nicolas-grekas May 3, 2017

Choose a reason for hiding this comment

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

The form types I linked are the ones that are not deprecated because they need special instantiation.
You're right that this trick is valid only on 3.4. But maybe 3.4 is the last one that will have public services? So if using the trick is enough for 3.4, then maybe better not to work on a new feature for nothing?

Anyway, that's an interesting discussion, but should not be hidden in this PR :)

@@ -23,9 +23,12 @@
},
"require-dev": {
"symfony/config": "~2.8|~3.0",
"symfony/dependency-injection": "~2.8|~3.0",
"symfony/dependency-injection": "~3.3",
Copy link
Member

Choose a reason for hiding this comment

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

shouldn't it be a normal requirement ? The bundle is all about defining services anyway.

Copy link
Member Author

Choose a reason for hiding this comment

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

In fact, DebugBundle can and is used standalone on Silex, see
https://packagist.org/packages/jeromemacias/silex-debug

Copy link
Member

Choose a reason for hiding this comment

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

hmm indeed, they get it to get the template for the profiler, which is not in WebProfilerBundle unlike other collectors

@@ -18,27 +20,27 @@
</argument>
</call>
</service>
<service id="Doctrine\Common\Annotations\Reader" alias="annotations.reader" public="false" />
<service id="Doctrine\Common\Annotations\Reader" alias="annotations.reader" />
Copy link
Member

Choose a reason for hiding this comment

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

@nicolas-grekas this autowiring alias should point to annotation_reader, which is the public name for it (even though it is an alias), so that people decorating the public name still get it applied in case of autowiring.

@@ -5,6 +5,8 @@
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

<services>
<defaults public="false" />

<!-- DataCollector -->
<service id="data_collector.cache" class="Symfony\Component\Cache\DataCollector\CacheDataCollector">
Copy link
Member

Choose a reason for hiding this comment

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

this changes the visibility of this collector. Is it expected ?

Copy link
Member Author

Choose a reason for hiding this comment

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

yes, the service does not need to be public

@@ -66,6 +66,7 @@
"phpunit/phpunit": "<4.8.35|<5.4.3,>=5.0",
"symfony/asset": "<3.3",
"symfony/console": "<3.3",
"symfony/dependency-injection": "<=3.3-beta1",
Copy link
Member

Choose a reason for hiding this comment

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

As we have a requirement on it, just update the lower bound of the requirement. It is more efficient in the composer solver than adding a conflict rule.

@@ -44,6 +44,7 @@
"twig/twig": "~1.28|~2.0"
},
"conflict": {
"symfony/dependency-injection": "<=3.3-beta1",
Copy link
Member

Choose a reason for hiding this comment

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

update the requirement lower bound instead

@nicolas-grekas nicolas-grekas force-pushed the di-defaults branch 3 times, most recently from ff6f36a to 259a606 Compare May 3, 2017 15:52
@nicolas-grekas
Copy link
Member Author

PR should be ready: comments addressed, and integration test case from Ryan added.

@weaverryan
Copy link
Member

I LOVE the idea of eating our own dog food.

👍 for the minor ChildDefinition -> _defaults behavior change. You can see the new behavior by comparing main.yml to expected.yml in the integration test: https://github.com/symfony/symfony/pull/22615/files#diff-e36b91c9d48d3617ee8a401577fcc603. Having a child definition in the same file as _defaults wasn't allowed at ALL anymore. Now it is, but only if you explicitly set any keys from _defaults on the child (i.e. we force you to override _defaults values (other than tags) so that there is no ambiguity).

And 👍 for the changes in general. I compiled an XML of the container on master versus this branch. They are identical, other than data_collector.cache, which was purposefully changed from public to private: https://gist.github.com/weaverryan/353a79c0e310b59d5eca9c922bc5cf5f

@fabpot
Copy link
Member

fabpot commented May 3, 2017

Thank you @nicolas-grekas.

@fabpot fabpot merged commit 0656284 into symfony:master May 3, 2017
fabpot added a commit that referenced this pull request May 3, 2017
…(nicolas-grekas)

This PR was merged into the 3.3-dev branch.

Discussion
----------

[DI] Defaults to public=false in all service config files

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

This is what we call "eating your own dog food" :)
Made me realize that we need a tweak to the defaults<>ChildDefinition conflict we have now:
tags should be applied, and there should be *no* conflict when everything is set *explicitly* on the child definition.

Commits
-------

0656284 [DI] Defaults to public=false in all service config files
@nicolas-grekas nicolas-grekas deleted the di-defaults branch May 4, 2017 12:17
fabpot added a commit that referenced this pull request May 13, 2017
…ublic (ogizanagi)

This PR was merged into the 3.3-dev branch.

Discussion
----------

[TwigBundle] service workflow.twig_extension should stay public

| Q             | A
| ------------- | ---
| Branch?       | master (3.3)
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | N/A
| License       | MIT
| Doc PR        | N/A

While it's not really required for this service to be public AFAIK, it was not made private when introducing it in 3.2. Which means it should stay public in upper branches. But [since we now default to `public: false`](#22615) in every service config file, it's currently private in 3.3.

I had a quick look on commits merged after this PR and didn't find any other similar case.

Commits
-------

5d07c6a [TwigBundle] service workflow.twig_extension should stay public
@fabpot fabpot mentioned this pull request May 17, 2017
@alcaeus
Copy link
Contributor

alcaeus commented May 25, 2017

@nicolas-grekas this change seems to have introduced a rather annoying deprecation warning:

Checking for the existence of the "debug.file_link_formatter" private service is deprecated since Symfony 3.2 and won't be supported anymore in Symfony 4.0: (383x)

This seems to come from the DebugBundle, where the service is injected in a method call using the on-invalid=ignore flag:

        <service id="var_dumper.html_dumper" class="Symfony\Component\VarDumper\Dumper\HtmlDumper">
            <argument>null</argument>
            <argument>%kernel.charset%</argument>
            <argument>0</argument> <!-- flags -->
            <call method="setDisplayOptions">
                <argument type="collection">
                    <argument key="fileLinkFormat" type="service" id="debug.file_link_formatter" on-invalid="ignore"></argument>
                </argument>
            </call>
        </service>

Apparently, when the compiled container is dumped to a file, this causes a $container->has call on the private service.

    protected function getTwigService()
    {
        // ...

        $d = new \Symfony\Component\VarDumper\Dumper\HtmlDumper(NULL, 'UTF-8', 0);
        if ($this->has('debug.file_link_formatter')) {
            $d->setDisplayOptions(array('fileLinkFormat' => $b));
        }

        // ...
    }

Should the debug.file_link_formatter service be made public or are you intending to ship 3.3 with this deprecation?

@nicolas-grekas
Copy link
Member Author

See #22912

@alcaeus
Copy link
Contributor

alcaeus commented May 25, 2017

Ah, my search didn't turn up any results, so I wasn't aware if this was being tracked. Thanks!

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