|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Philae\Utils; |
|
4
|
|
|
|
|
5
|
|
|
use League\Container\Container; |
|
6
|
|
|
use League\Container\ContainerAwareInterface; |
|
7
|
|
|
use League\Container\ContainerAwareTrait; |
|
8
|
|
|
use Middlewares\Utils\CallableResolver\CallableResolverInterface; |
|
9
|
|
|
use Middlewares\Utils\CallableResolver\ReflectionResolver; |
|
10
|
|
|
use Middlewares\Utils\CallableResolver\Resolver; |
|
11
|
|
|
use Psr\Container\ContainerInterface; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Callable resolver with support for: __invoke magic method, container resolution and reflection resolution. |
|
15
|
|
|
* |
|
16
|
|
|
* @method Container|ContainerInterface getContainer() |
|
17
|
|
|
*/ |
|
18
|
|
|
class CallableResolver extends Resolver implements CallableResolverInterface, ContainerAwareInterface |
|
19
|
|
|
{ |
|
20
|
|
|
use ContainerAwareTrait; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var ReflectionResolver |
|
24
|
|
|
*/ |
|
25
|
|
|
private $delegateResolver; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @param CallableResolverInterface|null $delegateResolver |
|
29
|
|
|
*/ |
|
30
|
15 |
|
public function __construct(CallableResolverInterface $delegateResolver = null) |
|
31
|
|
|
{ |
|
32
|
15 |
|
$this->delegateResolver = $delegateResolver ?: new ReflectionResolver(); |
|
|
|
|
|
|
33
|
15 |
|
} |
|
34
|
|
|
|
|
35
|
15 |
|
public function resolve($callable, array $args = []) |
|
36
|
|
|
{ |
|
37
|
15 |
|
$callable = $this->delegateResolver->resolve($this->normalizeCallable($callable, $args), $args); |
|
38
|
|
|
|
|
39
|
15 |
|
if (!$this->getContainer()) { |
|
40
|
|
|
return $callable; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
// Inflect callable |
|
44
|
15 |
|
if (is_array($callable) && isset($callable[0]) && ($callable[0] instanceof ContainerAwareInterface)) { |
|
45
|
|
|
$callable[0]->setContainer($this->getContainer()); |
|
|
|
|
|
|
46
|
15 |
|
} elseif ($callable instanceof ContainerAwareInterface) { |
|
47
|
|
|
$callable->setContainer($this->getContainer()); |
|
|
|
|
|
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
15 |
|
return $callable; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @param mixed $callable |
|
55
|
|
|
* @param array $args |
|
56
|
|
|
* @return array|string|mixed |
|
57
|
|
|
*/ |
|
58
|
15 |
|
protected function normalizeCallable($callable, array $args = []) |
|
59
|
|
|
{ |
|
60
|
|
|
// invokable object |
|
61
|
15 |
|
if (is_object($callable) && method_exists($callable, '__invoke')) { |
|
62
|
15 |
|
return $callable; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
// class::method string |
|
66
|
|
|
if (is_string($callable)) { |
|
67
|
|
|
// Split :: |
|
68
|
|
|
$callable = $this->resolveString($callable); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
// function string |
|
72
|
|
|
if (is_string($callable) && function_exists($callable)) { |
|
73
|
|
|
return $callable; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
// class string (invokable class) |
|
77
|
|
|
if (is_string($callable) |
|
78
|
|
|
&& class_exists($callable) |
|
79
|
|
|
&& function_exists($callable . '::__invoke') |
|
80
|
|
|
) { |
|
81
|
|
|
$instance = $this->findInContainer($callable, $args); |
|
82
|
|
|
if ($instance) { |
|
83
|
|
|
$callable = $instance; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
return [$callable, '__invoke']; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
// callable array with class and method as strings |
|
90
|
|
|
if (is_array($callable) && is_string($callable[0]) && class_exists($callable[0])) { |
|
91
|
|
|
$instance = $this->findInContainer($callable[0], $args); |
|
92
|
|
|
if ($instance) { |
|
93
|
|
|
$callable[0] = $instance; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
return $callable; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
return $callable; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @param string $className |
|
104
|
|
|
* @param array $args |
|
105
|
|
|
* @return bool|mixed |
|
106
|
|
|
*/ |
|
107
|
|
|
protected function findInContainer($className, array $args = []) |
|
108
|
|
|
{ |
|
109
|
|
|
if ($this->getContainer() && $this->getContainer()->has($className)) { |
|
110
|
|
|
return $this->getContainer()->get($className, $args); |
|
|
|
|
|
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
return false; |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.