Skip to content
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

[11.x] Simple scoped bindings container registration #52481

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/Illuminate/Foundation/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -867,7 +867,7 @@ public function register($provider, $force = false)

$provider->register();

// If there are bindings / singletons set as properties on the provider we
// If there are (scoped) bindings / singletons set as properties on the provider we
// will spin through them and register them with the application, which
// serves as a convenience layer while registering a lot of bindings.
if (property_exists($provider, 'bindings')) {
Expand All @@ -876,6 +876,14 @@ public function register($provider, $force = false)
}
}

if (property_exists($provider, 'scopedBindings')) {
foreach ($provider->scopedBindings as $key => $value) {
$key = is_int($key) ? $value : $key;

$this->scoped($key, $value);
}
}

if (property_exists($provider, 'singletons')) {
foreach ($provider->singletons as $key => $value) {
$key = is_int($key) ? $value : $key;
Expand Down
28 changes: 28 additions & 0 deletions tests/Foundation/FoundationApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,34 @@ public function testClassesAreBoundWhenServiceProviderIsRegistered()
$this->assertNotSame($instance, $app->make(AbstractClass::class));
}

public function testScopedBindingsAreCreatedWhenServiceProviderIsRegistered()
{
$app = new Application;
$app->register($provider = new class($app) extends ServiceProvider
{
public $scopedBindings = [
NonContractBackedClass::class,
AbstractClass::class => ConcreteClass::class,
];
});

$this->assertArrayHasKey(get_class($provider), $app->getLoadedProviders());

$instance = $app->make(AbstractClass::class);

$this->assertInstanceOf(ConcreteClass::class, $instance);
$this->assertSame($instance, $app->make(AbstractClass::class));
$app->forgetScopedInstances();
$this->assertNotSame($instance, $app->make(AbstractClass::class));

$instance = $app->make(NonContractBackedClass::class);

$this->assertInstanceOf(NonContractBackedClass::class, $instance);
$this->assertSame($instance, $app->make(NonContractBackedClass::class));
$app->forgetScopedInstances();
$this->assertNotSame($instance, $app->make(NonContractBackedClass::class));
}

public function testSingletonsAreCreatedWhenServiceProviderIsRegistered()
{
$app = new Application;
Expand Down