New in Symfony 4.1: Hidden services

Contributed by
Nicolas Grekas
in #26921.

In Symfony 3.4 we made all Symfony services private by default. This is generally better and makes applications more robust (as explained in the previous post) but it also has some drawbacks.

The debug:container command for example hides the private services by default, so it's no longer useful to list the services available in your application. That's why in Symfony 4.1 we've changed the debug:container command to show the private services by default and we've also deprecated the --show-private option.

Independently from being public or private, sometimes it's useful to hide some services from the debug:container listing because they are not intended for being used by the developers. In those cases, you can add a dot character (.) at the beginning of the service ID to turn it into a "hidden service".

Hidden services behave like the rest of services, except for the fact that they are not included by default in the output of debug:container, so you must add the new --show-hidden option to show them:

1
$ ./bin/console debug:container --show-hidden

We are already using this feature to hide some internal Symfony services created by the Lock component.

Comments

What are the rules that determine whether a service is hidden or not? Is it only when the name starts with a dot? And if yes, why are these services hidden? :)
@Alex that's right. If the service ID starts with '.' then it's hidden. Otherwise it's not. Why are they hidden? Each developer will decide to do that in their own app for their own reasons. In the case of Symfony, we hide some services that are not intended to be used by end users.
@Alexandre:

> Is it only when the name starts with a dot?

Yes, it's explained in this blog post

> And if yes, why are these services hidden? :)

I will quote this blog post:

>> because they are not intended for being used by the developers
Was there a particular reason to go with a "." prefix rather than a "hidden: true" option?
@Jaik I agree. We've created an issue to discuss about this: https://github.com/symfony/symfony/issues/27284
Thank you Javier, I am totally with you Jaik, it is very implicit
Services concept looks kinda more and more complicated. Talking outside 'debug:container' command, will those 'hidden' service have some special meaning?
This feature has two use cases it has been created for: cleaning up the output of the debug:container command, and cleaning up the service ids that might be suggested in autowiring errors. This means we won't have to care about using this daily. You can mostly forget about it. Internally, we did hide only the identifiers that are generated using hashes, which look like garbage already.
To me, the most important news in this blog post is not that you can hide your identifiers. It's mostly that we did remove some noise from the two output. \o/
I guess it would be helpful if debug:container command shows a note that there are N hidden services in addition to the presented list. Such a note may spare us some confusion when locating a bug or when a new developer is getting up to speed with a project.
@Josef but hidden services shouldn't be used by developers.
Upvote for deprecating "--show-private" because it was annoying to usually add this option.
After wtf effect has passed the idea of hidden services defined by dot seems a good improvement. Thanks
Login with SensioLabsConnect to post a comment