1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright (C) 2018 Gerrit Addiks. |
4
|
|
|
* This package (including this file) was released under the terms of the GPL-3.0. |
5
|
|
|
* You should have received a copy of the GNU General Public License along with this program. |
6
|
|
|
* If not, see <http://www.gnu.org/licenses/> or send me a mail so i can send you a copy. |
7
|
|
|
* |
8
|
|
|
* @license GPL-3.0 |
9
|
|
|
* |
10
|
|
|
* @author Gerrit Addiks <[email protected]> |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace Addiks\SymfonyGenerics\Arguments; |
14
|
|
|
|
15
|
|
|
use Addiks\SymfonyGenerics\Arguments\Argument; |
16
|
|
|
use Closure; |
17
|
|
|
use Webmozart\Assert\Assert; |
18
|
|
|
use ReflectionFunctionAbstract; |
19
|
|
|
use ReflectionFunction; |
20
|
|
|
use ReflectionObject; |
21
|
|
|
use ReflectionClass; |
22
|
|
|
use ReflectionParameter; |
23
|
|
|
use Addiks\SymfonyGenerics\Services\ArgumentCompilerInterface; |
24
|
|
|
|
25
|
|
|
final class ArgumentCall implements Argument |
26
|
|
|
{ |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var Argument |
30
|
|
|
*/ |
31
|
|
|
private $callee; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
private $methodName; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var array<Argument> |
40
|
|
|
*/ |
41
|
|
|
private $arguments = array(); |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var ArgumentCompilerInterface |
45
|
|
|
*/ |
46
|
|
|
private $argumentCompiler; |
47
|
|
|
|
48
|
10 |
|
public function __construct( |
49
|
|
|
ArgumentCompilerInterface $argumentCompiler, |
50
|
|
|
Argument $callee, |
51
|
|
|
string $methodName, |
52
|
|
|
array $arguments |
53
|
|
|
) { |
54
|
10 |
|
$this->argumentCompiler = $argumentCompiler; |
55
|
10 |
|
$this->callee = $callee; |
56
|
10 |
|
$this->methodName = $methodName; |
57
|
|
|
$this->arguments = array_map(function (Argument $argument): Argument { |
58
|
6 |
|
return $argument; |
59
|
10 |
|
}, $arguments); |
60
|
10 |
|
} |
61
|
|
|
|
62
|
2 |
|
public function resolve() |
63
|
|
|
{ |
64
|
|
|
/** @var object|string $callee */ |
65
|
2 |
|
$callee = $this->callee->resolve(); |
66
|
|
|
|
67
|
|
|
/** @var ReflectionFunctionAbstract|null $methodReflection */ |
68
|
2 |
|
$methodReflection = null; |
69
|
|
|
|
70
|
2 |
|
if (is_string($callee)) { |
71
|
1 |
|
Assert::classExists($callee); |
72
|
|
|
|
73
|
|
|
$reflectionClass = new ReflectionClass($callee); |
74
|
|
|
|
75
|
|
|
if ($reflectionClass->hasMethod($this->methodName)) { |
76
|
|
|
$methodReflection = $reflectionClass->getMethod($this->methodName); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
} else { |
80
|
1 |
|
$reflectionObject = new ReflectionObject($callee); |
81
|
|
|
|
82
|
1 |
|
if ($reflectionObject->hasMethod($this->methodName)) { |
83
|
1 |
|
$methodReflection = $reflectionObject->getMethod($this->methodName); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** @var array<mixed> $argumentsConfiguration */ |
88
|
1 |
|
$argumentsConfiguration = array_map( |
89
|
|
|
/** @return mixed */ |
90
|
|
|
function (Argument $argument) { |
91
|
1 |
|
return $argument->resolve(); |
92
|
1 |
|
}, |
93
|
1 |
|
$this->arguments |
94
|
|
|
); |
95
|
|
|
|
96
|
|
|
/** @var callable $callback */ |
97
|
1 |
|
$callback = [$callee, $this->methodName]; |
98
|
|
|
|
99
|
|
|
/** @var array<mixed> $arguments */ |
100
|
1 |
|
$arguments = $argumentsConfiguration; |
101
|
|
|
|
102
|
1 |
|
if (!is_null($methodReflection)) { |
103
|
1 |
|
$arguments = $this->argumentCompiler->buildCallArguments( |
104
|
1 |
|
$methodReflection, |
105
|
1 |
|
$argumentsConfiguration |
106
|
|
|
); |
107
|
|
|
} |
108
|
|
|
|
109
|
1 |
|
return call_user_func_array($callback, $arguments); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
} |
113
|
|
|
|