|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Zenstruck\Browser\Util; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Utility to manipulate and validate \Closure arguments. |
|
7
|
|
|
* |
|
8
|
|
|
* TODO extract to a library as zenstruck/foundry could benefit. |
|
9
|
|
|
* |
|
10
|
|
|
* @author Kevin Bond <[email protected]> |
|
11
|
|
|
* |
|
12
|
|
|
* @internal |
|
13
|
|
|
*/ |
|
14
|
|
|
final class FunctionExecutor |
|
15
|
|
|
{ |
|
16
|
|
|
private \ReflectionFunction $function; |
|
17
|
|
|
private int $minArguments = 0; |
|
18
|
|
|
private array $typeReplace = []; |
|
19
|
|
|
|
|
20
|
|
|
public function __construct(\ReflectionFunction $function) |
|
21
|
|
|
{ |
|
22
|
|
|
$this->function = $function; |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @param callable|\ReflectionFunction $value |
|
27
|
|
|
*/ |
|
28
|
|
|
public static function createFor($value): self |
|
29
|
|
|
{ |
|
30
|
|
|
if (\is_callable($value)) { |
|
31
|
|
|
$value = new \ReflectionFunction(\Closure::fromCallable($value)); |
|
|
|
|
|
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
if (!$value instanceof \ReflectionFunction) { |
|
35
|
|
|
throw new \InvalidArgumentException('$value must be callable or \ReflectionFunction'); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
return new self($value); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function minArguments(int $min): self |
|
42
|
|
|
{ |
|
43
|
|
|
$this->minArguments = $min; |
|
44
|
|
|
|
|
45
|
|
|
return $this; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function replaceTypedArgument(string $typehint, $value): self |
|
49
|
|
|
{ |
|
50
|
|
|
$this->typeReplace[$typehint] = $value; |
|
51
|
|
|
|
|
52
|
|
|
return $this; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function replaceUntypedArgument($value): self |
|
56
|
|
|
{ |
|
57
|
|
|
$this->typeReplace[null] = $value; |
|
58
|
|
|
|
|
59
|
|
|
return $this; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function execute() |
|
63
|
|
|
{ |
|
64
|
|
|
$arguments = $this->function->getParameters(); |
|
65
|
|
|
|
|
66
|
|
|
if (\count($arguments) < $this->minArguments) { |
|
67
|
|
|
throw new \ArgumentCountError("{$this->minArguments} argument(s) required."); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
$arguments = \array_map([$this, 'replaceArgument'], $arguments); |
|
71
|
|
|
|
|
72
|
|
|
return $this->function->invoke(...$arguments); |
|
|
|
|
|
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
private function replaceArgument(\ReflectionParameter $argument) |
|
76
|
|
|
{ |
|
77
|
|
|
$type = $argument->getType(); |
|
78
|
|
|
|
|
79
|
|
|
if (!$type && \array_key_exists(null, $this->typeReplace)) { |
|
80
|
|
|
return $this->typeReplace[null]; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
if (!$type instanceof \ReflectionNamedType) { |
|
84
|
|
|
throw new \TypeError("Unable to replace argument \"{$argument->getName()}\"."); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
foreach (\array_keys($this->typeReplace) as $typehint) { |
|
88
|
|
|
if (!\is_a($type->getName(), $typehint, true)) { |
|
89
|
|
|
continue; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
if (!($value = $this->typeReplace[$typehint]) instanceof \Closure) { |
|
93
|
|
|
return $value; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
return $value($type->getName()); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
throw new \TypeError("Unable to replace argument \"{$argument->getName()}\"."); |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|