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

[7.x] Allow Contextual Binding outside of constructors #30542

Closed
wants to merge 2 commits into from
Closed
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
37 changes: 26 additions & 11 deletions src/Illuminate/Container/BoundMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ class BoundMethod
*/
public static function call($container, $callback, array $parameters = [], $defaultMethod = null)
{
if (static::isCallableWithAtSign($callback) || $defaultMethod) {
return static::callClass($container, $callback, $parameters, $defaultMethod);
if (($sign = static::getCallableSign($callback)) || $defaultMethod) {
return static::callClass($container, $callback, $parameters, $defaultMethod, $sign ?: '@');
}

return static::callBoundMethod($container, $callback, function () use ($container, $callback, $parameters) {
Expand All @@ -41,19 +41,19 @@ public static function call($container, $callback, array $parameters = [], $defa
* @param string $target
* @param array $parameters
* @param string|null $defaultMethod
* @param string $sign
* @return mixed
*
* @throws \InvalidArgumentException
*/
protected static function callClass($container, $target, array $parameters = [], $defaultMethod = null)
protected static function callClass($container, $target, array $parameters = [], $defaultMethod = null, $sign = '@')
{
$segments = explode('@', $target);
$segments = explode($sign, $target);

// We will assume an @ sign is used to delimit the class name from the method
// name. We will split on this @ sign and then build a callable array that
// we can pass right back into the "call" method for dependency binding.
$method = count($segments) === 2
? $segments[1] : $defaultMethod;
$method = count($segments) === 2 ? $segments[1] : $defaultMethod;

if (is_null($method)) {
throw new InvalidArgumentException('Method not provided.');
Expand Down Expand Up @@ -118,7 +118,7 @@ protected static function getMethodDependencies($container, $callback, array $pa
$dependencies = [];

foreach (static::getCallReflector($callback)->getParameters() as $parameter) {
static::addDependencyForCallParameter($container, $parameter, $parameters, $dependencies);
static::addDependencyForCallParameter($container, $parameter, $parameters, $dependencies, $callback);
}

return array_merge($dependencies, $parameters);
Expand Down Expand Up @@ -152,10 +152,11 @@ protected static function getCallReflector($callback)
* @param \ReflectionParameter $parameter
* @param array $parameters
* @param array $dependencies
* @param callable|string $callback
* @return void
*/
protected static function addDependencyForCallParameter($container, $parameter,
array &$parameters, &$dependencies)
array &$parameters, &$dependencies, $callback)
{
if (array_key_exists($parameter->name, $parameters)) {
$dependencies[] = $parameters[$parameter->name];
Expand All @@ -166,7 +167,9 @@ protected static function addDependencyForCallParameter($container, $parameter,

unset($parameters[$parameter->getClass()->name]);
} elseif ($parameter->getClass()) {
$dependencies[] = $container->make($parameter->getClass()->name);
$target = is_array($callback) ? get_class($callback[0]) : null;

$dependencies[] = $container->makeFor($parameter->getClass()->name, $target);
} elseif ($parameter->isDefaultValueAvailable()) {
$dependencies[] = $parameter->getDefaultValue();
}
Expand All @@ -178,8 +181,20 @@ protected static function addDependencyForCallParameter($container, $parameter,
* @param mixed $callback
* @return bool
*/
protected static function isCallableWithAtSign($callback)
protected static function getCallableSign($callback)
{
return self::hasCallableSign($callback, '@') ? '@' : (
self::hasCallableSign($callback, '::') ? '::' : '');
}

/**
* Determine if the given string is in Class@method syntax.
*
* @param mixed $callback
* @return bool
*/
protected static function hasCallableSign($callback, $sign)
{
return is_string($callback) && strpos($callback, '@') !== false;
return is_string($callback) && strpos($callback, $sign) !== false;
}
}
17 changes: 17 additions & 0 deletions src/Illuminate/Container/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,23 @@ public function make($abstract, array $parameters = [])
return $this->resolve($abstract, $parameters);
}

/**
* Resolve the given type from the container for a specific parent.
*
* @param string $abstract
* @param string $target
* @param array $parameters
* @return mixed
*
* @throws \Illuminate\Contracts\Container\BindingResolutionException
*/
public function makeFor($abstract, $target, array $parameters = [])
{
$this->buildStack[] = $target;

return $this->make($abstract, $parameters);
}

/**
* {@inheritdoc}
*/
Expand Down
40 changes: 40 additions & 0 deletions tests/Container/ContextualBindingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,23 @@ public function testContextualBindingWorksForNestedOptionalDependencies()
);
$this->assertInstanceOf(ContainerContextImplementationStubTwo::class, $resolvedInstance->implTwo->impl);
}

public function testContextualBindingWorksForJobHandleMethod()
{
$container = new Container;

$container->when(ContainerTestContextWithHandleMethod::class)
->needs(IContainerContextWorkerStub::class)
->give(ContainerContextWorkerInstance::class);

$instance = $container->make(ContainerTestContextWithHandleMethod::class);

$this->assertEquals(ContainerContextWorkerInstance::$calls, 0);

$container->call([$instance, 'handle']);

$this->assertEquals(ContainerContextWorkerInstance::$calls, 1);
}
}

interface IContainerContextContractStub
Expand Down Expand Up @@ -309,3 +326,26 @@ public function __construct(ContainerTestContextInjectOne $inner = null)
$this->inner = $inner;
}
}

interface IContainerContextWorkerStub
{
public function work();
}

class ContainerContextWorkerInstance implements IContainerContextWorkerStub
{
public static $calls = 0;

public function work()
{
static::$calls++;
}
}

class ContainerTestContextWithHandleMethod
{
public function handle(IContainerContextWorkerStub $impl)
{
$impl->work();
}
}