-
-
Notifications
You must be signed in to change notification settings - Fork 314
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
[Translator] Mark the translator as nullable #2061
base: 2.x
Are you sure you want to change the base?
[Translator] Mark the translator as nullable #2061
Conversation
I'm not entirely sure this will solve the issue - I think it's possible |
The issue I originally had is defined in #1962 and indeed mentions that the Unless I'm making assumptions that I couldn't have made. I'd be willing to add new test cases, but I'm unsure how I can configure the when@test:
framework:
translator:
enabled: false When the |
Oh ok so nulloninvalid will pass null even if the service exists but doesn't implement the correct interface? I didn't realize this was the case. |
I just managed to test and verify it locally, unfortunately it doesn't fix it. Will see whether I can make a test scenario. Apparently the test scenario already tests this scenario, and indeed, the test is failing, but not in the checks, which is weird. |
Locally the test was indeed failing, not sure why the test was succeeding in the checks though. I've used union types to either allow null, allow TranslatorInterface, or allow the TranslatorBagInterface. This should hopefully fix the issues. |
I've reverted the original PR just to get the test suite passing. We've also made some adjustments to prevent regressions in the future. I still want the original issue fixed though but just wanted some time to properly fix the issue. |
Co-authored-by: Alexis Lefebvre <[email protected]>
Leverage union types to properly set up the configuration
528de23
to
a548681
Compare
No worries, its great that more pressing bugs are fixed faster, where my main guess is that most people don't disable the I've made this MR up-to-date with |
Wondering, if you disable the translator in test environment, could you disable the UXTranslator bundle too ? |
I think that might also work, yeah. But he main issue is, I think, that the To be fair, if they choose a different Translator, one that doesn't implement the TranslatorBagInterface, the Symfony-UX/TranslatorBundle also won't work or do what it is meant to do, so in those cases they could also choose to disable or not use the symfony-ux/translator package 😅. In either case, should there be a hard crash when the translations can't be dumped, or can it fail silently? I'd also be happy to make it a hard crash with a better error message (rather than 'Unable to create the cache warmer, expected an instance of |
This was a genuine question ;) So i checked the code of the FrameworkBundle and the documentation of translation Standard caseDocumentation
Service names / definitions
Based on
This mean the Identify TranslatorThe identity translator is configured when the translator is not enabled and used instead of the previous one. // A translator must always be registered (as support is included by
// default in the Form and Validator component). If disabled, an identity
// translator will be used and everything will still work as expected. And then you are right the translator is then not a TranslatorBag. But none of those component need to extract and list translations. They just need a TranslatorInterface. UXTranslatorUXTranslator need the original In the bundle extension, we define the CacheWarmer service and ask to inject the 'translator' service, so i'm thinking this is ok. The UX Translator bundle needs the And a file must be generated by the CacheWarmer or the front-end scripts will not be available / import map will not work. So...So i don't see how any solution other than "disable the UX Translator" could work here. I think the Same goes for any "silent" / ignoring "crashes" :/ We 100% could improve the DX and dependency injection configuration (maybe allow some enable/disable configuration here), and any suggestion here would be welcomed! 😄 I also agree we could add a paragraph in the documentation to explain both the requirements and their reasons. And probably one to explain how to solve it (whatever way we find). |
Oh and I found why the bug !! The code i suggested originally (from the if (!$container->has('translator')) {
return;
} The code we merged from the PR if (!$containerBuilder->hasDefinition('translator')) {
return false;
} This explains the bug in production and the hotfix: the This is not a critique, and I take full responsibility for it, as I did not check carefully enough. |
Oh, that's tricky as hell 😅 Good catch tho! |
Oh wow, didn't know that the Anyways, I could make throw an error in the constructor if the injected As the 'only' purpose of this bundle is to 'map' varous catalogues to JS files, I think that it's fair to assume that when the |
The injected Translator is type-hinted So you cannot do this in the constructor, as PHP would throw a TypeError before. |
We both learned something here :) |
So, what about accepting One thing I'm not sure of: if we disable the ux translator bundle, will asset mapper still try and load |
I really don't think this should be done at runtime.. better make some check during compiler no ? And not a big fan to expand the interface "just" to deal with injection problems to be honest... that does not feel like a clean way (but if we don't have much let's do this)
That's a good question. And we should be able to offer the "activation/deactivation" of the component like others (e.g. serializer, validator, workflows, etc.) |
The cache warmer certainly isn't on the hot path but I get your point. I'm not a fan either. |
So... Yes ! ... and No So to run API tests, unit tests, it would work. For functional test it probably would crash, and E2E logically too. Solutions So i don't really see any solution easy to implement and that would not break anything ... @Kocal any idea here ? |
By any chance, do you have a reproducer that can help me to work on? Thanks |
Apparently, the
translator
service is not available by default, so removing the cache warmer if the container doesn't have the definition breaks in newer versions. In order to allow this to still work when thetranslator
service is not defined, but there is atranslator
defined in the container, the behaviour is updated to benullOnInvalid
and in the warmup it verifies whether theTranslatorBagInterface
is null or not.I've added the
logger
to be able to add a warning when thetranslator
is not valid, which should not cause any issues.