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; |
19
|
|
|
|
20
|
|
|
use DivineNii\Invoker\Interfaces\ParameterResolverInterface; |
21
|
|
|
use ReflectionFunctionAbstract; |
22
|
|
|
|
23
|
|
|
class ParameterResolver implements ParameterResolverInterface |
24
|
|
|
{ |
25
|
|
|
/** @var callable[] |
26
|
|
|
*/ |
27
|
|
|
private $resolvers; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param callable[] $resolvers |
31
|
|
|
*/ |
32
|
48 |
|
public function __construct(array $resolvers = []) |
33
|
|
|
{ |
34
|
48 |
|
$this->resolvers = $resolvers; |
35
|
48 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Push a parameter resolver after the ones already registered. |
39
|
|
|
* |
40
|
|
|
* @param callable $resolver |
41
|
|
|
*/ |
42
|
|
|
public function appendResolver(callable $resolver): void |
43
|
|
|
{ |
44
|
|
|
$this->resolvers[] = $resolver; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Insert a parameter resolver before the ones already registered. |
49
|
|
|
* |
50
|
|
|
* @param callable $resolver |
51
|
|
|
*/ |
52
|
|
|
public function prependResolver(callable $resolver): void |
53
|
|
|
{ |
54
|
|
|
\array_unshift($this->resolvers, $resolver); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* {@inheritdoc} |
59
|
|
|
*/ |
60
|
39 |
|
public function getParameters(ReflectionFunctionAbstract $reflection, array $providedParameters = []): array |
61
|
|
|
{ |
62
|
39 |
|
$reflectionParameters = $reflection->getParameters(); |
63
|
39 |
|
$resolvedParameters = []; |
64
|
|
|
|
65
|
39 |
|
foreach ($this->resolvers as $resolver) { |
66
|
39 |
|
$resolvedParameters = ($resolver)($reflection, $providedParameters, $resolvedParameters); |
67
|
|
|
|
68
|
39 |
|
$diff = \array_diff_key($reflectionParameters, $resolvedParameters); |
69
|
|
|
|
70
|
39 |
|
if (empty($diff)) { |
71
|
|
|
// Stop traversing: all parameters are resolved |
72
|
36 |
|
return $resolvedParameters; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
3 |
|
return $resolvedParameters; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|