-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[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
Conversation
d323232
to
d80354d
Compare
} | ||
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'))); | ||
} |
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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)
d80354d
to
78edb66
Compare
@@ -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"> |
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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", |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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" /> |
There was a problem hiding this comment.
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"> |
There was a problem hiding this comment.
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 ?
There was a problem hiding this comment.
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", |
There was a problem hiding this comment.
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", |
There was a problem hiding this comment.
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
ff6f36a
to
259a606
Compare
PR should be ready: comments addressed, and integration test case from Ryan added. |
259a606
to
e2fe3f5
Compare
e2fe3f5
to
0656284
Compare
I LOVE the idea of eating our own dog food. 👍 for the minor ChildDefinition -> And 👍 for the changes in general. I compiled an XML of the container on |
Thank you @nicolas-grekas. |
…(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
…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
@nicolas-grekas this change seems to have introduced a rather annoying deprecation warning:
This seems to come from the DebugBundle, where the service is injected in a method call using the
Apparently, when the compiled container is dumped to a file, this causes a
Should the |
See #22912 |
Ah, my search didn't turn up any results, so I wasn't aware if this was being tracked. Thanks! |
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.