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

[5.6] Adds bulk binding ability to service providers during registration #21961

Merged
merged 1 commit into from
Nov 10, 2017
Merged
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
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 @@ -5,6 +5,7 @@
use Mockery as m;
use PHPUnit\Framework\TestCase;
use Illuminate\Foundation\Application;
use Illuminate\Support\ServiceProvider;

class FoundationApplicationTest extends TestCase
{
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
{
//
}