-
Notifications
You must be signed in to change notification settings - Fork 11.3k
[12.x] Allow Container to build Migrator
from class name
#55501
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
[12.x] Allow Container to build Migrator
from class name
#55501
Conversation
5c98758
to
36f55c1
Compare
@@ -82,6 +82,7 @@ protected function registerMigrator() | |||
|
|||
return new Migrator($repository, $app['db'], $app['files'], $app['events']); | |||
}); | |||
$this->app->bind(Migrator::class, fn ($app) => $app['migrator']); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would use Container@alias
instead of bind.
Besides saving a closure call, as Migrator::class
would be an alias to migrator
, and migrator
is a singleton, it would be automatically considered the same singleton.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe I tried it with a container alias, but because it's a singleton, $app->forget('migrator') still keeps the aliases version in memory.
https://github.com/laravel/framework/actions/runs/14579501724/job/40892877095
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is weird, maybe we need to call $this->getAlias($abstract)
when forgetting instances, much like is done when forgetting extenders:
framework/src/Illuminate/Container/Container.php
Lines 1555 to 1564 in b6a2af2
/** | |
* Remove a resolved instance from the instance cache. | |
* | |
* @param string $abstract | |
* @return void | |
*/ | |
public function forgetInstance($abstract) | |
{ | |
unset($this->instances[$abstract]); | |
} |
framework/src/Illuminate/Container/Container.php
Lines 1533 to 1542 in b6a2af2
/** | |
* Remove all of the extender callbacks for a given type. | |
* | |
* @param string $abstract | |
* @return void | |
*/ | |
public function forgetExtenders($abstract) | |
{ | |
unset($this->extenders[$this->getAlias($abstract)]); | |
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe that makes sense, however, that definitely belongs in a separate PR. Also potentially a breaking-change?
Most of the aliases to the string bindings are registered on the framework/src/Illuminate/Foundation/Application.php Lines 1614 to 1666 in b6a2af2
Shouldn't this alias also be registered there? |
Writing integration tests that rely on the migrator (such as testing a single migration) requires me to write:
Because otherwise, there's a binding resolution exception:
With this change, I will now be able to write tests like this: