diff --git a/src/Illuminate/Foundation/Application.php b/src/Illuminate/Foundation/Application.php index 2f46dc08555d..0279df4276b1 100755 --- a/src/Illuminate/Foundation/Application.php +++ b/src/Illuminate/Foundation/Application.php @@ -586,6 +586,18 @@ public function register($provider, $options = [], $force = false) $provider->register(); } + if (property_exists($provider, 'bind')) { + foreach ($provider->bind as $abstract => $concrete) { + $this->bind($abstract, $concrete); + } + } + + if (property_exists($provider, 'singletons')) { + foreach ($provider->singletons as $abstract => $concrete) { + $this->singleton($abstract, $concrete); + } + } + $this->markAsRegistered($provider); // If the application has already booted, we will call this boot method on diff --git a/tests/Foundation/FoundationApplicationTest.php b/tests/Foundation/FoundationApplicationTest.php index b29924fba0ff..a181cb163e83 100755 --- a/tests/Foundation/FoundationApplicationTest.php +++ b/tests/Foundation/FoundationApplicationTest.php @@ -5,6 +5,7 @@ use Mockery as m; use PHPUnit\Framework\TestCase; use Illuminate\Foundation\Application; +use Illuminate\Support\ServiceProvider; class FoundationApplicationTest extends TestCase { @@ -37,6 +38,30 @@ public function testServiceProvidersAreCorrectlyRegistered() $this->assertTrue(in_array($class, $app->getLoadedProviders())); } + public function testClassesAreBoundWhenServiceProviderIsRegistered() + { + $app = new Application; + $provider = new ServiceProviderForTestingThree($app); + $app->register($provider); + + $this->assertTrue(in_array(get_class($provider), $app->getLoadedProviders())); + + $this->assertInstanceOf(ConcreteClass::class, $app->make(AbstractClass::class)); + } + + public function testSingletonsAreCreatedWhenServiceProviderIsRegistered() + { + $app = new Application; + $provider = new ServiceProviderForTestingThree($app); + $app->register($provider); + + $this->assertTrue(in_array(get_class($provider), $app->getLoadedProviders())); + + $instance = $app->make(AbstractClass::class); + + $this->assertSame($instance, $app->make(AbstractClass::class)); + } + public function testServiceProvidersAreCorrectlyRegisteredWhenRegisterMethodIsNotPresent() { $provider = m::mock('Illuminate\Support\ServiceProvider'); @@ -259,3 +284,32 @@ public function register() }); } } + +class ServiceProviderForTestingThree extends ServiceProvider +{ + public $bind = [ + AbstractClass::class => ConcreteClass::class, + ]; + + public $singletons = [ + AbstractClass::class => ConcreteClass::class, + ]; + + public function register() + { + } + + public function boot() + { + } +} + +abstract class AbstractClass +{ + // +} + +class ConcreteClass extends AbstractClass +{ + // +}