-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
Description
TODO:
- Make container PSR-11 compatible.
- Introduce aliasing.
- Make it possible to use another PSR-11 compatible container.
Original issue:
This is related to my open PR(#14107) regarding PSR-11 compatibility, it is not the same however.
My PR is about making the Yii DI container usable in libraries that need a PSR-11 container.
This is to be a discussion about supporting other DI containers for usage within Yii.
Yii's DI container does some things that are out of the scope of PSR-11.
These are:
- Allowing to not only resolve dependencies for objects but also for callables.
- Allow configuring of objects via
->get(..., ..., ...)
. - Allow passing constructor arguments to the object that must be returned.
Note that (1) and (2) can be refactored to wrap any PSR-11 container instead of being part of a container itself. (3) however is a problem. Since $container->get()
will return the object there is no option to pass constructor arguments, for these cases we need to look for alternatives (which is why this is a 2.1 discussion).
There are currently 5 places in the Yii source code that pass constructor arguments to $container->get
via Yii::createObject
:
/base/Module.php[423]: $module = Yii::createObject($this->_modules[$id], [$id, $this]);
/base/Module.php[578]: $controller = Yii::createObject($this->controllerMap[$id], [$id, $this]);
/base/Module.php[638]: $controller = Yii::createObject($className, [$id, $this]);
/base/Controller.php[225]: return Yii::createObject($actionMap[$id], [$id, $this]);
/db/ActiveRecord.php[305]: return Yii::createObject(ActiveQuery::className(), [get_called_class()]);
These 5 calls instantiate (subclasses of) 3 classes:
yii\db\ActiveQuery
yii\base\Module
yii\base\Controller
In these specific cases the constructor arguments consist of public variables combined with a configuration array. The reason for these variables to be constructor arguments (they could also be configured via the configuration array) is that they are not optional but required.
I do not have a specific solution that I feel is best, but I think the above provides enough information to start a discussion.
tldr; if we want Yii to be able to use PSR-11 DI Containers we must:
- Decide how to handle constructor arguments outside DI
- Decide how to handle configuration outside DI