-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExecutable.php
115 lines (97 loc) · 3.12 KB
/
Executable.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php
/**
* Qubus\Injector
*
* @link https://github.com/QubusPHP/injector
* @copyright 2020 Joshua Parker <[email protected]>
* @copyright 2013-2014 Daniel Lowrey, Levi Morrison, Dan Ackroyd
* @license https://opensource.org/licenses/mit-license.php MIT License
*/
declare(strict_types=1);
namespace Qubus\Injector;
use Closure;
use Qubus\Exception\Data\TypeException;
use ReflectionFunctionAbstract;
use ReflectionMethod;
use function call_user_func_array;
use function func_get_args;
use function is_object;
use function version_compare;
use const PHP_VERSION;
class Executable
{
/** @var ReflectionFunctionAbstract $callableReflection */
private ReflectionFunctionAbstract $callableReflection;
/** @var mixed $invocationObject */
private $invocationObject;
private bool $isInstanceMethod;
/**
* @throws TypeException
*/
public function __construct(ReflectionFunctionAbstract $reflFunc, ?object $invocationObject = null)
{
if ($reflFunc instanceof ReflectionMethod) {
$this->isInstanceMethod = true;
$this->setMethodCallable($reflFunc, $invocationObject);
} else {
$this->isInstanceMethod = false;
$this->callableReflection = $reflFunc;
}
}
/**
* @throws TypeException
*/
private function setMethodCallable(ReflectionMethod $reflection, ?object $invocationObject): void
{
if (is_object($invocationObject)) {
$this->callableReflection = $reflection;
$this->invocationObject = $invocationObject;
} elseif ($reflection->isStatic()) {
$this->callableReflection = $reflection;
} else {
throw new TypeException(
'ReflectionMethod callables must specify an invocation object'
);
}
}
public function __invoke()
{
$args = func_get_args();
$reflection = $this->callableReflection;
if ($this->isInstanceMethod) {
return $reflection->invokeArgs($this->invocationObject, $args);
}
return $this->callableReflection->isClosure()
? $this->invokeClosureCompat($reflection, $args)
: $reflection->invokeArgs($args);
}
/**
* @todo Remove this extra indirection when 5.3 support is dropped
*/
private function invokeClosureCompat($reflection, $args)
{
if (version_compare(PHP_VERSION, '5.4.0') >= 0) {
$scope = $reflection->getClosureScopeClass();
$closure = Closure::bind(
$reflection->getClosure(),
$reflection->getClosureThis(),
$scope ? $scope->name : null
);
return call_user_func_array($closure, $args);
} else {
return $reflection->invokeArgs($args);
}
}
public function getCallableReflection(): ReflectionFunctionAbstract
{
return $this->callableReflection;
}
public function getInvocationObject()
{
return $this->invocationObject;
}
public function isInstanceMethod(): bool
{
return $this->isInstanceMethod;
}
}