Skip to content

Commit

Permalink
resolve invokable class instead of callable
Browse files Browse the repository at this point in the history
  • Loading branch information
juliangut committed Aug 21, 2019
1 parent 99ebbdf commit 104e3a8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
15 changes: 9 additions & 6 deletions src/CallableResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function resolve($toResolve): callable
$resolvable = $toResolve;

if (\is_string($resolvable)) {
$resolvable = $this->callableFromStringNotation($resolvable, '__invoke');
$resolvable = $this->callableFromStringNotation($resolvable);
}

return $this->resolveCallable($resolvable, $toResolve);
Expand Down Expand Up @@ -128,15 +128,18 @@ protected function resolveCallable($resolvable, $toResolve): callable
/**
* Get callable from string callable notation.
*
* @param string $toResolve
* @param string $defaultMethod
* @param string $toResolve
* @param string|null $defaultMethod
*
* @return string[]
* @return string|string[]
*/
private function callableFromStringNotation(string $toResolve, string $defaultMethod): array
private function callableFromStringNotation(string $toResolve, ?string $defaultMethod = null)
{
\preg_match(static::CALLABLE_PATTERN, $toResolve, $matches);
if ($matches) {
return [$matches[1], $matches[2]];
}

return $matches ? [$matches[1], $matches[2]] : [$toResolve, $defaultMethod];
return $defaultMethod !== null ? [$toResolve, $defaultMethod] : $toResolve;
}
}
12 changes: 6 additions & 6 deletions tests/PHPDI/CallableResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ class CallableResolverTest extends TestCase
*
* @param string $resolveMethod
* @param string|mixed[]|object $toResolve
* @param mixed[] $expectedResolvable
* @param string|mixed[] $expectedResolvable
*/
public function testResolveFromString(
string $resolveMethod,
$toResolve,
array $expectedResolvable
$expectedResolvable
): void {
$invoker = $this->getMockBuilder(InvokerResolver::class)
->disableOriginalConstructor()
Expand All @@ -62,7 +62,7 @@ public function getResolvableList(): array
$middleware = $this->getMockBuilder(MiddlewareInterface::class)->getMock();

return [
['resolve', 'Service', ['Service', '__invoke']],
['resolve', 'Service', 'Service'],
['resolve', 'Service:method', ['Service', 'method']],
['resolve', 'Service::method', ['Service', 'method']],
['resolve', ['Service', 'method'], ['Service', 'method']],
Expand All @@ -82,12 +82,12 @@ public function getResolvableList(): array
*
* @param string $resolveMethod
* @param string|mixed[]|object $toResolve
* @param mixed[] $expectedResolvable
* @param string|mixed[] $expectedResolvable
*/
public function testNotResolvable(
string $resolveMethod,
$toResolve,
array $expectedResolvable,
$expectedResolvable,
string $expectedEsceptionType
): void {
$this->expectException(\RuntimeException::class);
Expand All @@ -113,7 +113,7 @@ public function getNotResolvableList(): array
$middleware = $this->getMockBuilder(MiddlewareInterface::class)->getMock();

return [
['resolve', 'Service', ['Service', '__invoke'], 'Service'],
['resolve', 'Service', 'Service', 'Service'],
['resolve', 'Service:method', ['Service', 'method'], 'Service:method'],
['resolve', 'Service::method', ['Service', 'method'], 'Service::method'],
['resolve', ['Service', 'method'], ['Service', 'method'], \json_encode(['Service', 'method'])],
Expand Down

0 comments on commit 104e3a8

Please sign in to comment.