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

[8.x] Add before resolving callbacks #35228

Merged
merged 3 commits into from Nov 16, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
71 changes: 71 additions & 0 deletions src/Illuminate/Container/Container.php
Expand Up @@ -105,6 +105,13 @@ class Container implements ArrayAccess, ContainerContract
*/
protected $reboundCallbacks = [];

/**
* All of the global before resolving callbacks.
*
* @var \Closure[]
*/
protected $globalBeforeResolvingCallbacks = [];

/**
* All of the global resolving callbacks.
*
Expand All @@ -119,6 +126,13 @@ class Container implements ArrayAccess, ContainerContract
*/
protected $globalAfterResolvingCallbacks = [];

/**
* All of the before resolving callbacks by class type.
*
* @var array[]
*/
protected $beforeResolvingCallbacks = [];

/**
* All of the resolving callbacks by class type.
*
Expand Down Expand Up @@ -667,6 +681,10 @@ protected function resolve($abstract, $parameters = [], $raiseEvents = true)
{
$abstract = $this->getAlias($abstract);

if ($raiseEvents) {
$this->fireBeforeResolvingCallbacks($abstract, $parameters);
}

$concrete = $this->getContextualConcrete($abstract);

$needsContextualBuild = ! empty($parameters) || ! is_null($concrete);
Expand Down Expand Up @@ -1032,6 +1050,26 @@ protected function unresolvablePrimitive(ReflectionParameter $parameter)
throw new BindingResolutionException($message);
}

/**
* Register a new before resolving callback for all types.
*
* @param \Closure|string $abstract
* @param \Closure|null $callback
* @return void
*/
public function beforeResolving($abstract, Closure $callback = null)
{
if (is_string($abstract)) {
$abstract = $this->getAlias($abstract);
}

if ($abstract instanceof Closure && is_null($callback)) {
$this->globalBeforeResolvingCallbacks[] = $abstract;
} else {
$this->beforeResolvingCallbacks[$abstract][] = $callback;
}
}

/**
* Register a new resolving callback.
*
Expand Down Expand Up @@ -1072,6 +1110,39 @@ public function afterResolving($abstract, Closure $callback = null)
}
}

/**
* Fire all of the before resolving callbacks.
*
* @param string $abstract
* @param array $parameters
* @return void
*/
protected function fireBeforeResolvingCallbacks($abstract, $parameters = [])
{
$this->fireBeforeCallbackArray($abstract, $parameters, $this->globalBeforeResolvingCallbacks);

foreach ($this->beforeResolvingCallbacks as $type => $callbacks) {
if ($type === $abstract || is_subclass_of($abstract, $type)) {
$this->fireBeforeCallbackArray($abstract, $parameters, $callbacks);
}
}
}

/**
* Fire an array of callbacks with an object.
*
* @param string $abstract
* @param array $parameters
* @param array $callbacks
* @return void
*/
protected function fireBeforeCallbackArray($abstract, $parameters, array $callbacks)
{
foreach ($callbacks as $callback) {
$callback($abstract, $parameters, $this);
}
}

/**
* Fire all of the resolving callbacks.
*
Expand Down
39 changes: 39 additions & 0 deletions tests/Container/ResolvingCallbackTest.php
Expand Up @@ -441,6 +441,45 @@ public function testAfterResolvingCallbacksAreCalledOnceForImplementation()
$container->make(ResolvingContractStub::class);
$this->assertEquals(2, $callCounter);
}

public function testBeforeResolvingCallbacksAreCalled()
{
// Given a call counter initialized to zero.
$container = new Container;
$callCounter = 0;

// And a contract/implementation stub binding.
$container->bind(ResolvingContractStub::class, ResolvingImplementationStub::class);

// When we add a before resolving callback that increment the counter by one.
$container->beforeResolving(ResolvingContractStub::class, function () use (&$callCounter) {
$callCounter++;
});

// Then resolving the implementation stub increases the counter by one.
$container->make(ResolvingImplementationStub::class);
$this->assertEquals(1, $callCounter);

// And resolving the contract stub increases the counter by one.
$container->make(ResolvingContractStub::class);
$this->assertEquals(2, $callCounter);
}

public function testGlobalBeforeResolvingCallbacksAreCalled()
{
// Given a call counter initialized to zero.
$container = new Container;
$callCounter = 0;

// When we add a global before resolving callback that increment that counter by one.
$container->beforeResolving(function () use (&$callCounter) {
$callCounter++;
});

// Then resolving anything increases the counter by one.
$container->make(ResolvingImplementationStub::class);
$this->assertEquals(1, $callCounter);
}
}

interface ResolvingContractStub
Expand Down