Skip to content

Commit 225bc06

Browse files
feature(allow calling invokable class from container using fqn)
1 parent 8d83d7d commit 225bc06

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

src/Illuminate/Container/BoundMethod.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ class BoundMethod
2424
*/
2525
public static function call($container, $callback, array $parameters = [], $defaultMethod = null)
2626
{
27+
if (is_string($callback) && !$defaultMethod && method_exists($callback, '__invoke')) {
28+
$defaultMethod = '__invoke';
29+
}
30+
2731
if (static::isCallableWithAtSign($callback) || $defaultMethod) {
2832
return static::callClass($container, $callback, $parameters, $defaultMethod);
2933
}

tests/Container/ContainerCallTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,14 @@ public function testCallWithCallableObject()
169169
$this->assertSame('jeffrey', $result[1]);
170170
}
171171

172+
public function testCallWithCallableClassString()
173+
{
174+
$container = new Container;
175+
$result = $container->call(ContainerCallCallableClassStringStub::class);
176+
$this->assertInstanceOf(ContainerCallConcreteStub::class, $result[0]);
177+
$this->assertSame('jeffrey', $result[1]);
178+
}
179+
172180
public function testCallWithoutRequiredParamsThrowsException()
173181
{
174182
$this->expectException(BindingResolutionException::class);
@@ -233,3 +241,21 @@ public function __invoke(ContainerCallConcreteStub $stub, $default = 'jeffrey')
233241
return func_get_args();
234242
}
235243
}
244+
245+
class ContainerCallCallableClassStringStub
246+
{
247+
public $stub;
248+
249+
public $default;
250+
251+
public function __construct(ContainerCallConcreteStub $stub, $default = 'jeffrey')
252+
{
253+
$this->stub = $stub;
254+
$this->default = $default;
255+
}
256+
257+
public function __invoke()
258+
{
259+
return [$this->stub, $this->default];
260+
}
261+
}

0 commit comments

Comments
 (0)