-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
[4.0] Add deprecate messages for container access in the Factory #18894
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
[4.0] Add deprecate messages for container access in the Factory #18894
Conversation
|
So before we merge this we need to make a decision about what the "one true" global in the system is going to be. In Symfony it's the Kernel where you call |
|
I personally like the approach to have (a proxy to) the container in the application object. That way, there's architectural no global, just a main object. Since the application is started with something like $app->execute($request);the application can easily provide itself to other parts, so the container (content) can be accessed. |
|
Do we even need a global? The goal of our DI approach should be to eliminate it. On a long term stuff should be injected. Why do you want to have a global? |
|
So here's the thing. If we don't have something like Symfony's Kernel, which is in essence doing a lot of the architectural bootstrapping that we do throughout our |
|
as this is only a deprecation notice for something to be removed in 5 is there any reason not to merge it? |
|
Repeating my comment from last year |
Yeah. The current message is unclear and self contradicting. The annotation on In truth, the architectural issues pointed out in this thread need to be addressed so there is a proper plan surrounding this deprecation. Otherwise right now it is adding a deprecation without anything actionable for when the deprecated element should be removed or how its removal doesn't catastrophically break the CMS. |
|
@mbabker any suggestion for a better text?
|
And therein lies the root issue. Here's why I don't see a deprecation of Generally, in a PHP application that is built around some kind of core container ( As a side effect of that architectural concern, a lot of code (primarily in static helpers because you don't have the luxury of being in a class instance and you risk potentially leaking implementation details if you make all dependencies of a static something that has to be injected, #23779 highlights why that is an architectural concern) is calling There is still a lot of work that needs to be done to make dependency injection the preferred method of operation in the CMS over using a static factory and service location. Some intimate details include:
|
|
For now, what I would suggest doing is this:
|
|
@mbabker to speed up things, feel free to open a pr and I will close this one then. |
/**
* Get a container object
*
* Returns the global service container object, only creating it if it doesn't already exist.
*
* This method is only suggested for use in code whose responsibility is to create new services
* and needs to be able to resolve the dependencies, and should therefore only be used when the
* container is not accessible by other means. Valid uses of this method include:
*
* - A static `getInstance()` method calling a factory service from the container, see `Joomla\CMS\Toolbar\Toolbar::getInstance()` as an example
* - An application front controller loading and executing the Joomla application class, see the `cli/joomla.php` file as an example
* - Retrieving optional constructor dependencies when not injected into a class during a transitional
* period to retain backward compatibility, in this case a deprecation notice should also be emitted to
* notify developers of changes needed in their code
*
* This method is not suggested for use as a one-for-one replacement of static calls, such as
* replacing calls to `Factory::getDbo()` with calls to `Factory::getContainer()->get('db')`, code
* should be refactored to support dependency injection instead of making this change.
*
* @return Container
*
* @since 4.0
*/
public static function getContainer(): Container;That should be enough. Usually I'm not a fan of this verbose of doc blocks in the code, but since a lot of people like to argue nobody reads the documentation anyway this is the one time I'll bend on that opinion. As for the last example of a valid use, I'd say the |
|
Adapted the message with the suggestion from Michael. |
|
This works until there's a definitive long term plan in place (FWIW I fully support deprecating |
|
🚢 'd Thanks for figuring this out guys :) |
As discussed in #16918 the container functionality in the
Factoryexists only for the transition period, this pr adds the missing deprecated tags.I'm splitting #16918 into different pr's to be easier to review.