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

[6.x] Fix loading deferred providers for binding interfaces and implementations #31629

Merged
Show file tree
Hide file tree
Changes from 2 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
7 changes: 4 additions & 3 deletions src/Illuminate/Container/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ protected function getClosure($abstract, $concrete)
return $container->build($concrete);
}

return $container->resolve(
return $container->make(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The make method is just an alias of resolve. This can't solve anything really.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It solves the issue. Try checking out previous commit where test was added. Resolve is an alias of make in Illuminate\Container\Container, but not in Illuminate\Foundation\Application.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@driesvints it actually could in the context of an application because the Foundation Application overrides make to do the deferred provider stuff.

$concrete, $parameters, $raiseEvents = false
);
};
Expand Down Expand Up @@ -620,13 +620,14 @@ public function makeWith($abstract, array $parameters = [])
*
* @param string $abstract
* @param array $parameters
* @param bool $raiseEvents
* @return mixed
*
* @throws \Illuminate\Contracts\Container\BindingResolutionException
*/
public function make($abstract, array $parameters = [])
public function make($abstract, array $parameters = [], $raiseEvents = true)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changing the method signature here is unfortunately a breaking change.

{
return $this->resolve($abstract, $parameters);
return $this->resolve($abstract, $parameters, $raiseEvents);
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/Illuminate/Contracts/Container/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,12 @@ public function flush();
*
* @param string $abstract
* @param array $parameters
* @param bool $raiseEvents
* @return mixed
*
* @throws \Illuminate\Contracts\Container\BindingResolutionException
*/
public function make($abstract, array $parameters = []);
public function make($abstract, array $parameters = [], $raiseEvents = true);

/**
* Call the given Closure / class@method and inject its dependencies.
Expand Down
5 changes: 3 additions & 2 deletions src/Illuminate/Foundation/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -763,17 +763,18 @@ public function registerDeferredProvider($provider, $service = null)
*
* @param string $abstract
* @param array $parameters
* @param bool $raiseEvents
* @return mixed
*/
public function make($abstract, array $parameters = [])
public function make($abstract, array $parameters = [], $raiseEvents = true)
{
$abstract = $this->getAlias($abstract);

if ($this->isDeferredService($abstract) && ! isset($this->instances[$abstract])) {
$this->loadDeferredProvider($abstract);
}

return parent::make($abstract, $parameters);
return parent::make($abstract, $parameters, $raiseEvents);
}

/**
Expand Down
49 changes: 49 additions & 0 deletions tests/Foundation/FoundationApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,17 @@ public function testSingleProviderCanProvideMultipleDeferredServices()
$this->assertSame('foobar', $app->make('bar'));
}

public function testDeferredServiceIsLoadedWhenAccessingImplementationThroughInterface()
{
$app = new Application;
$app->setDeferredServices([
SampleInterface::class => InterfaceToImplementationDeferredServiceProvider::class,
SampleImplementation::class => SampleImplementationDeferredServiceProvider::class,
]);
$instance = $app->make(SampleInterface::class);
$this->assertEquals($instance->getPrimitive(), 'foo');
}

public function testEnvironment()
{
$app = new Application;
Expand Down Expand Up @@ -473,6 +484,44 @@ public function register()
}
}

interface SampleInterface
{
public function getPrimitive();
}

class SampleImplementation implements SampleInterface
{
private $primitive;

public function __construct($primitive)
{
$this->primitive = $primitive;
}

public function getPrimitive()
{
return $this->primitive;
}
}

class InterfaceToImplementationDeferredServiceProvider extends ServiceProvider implements DeferrableProvider
{
public function register()
{
$this->app->bind(SampleInterface::class, SampleImplementation::class);
driesvints marked this conversation as resolved.
Show resolved Hide resolved
}
}

class SampleImplementationDeferredServiceProvider extends ServiceProvider implements DeferrableProvider
{
public function register()
{
$this->app->when(SampleImplementation::class)->needs('$primitive')->give(function () {
return 'foo';
});
}
}

class ApplicationFactoryProviderStub extends ServiceProvider implements DeferrableProvider
{
public function register()
Expand Down