Skip to content

[RFC] Normalize root namespaces with DI #28006

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
ro0NL opened this issue Jul 19, 2018 · 9 comments
Closed

[RFC] Normalize root namespaces with DI #28006

ro0NL opened this issue Jul 19, 2018 · 9 comments

Comments

@ro0NL
Copy link
Contributor

ro0NL commented Jul 19, 2018

Description
For context see: symfony/symfony-docs#8403 and #24145

Nobody should prefix their service name by a \ IMHO.
-- nicolas-grekas

Currently only namespaced classes are detected as "class named services", i.e.

MyClass: ~ # doesnt work
\MyClass: ~ # doesnt work
\My\Class: ~ # doesnt work
My\Class: ~ # works

I propose to deprecate leading slashes in service ids. So you most likely end up with the working/last example in the future.

In case a class lives in the root namespace (first example) you'd get the error about a missing class attribute which we can easily explain: "root class named services" are not supported. Hence you must specify the class attribute. Which brings me to the next point;

Currently it's allowed to use both class: MyClass and class: \MyClass which causes some form of inconsistency, and makes $def->getClass() === MyClass::class error prone.

I propose to normalize leading slashes in a service its class attribute. Although i tend to prefer a deprecation here as well.

Thoughts?

@stof
Copy link
Member

stof commented Jul 19, 2018

root class named services are supported. What is not supported for them is the shortcut allowing to guess the class name from the id.

@ro0NL
Copy link
Contributor Author

ro0NL commented Jul 19, 2018

Correct. My reasoning is, if My\Class: ~ is described as a class named service then \MyClass: ~ is a root class named service :)

@nicolas-grekas
Copy link
Member

👎 on my side: there should be only one valid id. Normalization creates alternative ways and leads to issues. We deprecated lowercasing for this reason.

@ro0NL
Copy link
Contributor Author

ro0NL commented Jul 19, 2018

I propose to deprecate leading slashes in service ids

So yes, a leading \ would throw eventually. No normalization.

For the class: attribute normalization could work, but on 2nd thought i prefer a deprecation here as well. Exactly for the reasons you mentioned.

@nicolas-grekas
Copy link
Member

But if there are no technical reasons, deprecating for the sake of it is not worth it IMHO.

@ro0NL
Copy link
Contributor Author

ro0NL commented Jul 19, 2018

Fair, that's why I opened the RFC.

You're right, it's purely a DX issue. No technical reason.

In a way this error is just a weird side effect:

The definition for "\My\DateTime" has no class attribute,
and appears to reference a class or interface in the global namespace.
Leaving out the "class" attribute is only allowed for namespaced classes.

Nevertheless, we can settle by qualifying it a doc issue. Then i'll go back to symfony/symfony-docs#8403 :)

@nicolas-grekas
Copy link
Member

Sure, it's always worth asking, and that's just my 2cts :)

@javiereguiluz
Copy link
Member

@ro0NL thanks for starting this discussion. I disagree about "polluting" the Symfony Docs with these minor details. This can be solved with a great error message generated by Symfony when this happens.

The current error message is good, but I'd like to propose a minor reword to make it more clear what the developer must do to solve it:

The "\My\DateTime" service definition must include the "class" attribute because
it references a class or interface in the global PHP namespace. Leaving out the
"class" attribute is only allowed for namespaced classes.

nicolas-grekas added a commit that referenced this issue Jul 29, 2018
This PR was squashed before being merged into the 3.4 branch (closes #28057).

Discussion
----------

[DI] Improve class named servics error message

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no     <!-- see https://symfony.com/bc -->
| Deprecations? | no
| Tests pass?   | yes    <!-- please add some, will be required by reviewers -->
| Fixed tickets | #28006
| License       | MIT
| Doc PR        | symfony/symfony-docs#... <!-- required for new features -->

Commits
-------

61de060 [DI] Improve class named servics error message
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

No branches or pull requests

4 participants