#14832 - Add the rebinder mechanism to DI#14834
#14832 - Add the rebinder mechanism to DI#14834TimurFlush wants to merge 31 commits intophalcon:4.1.xfrom TimurFlush:rebinder-4.1.x
Conversation
…ve a service #14832
This reverts commit e5da602.
|
Hmmm, why exactly we need this? I guess something new is always nice but I don't feel it, do you maybe have some examples of this feature existing in other frameworks or containers? |
Yes. Laravel for example. |
|
Oh, finally |
|
In general I would like to see any real use case. Right now there is just example how to use it but nothing about why I should use it. |
|
|
For example: index.php: $di = new Di();
//common DI
$di->setShared('volt', function () use ($di) {
$view = new \Phalcon\Mvc\View\Simple();
$view->setViewsDir('/path/to/common/template/directory');
return $view;
});AdminModule.php class Module implements Phalcon\Mvc\ModuleDefinitionInterface
{
public function registerServices($di)
{
// this will be done without resolving
$di->rebind('view', function ($di, \Phalcon\Mvc\View\Simple $view) {
$view->setViewsDir('/path/to/admin/templates/directory');
});
}
} |
|
I've run into a perfect use case for this today. So I have a shared A long time ago, I wrote a bunch of business logic that relied upon this user service. Now we have new features/tools that require CLI tasks that also need to be able to execute this same business logic. The trouble is, these CLI tasks need to work across multiple users at a time, not just one user per process like the web application. Naturally, you'd assume you could just create a new method that does something like this. public static function setUser(int $user_id): void
{
$app = self::getInstance();
$app->getDI()->setShared('user', function () use ($user_id) {
return UserModel::findFirstById($user_id);
});
}However, this doesn't reset the DI's I've also only just gotten this far and read the code for this PR. I think calling |
|
Thank you @TimurFlush but I have to close this for now as we won't be able to include this in 4.1 |
Hello!
In raising this pull request, I confirm the following:
Small description of change:
Q1: What does that mean?
A1: This means that already existing services in the container can be modified.
Q2: Are there any differences in modification between shared and non shared services?
A2: Yes.
Non shared services can be changed even after they have been resolved.
Shared services cannot be changed even after they have been resolved. That is, when creating a shared service, we polls all registered binders and passes this shared service to them. Once a shared service has passed into the container cache, it can no longer be modified by the binders.
Q3: How it differs from the chain of methods ->remove(), ->get(), ->set().
A3: My implementation does not create/delete services, but only adds "handlers" that will be called when the service is created. In this way, we can avoid additional overhead.
Q4: Will it be useful in real projects
Q4: Yes.
Q5: How can I use this?
Q5: Very simply.
PersistentJsonResponseServiceProvider.php
...I will not describe how to register a provider...
IndexController.php
Thanks
P.S I apologize for my English.