Skip to content

Commit cba2b53

Browse files
committed
Merge branch 'bugfix/fix-loading-deferred-providers-for-binding-interfaces-and-implementations' of https://github.com/lprzybylek/framework into lprzybylek-bugfix/fix-loading-deferred-providers-for-binding-interfaces-and-implementations
2 parents 6979b11 + 1b6b99a commit cba2b53

File tree

2 files changed

+79
-1
lines changed

2 files changed

+79
-1
lines changed

src/Illuminate/Foundation/Application.php

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -769,11 +769,40 @@ public function make($abstract, array $parameters = [])
769769
{
770770
$abstract = $this->getAlias($abstract);
771771

772+
$this->loadDeferredProviderIfNeeded($abstract);
773+
774+
return parent::make($abstract, $parameters);
775+
}
776+
777+
/**
778+
* Load deferred provider if $abstract is deferred service and instance was not loaded.
779+
*
780+
* @param string $abstract
781+
*/
782+
private function loadDeferredProviderIfNeeded($abstract)
783+
{
772784
if ($this->isDeferredService($abstract) && ! isset($this->instances[$abstract])) {
773785
$this->loadDeferredProvider($abstract);
774786
}
787+
}
775788

776-
return parent::make($abstract, $parameters);
789+
/**
790+
* Resolve the given type from the container.
791+
*
792+
* (Overriding Container::resolve)
793+
*
794+
* @param string $abstract
795+
* @param array $parameters
796+
* @param bool $raiseEvents
797+
* @return mixed
798+
*/
799+
protected function resolve($abstract, $parameters = [], $raiseEvents = true)
800+
{
801+
$abstract = $this->getAlias($abstract);
802+
803+
$this->loadDeferredProviderIfNeeded($abstract);
804+
805+
return parent::resolve($abstract, $parameters, $raiseEvents);
777806
}
778807

779808
/**

tests/Foundation/FoundationApplicationTest.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,17 @@ public function testSingleProviderCanProvideMultipleDeferredServices()
172172
$this->assertSame('foobar', $app->make('bar'));
173173
}
174174

175+
public function testDeferredServiceIsLoadedWhenAccessingImplementationThroughInterface()
176+
{
177+
$app = new Application;
178+
$app->setDeferredServices([
179+
SampleInterface::class => InterfaceToImplementationDeferredServiceProvider::class,
180+
SampleImplementation::class => SampleImplementationDeferredServiceProvider::class,
181+
]);
182+
$instance = $app->make(SampleInterface::class);
183+
$this->assertEquals($instance->getPrimitive(), 'foo');
184+
}
185+
175186
public function testEnvironment()
176187
{
177188
$app = new Application;
@@ -473,6 +484,44 @@ public function register()
473484
}
474485
}
475486

487+
interface SampleInterface
488+
{
489+
public function getPrimitive();
490+
}
491+
492+
class SampleImplementation implements SampleInterface
493+
{
494+
private $primitive;
495+
496+
public function __construct($primitive)
497+
{
498+
$this->primitive = $primitive;
499+
}
500+
501+
public function getPrimitive()
502+
{
503+
return $this->primitive;
504+
}
505+
}
506+
507+
class InterfaceToImplementationDeferredServiceProvider extends ServiceProvider implements DeferrableProvider
508+
{
509+
public function register()
510+
{
511+
$this->app->bind(SampleInterface::class, SampleImplementation::class);
512+
}
513+
}
514+
515+
class SampleImplementationDeferredServiceProvider extends ServiceProvider implements DeferrableProvider
516+
{
517+
public function register()
518+
{
519+
$this->app->when(SampleImplementation::class)->needs('$primitive')->give(function () {
520+
return 'foo';
521+
});
522+
}
523+
}
524+
476525
class ApplicationFactoryProviderStub extends ServiceProvider implements DeferrableProvider
477526
{
478527
public function register()

0 commit comments

Comments
 (0)