1
|
|
|
<?php |
2
|
|
|
namespace Wandu\DI\Resolvers; |
3
|
|
|
|
4
|
|
|
use Wandu\DI\ContainerInterface; |
5
|
|
|
use Wandu\DI\Contracts\ResolverInterface; |
6
|
|
|
use Wandu\DI\Exception\CannotFindParameterException; |
7
|
|
|
use Wandu\Reflection\ReflectionCallable; |
8
|
|
|
use ReflectionException; |
9
|
|
|
use ReflectionFunctionAbstract; |
10
|
|
|
|
11
|
|
|
class CallableResolver implements ResolverInterface |
12
|
|
|
{ |
13
|
|
|
/** @var callable */ |
14
|
|
|
protected $handler; |
15
|
|
|
|
16
|
20 |
|
public function __construct(callable $handler) |
17
|
|
|
{ |
18
|
20 |
|
$this->handler = $handler; |
19
|
20 |
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* {@inheritdoc} |
23
|
|
|
*/ |
24
|
19 |
|
public function resolve(ContainerInterface $container, array $arguments = []) |
25
|
|
|
{ |
26
|
19 |
|
return call_user_func_array( |
27
|
19 |
|
$this->handler, |
28
|
19 |
|
$this->getParameters($container, new ReflectionCallable($this->handler), $arguments) |
29
|
|
|
); |
30
|
|
|
} |
31
|
|
|
|
32
|
19 |
|
protected function getParameters( |
33
|
|
|
ContainerInterface $container, |
34
|
|
|
ReflectionFunctionAbstract $reflectionFunction, |
35
|
|
|
array $arguments = [] |
36
|
|
|
) { |
37
|
19 |
|
$parametersToReturn = static::getSeqArray($arguments); |
38
|
|
|
|
39
|
19 |
|
$reflectionParameters = array_slice($reflectionFunction->getParameters(), count($parametersToReturn)); |
40
|
19 |
|
if (!count($reflectionParameters)) { |
41
|
10 |
|
return $parametersToReturn; |
42
|
|
|
} |
43
|
|
|
/* @var \ReflectionParameter $param */ |
44
|
10 |
|
foreach ($reflectionParameters as $param) { |
45
|
|
|
/* |
46
|
|
|
* #1. search in arguments by parameter name |
47
|
|
|
* #1.1. search in arguments by class name |
48
|
|
|
* #2. if parameter has type hint |
49
|
|
|
* #2.1. search in container by class name |
50
|
|
|
* #3. if has default value, insert default value. |
51
|
|
|
* #4. exception |
52
|
|
|
*/ |
53
|
10 |
|
$paramName = $param->getName(); |
|
|
|
|
54
|
|
|
try { |
55
|
10 |
|
if (array_key_exists($paramName, $arguments)) { // #1. |
56
|
3 |
|
$parametersToReturn[] = $arguments[$paramName]; |
57
|
3 |
|
continue; |
58
|
|
|
} |
59
|
9 |
|
$paramClass = $param->getClass(); |
60
|
9 |
|
if ($paramClass) { // #2. |
61
|
8 |
|
$paramClassName = $paramClass->getName(); |
|
|
|
|
62
|
8 |
|
if ($container->has($paramClassName)) { // #2.1. |
63
|
8 |
|
$parametersToReturn[] = $container->get($paramClassName); |
64
|
8 |
|
continue; |
65
|
|
|
} |
66
|
|
|
} |
67
|
3 |
|
if ($param->isDefaultValueAvailable()) { // #3. |
68
|
1 |
|
$parametersToReturn[] = $param->getDefaultValue(); |
69
|
1 |
|
continue; |
70
|
|
|
} |
71
|
3 |
|
throw new CannotFindParameterException($paramName); // #4. |
72
|
4 |
|
} catch (ReflectionException $e) { |
73
|
|
|
throw new CannotFindParameterException($paramName); |
74
|
|
|
} |
75
|
|
|
} |
76
|
10 |
|
return $parametersToReturn; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param array $array |
81
|
|
|
* @return array |
82
|
|
|
*/ |
83
|
19 |
|
protected static function getSeqArray(array $array) |
84
|
|
|
{ |
85
|
19 |
|
$arrayToReturn = []; |
86
|
19 |
|
foreach ($array as $key => $item) { |
87
|
4 |
|
if (is_int($key)) { |
88
|
4 |
|
$arrayToReturn[] = $item; |
89
|
|
|
} |
90
|
|
|
} |
91
|
19 |
|
return $arrayToReturn; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|