-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[DI][DX] Optional class for named services DX issue #21380
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
Comments
IMHO, there are two no-gos:
Which means I'm leaning towards answering "no, we can't" to your "Should we care?" question. That being said, what about catching the |
It'll indeed be a step forward, but how would you proceed in order to achieve that? 😕 The two possibilities I see are:
Both solutions looks complex for this very specific DX regression, and moreover are still runtime exceptions, while it used to throw at compil time before :/ |
@ogizanagi what about adding a leading backslash and removing it when registering the service? i.e. forcing: services:
\bar: ~ but behind the scene remove the leading backslash so that Alternatively we could make the class mandatory if no backslash if found at all in the service name: services:
bar: ~ # throw an error as not no `class` property has been found an `bar` is ambigus` |
Thanks @theofidry for these suggestions.
Could be a solution. However, as mentioned earlier, I think we should keep the ability to do: $container->register(\ExistingClassFromTheGlobalNamespace::class) using the PHP format. So either we don't support the above, either that'll require moving the logic from passes to file loaders :/
Yes, I think it's better to consider that registering a class from the global namespace this way is a limitation and throw. But I'm afraid everyone will disagree. 😄 |
I also prefer the second option, less magics and I think it's an reasonable limitation. |
I fact, why not, with one more step: let's not map id to class when no namespace is found. |
Sure, I'll do it. |
Keep me in the loop when you open the PR if I miss it please. I don't think
I'll be of a big help, but i would love to see the follow up still ;)
…On Sun, 29 Jan 2017 at 15:25, Maxime Steinhausser ***@***.***> wrote:
Sure, I'll do it.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#21380 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AE76gdqB7wAN5w9QUATIO0GMYk-1pTVEks5rXKE9gaJpZM4LraTL>
.
|
@theofidry : It's already opened 😄 See #21453. |
…anagi) This PR was merged into the 3.3-dev branch. Discussion ---------- [DI][DX] Do not map id to class for global classes | Q | A | ------------- | --- | Branch? | master | Bug fix? | yesish | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #21380 | License | MIT | Doc PR | N/A Using a global classname as service id without specifying the definition class attribute won't work anymore after this, in the benefit of properly throwing an exception at compilation time for a misconfigured service. Service ids could previously be wrongly interpreted as a class name. So: ```yml services: app_bar: arguments: ['foo'] ``` will now properly result into: > Fatal error: Uncaught Symfony\Component\DependencyInjection\Exception\RuntimeException: The definition for "app_bar" has no class. at compilation time. Commits ------- bb87030 [DI][DX] Do not map id to class for global classes
Since #21133, the
class
attribute isn't required anymore when using a classname as service id:However, due to the current implementation, the following is considered valid too:
The
bar
key is considered valid, and will be used as theDefinition
class, despite there is nobar
class in the project.Which means no compile-time exception. You'll only get:
at runtime, when trying to use the service.
Previously, you got an error at compilation time:
I think it's a DX regression, especially for someone not being aware of this behavior.
But the fact is that, looking at the tests, it seems expected.
In #20943 (comment), I suggested to enforce a leading backslash for global namespace classes, ie:
but that's not something to consider, given one of the main benefits of declaring a service this way, is to call it using
$container->get(\bar::class)
. Which doesn't resolves to a FQCN with a leading backslash.So, unless we move the service id as definition class logic inside each loaders, it's probably a no-go...
First question: Should we care?
Then, what are the possible solutions ?
In my experience, most services ids are lowercased (despite the fact case doesn't matter until [DI] Deprecate case insentivity of service identifiers #21193), whereas classnames are mainly capitalized (despite there is no recommendation about it in PSR-0/PSR-4 AFAIK), so it'll prevent WTF moments for a majority and looks sensible to me.
The text was updated successfully, but these errors were encountered: