diff --git a/src/Illuminate/Foundation/Application.php b/src/Illuminate/Foundation/Application.php index b0f3e38ae425..532f272db60c 100755 --- a/src/Illuminate/Foundation/Application.php +++ b/src/Illuminate/Foundation/Application.php @@ -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')) { @@ -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; diff --git a/tests/Foundation/FoundationApplicationTest.php b/tests/Foundation/FoundationApplicationTest.php index d403e4a0702a..740f53f7c255 100755 --- a/tests/Foundation/FoundationApplicationTest.php +++ b/tests/Foundation/FoundationApplicationTest.php @@ -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;