-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[RFC][Console][DI] Simplify adding DI container to CLI apps #27479
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
Thanks for opening this 👍 |
By using symfony/skeleton, you're already able to trivially create a DI-enabled console app. What's the issue this would solve that isn't solved using the skeleton? |
The issue by creating a cli-kernel parallel to http-kernel is that this would create a parallel bundle system to register third-party extension points. That would be much worse for the ecosystem than having the HTTP layer around in your CLI project IMO. |
the article from @TomasVotruba is actually using HttpKernel (but then not reusing the built-in tag-based configuration for commands). @symfony/deciders a solution might be to move |
@nicolas-grekas You are right but @stof You are also right, but i was thinking about something that will just glue DI container and Console component. As i said, in this RFC i'm only interested in something that will just glue DI container and Console components. Maybe new component, maybe new class in DI/Console component - i don't know but i'm open for suggestions. IMO there is not sense in writing this integration in every CLI app because this integration is already written, but only for BTW having skeleton that will bootstrap CLI app, with Symfony components of course, would be very nice :) |
@PabloKowalczyk but if you don't have FrameworkBundle, you don't have any of the autoconfiguration rules for core classes (as FrameworkBundle is the one shipping them). So you would have to reconfigure it all. |
@stof > but if you don't have FrameworkBundle, you don't have any of the autoconfiguration rules for core classes (as FrameworkBundle is the one shipping them). So you would have to reconfigure it all. That's right. Framework bundle should be split to more decoupled parts. It seems like storage for "all other features". I don't want to be forced to use whole FrameworkBundle, just to be able to load Command services. So I rather load them in own Compiler in 5 lines. |
👍 for @stof proposal about moving the frameworkbundle application to the kernel.
@TomasVotruba we already moved compiler passes to their respective component, we try hard to break that statement as time goes by. |
So, any ideas how we can simplify adding DI container to |
Hi, quite new around, so if I'm doing this bad, just tell me, I'll be glad to improve myself ;) I'm doing some console apps and would love to see such functionality ! So here I'm trying to help :) So, from what I understood, we have a lot of configuration stuff coming from Another problem here, on my opinion, is that we should not mix framework-related configuration and components (console or any other component), since components are standalone and adding this configuration stuff will add dependencies. Instead of moving this configuration stuff, could we make it more flexible ? framework:
use_compiler_pass:
- console
- annotations That will filter what we have to use in This is probably not the best way to do it, but it will fulfill our purpose without having to change many stuff in DI and all configuration there is in On another hand, we could do like for AddConsoleCommandPass and handpick all CompilerPass we need to move. But that won't resolve the way to load them. By the way, I'll be glad to help for both options. |
There's also a partial issue of perception, the fact that Symfony can't genuinely tout a fat-free setup for CLI-only DI-enabled apps will hinder adoption rate. |
After a fresh Symfony skeleton install:
What looks useless at first sight for a backbone CLI app is:
|
|
I'm not saying they are not, I'm questioning if they are really necessary and can seem overkill for a backbone app. But in the end the skeleton is 15M and ~3K files, it's really not much to begin with. I feel like if you CLI app is big enough to justify a DI container like the Symfony one, those extra dependencies shouldn't be a blocker. I just irk a bit at the idea of depending on non-necessary dependencies but it's really nitpicking at this point |
What about extending |
There is a big issue that has been described by @stof above: this is seeking to create an alternative configuration system than the existing one based on bundles. Technically, you can create one for sure, but this also means that this would create an incompatible way of doing things. What I'd call a dead end for projects that take this direction - or a niche at least. A better vision for me is to standardize on the skeleton to start small, and have all of us extend it in the same way. This would allow composing/sharing features more easily in the community. Honestly, I don't see the issue with the frameworkbundle as a glue system. Of course, if we can move some logic to components - and if can further shrink the skeleton, why not. But I would really like we work on something that doesn't create an alternative configuration system for the sake of it and instead creates a standard way to wire features in the Symfony world, like SF4+Flex already started to do. |
Tbh, as long as http-foundation, http-kernel, and routing are out of the structure of a CLI only app, I'm happy, anything that gives us that is welcome. |
Size nor file count nor number components is the main issue. The main point from my side is teaching programmers bad programming habbits by overcoupling to the framework. This naturally leads to legacy code and "use everything we can because we can" instead of "is there a reason to do this" thinking. |
And light coupling to the framework doesn't mean don't use a framework. I feel like that's what you're implying here, which is wrong of course. You can build clean architecture on top of Symfony. And it's harder without actually. |
@nicolas-grekas did you saw my comment from an hour ago? |
I just did thanks for the ping. I don't know actually... |
Just got some idea, let's share, the idea is to create a For example, the Console component:
Like that, we can still manage every configuration from It will also contain all configuration related class that we actually store in |
Quick update. After some thinking, I think we should focus on component loading on Autoconfiguration being decoupled from (I still rebased my last PR since it's still a good thing to move theses and will try to take a look at how we could load components only when they're here) |
… Cache component (Korbeil) This PR was merged into the 4.2-dev branch. Discussion ---------- [FrameworkBundle] Moving Cache-related CompilerPass to Cache component | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | no | BC breaks? | yes | Deprecations? | yes | Tests pass? | yes | Fixed tickets | | License | MIT | Doc PR | Hi, first PR here 🎉 This is related to #27479 and a first work to move Cache-related CompilerPass out of `FrameworkBundle`, it allows to decouple part of the FrameworkBundle configuration classes. Since we didn't choosed a fixed directory organization to manage theses, I actually did same as in Bundles and used DependencyInjection folder. If we do choose to follow my [last comment directory organization proposal](symfony/symfony#27479 (comment)), I'll move theses CompilerPass to `Framework/DependencyInjection/Compiler` directory (nothing hard here). Thanks to @DanieleGBX that helped me checking this PR and gave me some good advices ! Here is a list of all CompilerPass I moved, with related component (I also moved related tests when they were present): - **Cache** - CacheCollectorPass - **Cache** - CachePoolClearerPass - **Cache** - CachePoolPass - **Cache** - CachePoolPrunerPass Commits ------- 53e7040829 moving Cache-related compiler pass from FrameworkBundle to Cache component
… Cache component (Korbeil) This PR was merged into the 4.2-dev branch. Discussion ---------- [FrameworkBundle] Moving Cache-related CompilerPass to Cache component | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | no | BC breaks? | yes | Deprecations? | yes | Tests pass? | yes | Fixed tickets | | License | MIT | Doc PR | Hi, first PR here 🎉 This is related to #27479 and a first work to move Cache-related CompilerPass out of `FrameworkBundle`, it allows to decouple part of the FrameworkBundle configuration classes. Since we didn't choosed a fixed directory organization to manage theses, I actually did same as in Bundles and used DependencyInjection folder. If we do choose to follow my [last comment directory organization proposal](symfony/symfony#27479 (comment)), I'll move theses CompilerPass to `Framework/DependencyInjection/Compiler` directory (nothing hard here). Thanks to @DanieleGBX that helped me checking this PR and gave me some good advices ! Here is a list of all CompilerPass I moved, with related component (I also moved related tests when they were present): - **Cache** - CacheCollectorPass - **Cache** - CachePoolClearerPass - **Cache** - CachePoolPass - **Cache** - CachePoolPrunerPass Commits ------- 53e7040829 moving Cache-related compiler pass from FrameworkBundle to Cache component
… Cache component (Korbeil) This PR was merged into the 4.2-dev branch. Discussion ---------- [FrameworkBundle] Moving Cache-related CompilerPass to Cache component | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | no | BC breaks? | yes | Deprecations? | yes | Tests pass? | yes | Fixed tickets | | License | MIT | Doc PR | Hi, first PR here 🎉 This is related to #27479 and a first work to move Cache-related CompilerPass out of `FrameworkBundle`, it allows to decouple part of the FrameworkBundle configuration classes. Since we didn't choosed a fixed directory organization to manage theses, I actually did same as in Bundles and used DependencyInjection folder. If we do choose to follow my [last comment directory organization proposal](#27479 (comment)), I'll move theses CompilerPass to `Framework/DependencyInjection/Compiler` directory (nothing hard here). Thanks to @DanieleGBX that helped me checking this PR and gave me some good advices ! Here is a list of all CompilerPass I moved, with related component (I also moved related tests when they were present): - **Cache** - CacheCollectorPass - **Cache** - CachePoolClearerPass - **Cache** - CachePoolPass - **Cache** - CachePoolPrunerPass Commits ------- 53e7040 moving Cache-related compiler pass from FrameworkBundle to Cache component
Okay, so step one happened, what's next on the line? |
Thanks for keeping that issue updated ;) Like I said in last comment, it wasn't really a first step, I started thinking it was but we don't really need to move theses to make I'll try to make a draft on how we could make |
I have the feeling that the discussion is deviating from the original issue. |
@chalasr I started moving stuff out of If you prefer I can open 2 other issues about decoupling passes from FrameworkBundle & about that loading issue. |
@chalasr I strongly disagree, I don't see any consensus established against allowing cli-only applications to be able to run using cached container without requiring the http layer. |
@Korbeil do you also plan to move code related to container building/caching out of |
I don't see how to provide more reusable code regarding container caching in a relevant way. |
Hello @PabloKowalczyk,
Yes, the idea is this one. Currently the We can see that the console Application only required the I propose a solution to this issue. The plan can be the following1. Create
|
As the version 4.2 is currently on stabilization process then if the idea is approved it will target the version 4.3 anyway in December. However, for experimentation purpose can I create a repository to support from 4.1 to current version (v4.1.7, I have a project that initiated the migration to Symfony4 and need this extraction to take profit of wonderful features provided by the DIC. cc @thePanz |
The experimental repository created there https://github.com/alquerci/kernel. |
As soon as the version 4.2 is released I will create the PR to add the @stof @nicolas-grekas Do you think that is required to create a dedicated RFC issue to expose motivations for this new component? See initial message #27479 (comment) for more details. |
This kernel component requires rewriting the whole ecosystem of bundles (to make them use the new component rather than the old one), which is a huge cost for the ecosystem. So yeah, discussing the motivation and the migration path and its cost is a must-have. |
@alquerci can we have a side-by-side comparison of the two kernels? |
@theofidry You can see the comparison there: v4.1.6...alquerci:add-kernel-component That's mainly a code extraction except for the @stof For bundles depending on the HttpKernel then there is no nothing to do for them until Symfony5. Where it will be just class namespace to rename. I agree that's better to have a dedicated issue. |
The dedicated issue has been created for the possibility of a Here we go #29275 |
I've just re-read this issue and some other related to the same. I don't think this is something we will ever do. Lately, we have decoupled things more. And Flex did part of the job. But, having big BC breaks is out of the equation with the maturity of the framework. We might do some small steps in this direction, but no big bang. I'm closing for these reasons. |
@fabpot could you recommend a more acceptable breakdown of this process, so we can get some momentum on those partial updates? |
Description
Hello, recently i've became the Crunz maintainer, after some review i've realized that Crunz doesn't use DI container - this means static calls and no way to write sensible test. In the meantime @TomasVotruba published some blog posts about adding DI container to CLI apps, as you can see there is no standard/recommended way to add DI to CLI app.
One approach is to copy some code from
HttpKernel
as i do in Crunz , but maintaining it is not nice nor easy. Nobody wants to reinvent the wheel.Another approach, as suggested in above blog post, is to use
Kernel
provded byhttp-kernel
package but i do not like this idea because it is weird to have full http support in CLI app :)IMO there are three ways to increase DX and easy development of CLI apps with DI container:
http-kernel
(andframework-bundle
) but for CLI. IMO this is the best way because developers can also have other features, out-of-the-box, likeautoconfigure
Symfony commands, easy access to container in test env and many others.Symfony\Component\Console\Application
to support DI natively. For example by decorator.DI
andConsole
.What do you think?
Thanks.
The text was updated successfully, but these errors were encountered: