-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[DI] Some Twig extensions work automatically, others don't #22849
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
Well, what we could do is having TwigBundle registering this extension automatically when the twig-extensions package is installed (but keeping it an optional dependency of the bundle). I think this would fit the Flex idea of activating things by default when the package is installed. |
The explanation we need to "push" (and is accurate) is that we're auto-registering & tagging things in
I like this! But, wouldn't we need to register all the extensions in that library then? That could be wasteful. Could we - as an alternative in Flex - add service configuration for all of the Twig extensions when |
Should we move this issue to the 3.4 milestone? |
I just changed to 3.4 milestone. If we're going to do anything in this repo, it would be a new feature to auto-register the Twig extensions in TwigBundle... so a new feature I think |
This PR was merged into the 1.5-dev branch. Discussion ---------- Add namespaced aliases This is a first step towards moving this lib to namespaces. It should fix symfony/symfony#22849 If it works well, the same strategy could be applied to Twig. I think adding runtime notices would make things more complex and is not needed. Commits ------- fe6f308 Add namespaced aliases
Twig extensions 1.5.0 (with the needed changes) is out now. |
It works, but you need to explicitly enable the extension: Twig\Extensions\IntlExtension: ~ |
Not sure if registering all extensions automatically is a good idea. Some filters/functions could collide with some created by the developer locally, and it adds some overhead. Moreover, Flex is really all about adding only what you need. What we could do is adding a recipe that adds a file for those configurations, all commented, to ease discovery. See symfony/recipes#173 |
+1 So, no changes to core needed. Any Twig extensions that you create in your |
I came there with Google and it seems to me, that's the good place to post the following. services:
twig.intl_extension:
class: Twig_Extensions_Extension_Intl
tags:
- name: twig.extension But I receive the following error
As this configuration is already done in the The only way is to keep only one declaration, but this will lead to an error if the Furthermore, if I do the same with services:
twig.text_extension:
class: Twig_Extensions_Extension_Text
tags:
- name: twig.extension It works! Even if declared on both app + bundle. |
A standalone bundle should not declare services that it is not providing itself. Your bundle should either degrade gracefully if the extension is not installed by the end user, or provide the services you use from them yourself (if needed by wrapping the originally provided extensions), or last resort depend hard on the TwigExtensions package and use a late-phase compiler pass to register the service only if the end user did not already do this on application level. |
I will need some time (to understand) how to implement the bundle to degrade gracefully, if the extension is not installed, as I am just using |
In fact my misunderstanding came because in the |
well, in project-level, you still provide some services (using them is not happening in this file). The difference is that you can also provide services for some third-party classes, if these classes don't have a bundle providing the service. |
1 year and 4 days later I'm facing another problem with Twig Extensions and by googling I came there again ;-) services:
_defaults:
public: false
autowire: true
autoconfigure: true
# Uncomment any lines below to activate that Twig extension
#Twig\Extensions\ArrayExtension: ~
Twig\Extensions\DateExtension: ~
Twig\Extensions\IntlExtension: ~
#Twig\Extensions\TextExtension: ~ where it works well. But on my production server, having the same file as it's in a Git repo, it can't find the
|
Do you have some bundle that is registered only in the |
Great! You should be right! I'll investigate it as I have the following in my "c975l/countlinescode-bundle": "^1",
"c975l/xliff-bundle": "^2",
"phpstan/phpstan": "^0",
"rector/rector": "^0",
"symfony/debug-pack": "*",
"symfony/maker-bundle": "^1",
"symfony/phpunit-bridge": "^4",
"symfony/profiler-pack": "*",
"symfony/test-pack": "*",
"symfony/web-server-bundle": "^4",
"symplify/easy-coding-standard": "^5" Is there a way, instead of looking in each composer.json, to know which bundle requires what? |
It's one of the bundle I've developed ;-) |
Uh oh!
There was an error while loading. Please reload this page.
In symfony/demo#562 we're updating the Symfony Demo app to 3.3.0 RC1 and all the new DI features. During the upgrade I saw this error:
The reason is that I removed the original service definition for the Intl Twig extension:
If I put the config back, everything works again. I think it's confusing that "auto-services" work for our own Twig extensions but not for Symfony's extensions. Is there anything we can do to improve this situation? Thanks
The text was updated successfully, but these errors were encountered: