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\Exceptions\NotEnoughParametersException; |
21
|
|
|
use DivineNii\Invoker\Interfaces\ArgumentResolverInterface; |
22
|
|
|
use DivineNii\Invoker\Interfaces\ArgumentValueResolverInterface; |
23
|
|
|
use Psr\Container\ContainerInterface; |
24
|
|
|
use ReflectionParameter; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Invoke a callable. |
28
|
|
|
* |
29
|
|
|
* @author Matthieu Napoli <[email protected]> |
30
|
|
|
* @author Divine Niiquaye Ibok <[email protected]> |
31
|
|
|
*/ |
32
|
|
|
class Invoker implements Interfaces\InvokerInterface |
33
|
|
|
{ |
34
|
|
|
/** @var CallableResolver */ |
35
|
|
|
private $callableResolver; |
36
|
|
|
|
37
|
|
|
/** @var ArgumentResolverInterface */ |
38
|
|
|
private $argumentResolver; |
39
|
|
|
|
40
|
|
|
/** @var null|ContainerInterface */ |
41
|
|
|
private $container; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param iterable<ArgumentValueResolverInterface> $argumentValueResolvers |
45
|
|
|
* @param null|ContainerInterface $container |
46
|
|
|
*/ |
47
|
47 |
|
public function __construct(iterable $argumentValueResolvers = [], ?ContainerInterface $container = null) |
48
|
|
|
{ |
49
|
47 |
|
$this->container = $container; |
50
|
47 |
|
$this->callableResolver = new CallableResolver($container); |
51
|
47 |
|
$this->argumentResolver = new ArgumentResolver($argumentValueResolvers, $container); |
52
|
47 |
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* {@inheritdoc} |
56
|
|
|
*/ |
57
|
28 |
|
public function call($callable, array $parameters = []) |
58
|
|
|
{ |
59
|
28 |
|
$callable = $this->callableResolver->resolve($callable); |
60
|
22 |
|
$callableReflection = CallableReflection::create($callable); |
61
|
21 |
|
$args = $this->argumentResolver->getParameters($callableReflection, $parameters); |
62
|
|
|
|
63
|
|
|
// Sort by array key because call_user_func_array ignores numeric keys |
64
|
21 |
|
\ksort($args); |
65
|
|
|
|
66
|
|
|
// Check all parameters are resolved |
67
|
21 |
|
$diff = \array_diff_key($callableReflection->getParameters(), $args); |
68
|
|
|
|
69
|
21 |
|
if (!empty($diff)) { |
70
|
|
|
/** @var ReflectionParameter $parameter */ |
71
|
2 |
|
$parameter = \reset($diff); |
72
|
|
|
|
73
|
2 |
|
throw new NotEnoughParametersException(\sprintf( |
74
|
2 |
|
'Unable to invoke the callable because no value was given for parameter %d ($%s)', |
75
|
2 |
|
$parameter->getPosition() + 1, |
76
|
2 |
|
$parameter->name |
77
|
|
|
)); |
78
|
|
|
} |
79
|
|
|
|
80
|
19 |
|
return \call_user_func_array($callable, $args); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @return ArgumentResolverInterface |
85
|
|
|
*/ |
86
|
19 |
|
public function getArgumentResolver(): ArgumentResolverInterface |
87
|
|
|
{ |
88
|
19 |
|
return $this->argumentResolver; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @return CallableResolver |
93
|
|
|
*/ |
94
|
1 |
|
public function getCallableResolver(): CallableResolver |
95
|
|
|
{ |
96
|
1 |
|
return $this->callableResolver; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @return null|ContainerInterface |
101
|
|
|
*/ |
102
|
1 |
|
public function getContainer(): ?ContainerInterface |
103
|
|
|
{ |
104
|
1 |
|
return $this->container; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|