generated from yiisoft/package-template
-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
CallableDefinition.php
57 lines (47 loc) · 1.44 KB
/
CallableDefinition.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<?php
declare(strict_types=1);
namespace Yiisoft\Definitions;
use ReflectionMethod;
use Yiisoft\Definitions\Contract\DefinitionInterface;
use Yiisoft\Definitions\Contract\DependencyResolverInterface;
use function is_array;
use function is_object;
final class CallableDefinition implements DefinitionInterface
{
/**
* @var array|callable
* @psalm-var callable|array{0:class-string,1:string}
*/
private $method;
/**
* @param array|callable $method
*
* @psalm-param callable|array{0:class-string,1:string} $method
*/
public function __construct($method)
{
$this->method = $method;
}
public function resolve(DependencyResolverInterface $container)
{
$callable = $this->prepareCallable($this->method, $container);
/** @psalm-suppress MixedMethodCall */
return $container->invoke($callable);
}
/**
* @param array|callable $callable
*
* @psalm-param callable|array{0:class-string,1:string} $callable
*/
private function prepareCallable($callable, DependencyResolverInterface $container): callable
{
if (is_array($callable) && !is_object($callable[0])) {
$reflection = new ReflectionMethod($callable[0], $callable[1]);
if (!$reflection->isStatic()) {
/** @var mixed */
$callable[0] = $container->get($callable[0]);
}
}
return $callable;
}
}