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