|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of PHP Invoker. |
|
7
|
|
|
* |
|
8
|
|
|
* PHP version 7.1 and above required |
|
9
|
|
|
* |
|
10
|
|
|
* @author Divine Niiquaye Ibok <[email protected]> |
|
11
|
|
|
* @copyright 2019 Biurad Group (https://biurad.com/) |
|
12
|
|
|
* @license https://opensource.org/licenses/BSD-3-Clause License |
|
13
|
|
|
* |
|
14
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
15
|
|
|
* file that was distributed with this source code. |
|
16
|
|
|
*/ |
|
17
|
|
|
|
|
18
|
|
|
namespace DivineNii\Invoker\ArgumentResolver; |
|
19
|
|
|
|
|
20
|
|
|
use DivineNii\Invoker\Interfaces\ArgumentValueResolverInterface; |
|
21
|
|
|
use Psr\Container\ContainerInterface; |
|
22
|
|
|
use Psr\Container\NotFoundExceptionInterface; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Inject entries using type-hints or create a class instance. |
|
26
|
|
|
* Tries to match type-hints with the parameters provided. |
|
27
|
|
|
* |
|
28
|
|
|
* @author Divine Niiquaye Ibok <[email protected]> |
|
29
|
|
|
*/ |
|
30
|
|
|
class TypeHintValueResolver implements ArgumentValueResolverInterface |
|
31
|
|
|
{ |
|
32
|
|
|
/** @var null|ContainerInterface */ |
|
33
|
|
|
private $container; |
|
34
|
|
|
|
|
35
|
56 |
|
public function __construct(?ContainerInterface $container = null) |
|
36
|
|
|
{ |
|
37
|
56 |
|
$this->container = $container; |
|
38
|
56 |
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* {@inheritdoc} |
|
42
|
|
|
*/ |
|
43
|
25 |
|
public function resolve(\ReflectionParameter $parameter, array $providedParameters) |
|
44
|
|
|
{ |
|
45
|
25 |
|
$paramType = $parameter->getType(); |
|
46
|
25 |
|
$types = $paramType instanceof \ReflectionUnionType ? $paramType->getTypes() : [$paramType]; |
|
47
|
25 |
|
$result = []; |
|
48
|
|
|
|
|
49
|
25 |
|
foreach ($types as $parameterType) { |
|
50
|
25 |
|
if (!$parameterType instanceof \ReflectionNamedType || $parameterType->isBuiltin()) { |
|
51
|
|
|
// No type, Primitive types are not supported |
|
52
|
14 |
|
return null; |
|
53
|
|
|
} |
|
54
|
12 |
|
$paramName = $parameterType->getName(); |
|
55
|
|
|
|
|
56
|
12 |
|
if ($paramName === 'self') { |
|
57
|
1 |
|
$paramName = $parameter->getDeclaringClass()->getName(); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
12 |
|
if (\array_key_exists($paramName, $providedParameters)) { |
|
61
|
1 |
|
$result[] = $providedParameters[$paramName]; |
|
62
|
1 |
|
unset($providedParameters[$paramName]); |
|
63
|
|
|
|
|
64
|
1 |
|
continue; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
// If an instance is detected |
|
68
|
11 |
|
foreach ($providedParameters as $key => $value) { |
|
69
|
1 |
|
if (\is_a($value, $paramName, true)) { |
|
70
|
1 |
|
$result[] = $providedParameters[$key]; |
|
71
|
|
|
|
|
72
|
1 |
|
continue; |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
// Inject entries from a DI container using the type-hints. |
|
77
|
11 |
|
if (null !== $this->container) { |
|
78
|
|
|
try { |
|
79
|
6 |
|
$result[] = $this->container->get($paramName); |
|
80
|
|
|
|
|
81
|
5 |
|
continue; |
|
82
|
1 |
|
} catch (NotFoundExceptionInterface $e) { |
|
83
|
|
|
// We need no exception thrown here |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
try { |
|
88
|
7 |
|
if (\class_exists($paramName)) { |
|
89
|
7 |
|
$result[] = new $paramName(); |
|
90
|
|
|
} |
|
91
|
1 |
|
} catch (\ArgumentCountError $e) { |
|
92
|
|
|
// Throw no exception ... |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
12 |
|
if ([] !== $result) { |
|
97
|
10 |
|
return $parameter->isVariadic() ? $result : end($result); |
|
98
|
|
|
} |
|
99
|
2 |
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|