Skip to content

Commit

Permalink
Adds bulk binding ability to service providers during registration
Browse files Browse the repository at this point in the history
  • Loading branch information
dallincoons committed Nov 5, 2017
1 parent 44680ec commit ce7a23d
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/Illuminate/Foundation/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
54 changes: 54 additions & 0 deletions tests/Foundation/FoundationApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Illuminate\Tests\Foundation;

use Illuminate\Support\ServiceProvider;
use Mockery as m;
use PHPUnit\Framework\TestCase;
use Illuminate\Foundation\Application;
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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
{
//
}

0 comments on commit ce7a23d

Please sign in to comment.