Skip to content

[5.4] Add support for binding methods to the container #16800

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

Merged
merged 3 commits into from
Dec 14, 2016
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
49 changes: 47 additions & 2 deletions src/Illuminate/Container/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ class Container implements ArrayAccess, ContainerContract
*/
protected $bindings = [];

/**
* The container's method bindings.
*
* @var array
*/
protected $methodBindings = [];

/**
* The container's shared instances.
*
Expand Down Expand Up @@ -232,6 +239,42 @@ protected function getClosure($abstract, $concrete)
};
}

/**
* Bind a callback to resolve with Container::call.
*
* @param string $method
* @param \Closure $concrete
* @return void
*/
public function bindMethod($method, $callback)
{
$this->methodBindings[$this->normalize($method)] = $callback;
}

/**
* Call a method that has been bound to the container.
*
* @param callable $callback
* @param mixed $default
* @return mixed
*/
protected function callBoundMethod($callback, $default)
{
if (! is_array($callback)) {
return value($default);
}

$class = is_string($callback[0]) ? $callback[0] : get_class($callback[0]);

$method = $this->normalize("{$class}@{$callback[1]}");

if (! isset($this->methodBindings[$method])) {
return value($default);
}

return $this->methodBindings[$method]($callback[0], $this);
}

/**
* Add a contextual binding to the container.
*
Expand Down Expand Up @@ -503,9 +546,11 @@ public function call($callback, array $parameters = [], $defaultMethod = null)
return $this->callClass($callback, $parameters, $defaultMethod);
}

$dependencies = $this->getMethodDependencies($callback, $parameters);
return $this->callBoundMethod($callback, function () use ($callback, $parameters) {
$dependencies = $this->getMethodDependencies($callback, $parameters);

return call_user_func_array($callback, $dependencies);
return call_user_func_array($callback, $dependencies);
});
}

/**
Expand Down
22 changes: 22 additions & 0 deletions tests/Container/ContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,23 @@ public function testCallWithGlobalMethodName()
$this->assertEquals('taylor', $result[1]);
}

public function testCallWithBoundMethod()
{
$container = new Container;
$container->bindMethod('ContainerTestCallStub@unresolvable', function ($stub) {
return $stub->unresolvable('foo', 'bar');
});
$result = $container->call('ContainerTestCallStub@unresolvable');
$this->assertEquals(['foo', 'bar'], $result);

$container = new Container;
$container->bindMethod('ContainerTestCallStub@unresolvable', function ($stub) {
return $stub->unresolvable('foo', 'bar');
});
$result = $container->call([new ContainerTestCallStub, 'unresolvable']);
$this->assertEquals(['foo', 'bar'], $result);
}

public function testContainerCanInjectDifferentImplementationsDependingOnContext()
{
$container = new Container;
Expand Down Expand Up @@ -812,6 +829,11 @@ public function inject(ContainerConcreteStub $stub, $default = 'taylor')
{
return func_get_args();
}

public function unresolvable($foo, $bar)
{
return func_get_args();
}
}

class ContainerTestContextInjectOne
Expand Down