-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExecutable.php
More file actions
112 lines (93 loc) · 3.02 KB
/
Copy pathExecutable.php
File metadata and controls
112 lines (93 loc) · 3.02 KB
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
<?php
/**
* Qubus\Injector
*
* @link https://github.com/QubusPHP/injector
* @copyright 2020 Joshua Parker <joshua@joshuaparker.dev>
* @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 ReflectionException;
use ReflectionFunctionAbstract;
use ReflectionMethod;
use function call_user_func_array;
use function func_get_args;
use function is_object;
class Executable
{
private ReflectionFunctionAbstract $callableReflection;
private ?object $invocationObject = null;
private bool $isInstanceMethod;
/**
* @throws TypeException
*/
public function __construct(ReflectionFunctionAbstract $reflFunc, ?object $invocationObject = null)
{
if ($reflFunc instanceof ReflectionMethod) {
$this->isInstanceMethod = ! $reflFunc->isStatic();
$this->setMethodCallable($reflFunc, $invocationObject);
} else {
$this->isInstanceMethod = false;
$this->callableReflection = $reflFunc;
}
}
/**
* @throws TypeException
*/
private function setMethodCallable(ReflectionMethod $reflection, ?object $invocationObject = null): void
{
if (! $reflection->isPublic()) {
throw new TypeException('ReflectionMethod callables must be public');
}
if ($reflection->isStatic()) {
$this->callableReflection = $reflection;
} elseif (is_object($invocationObject)) {
$this->callableReflection = $reflection;
$this->invocationObject = $invocationObject;
} else {
throw new TypeException(
'ReflectionMethod callables must specify an invocation object'
);
}
}
/**
* @throws ReflectionException
*/
public function __invoke()
{
$args = func_get_args();
$reflection = $this->callableReflection;
if ($reflection instanceof ReflectionMethod) {
return $reflection->invokeArgs($this->invocationObject, $args);
}
return $reflection->isClosure()
? $this->invokeClosure($reflection, $args)
: $reflection->invokeArgs($args);
}
private function invokeClosure(ReflectionFunctionAbstract $reflection, array $args): mixed
{
$scope = $reflection->getClosureScopeClass();
$closure = Closure::bind(
$reflection->getClosure(),
$reflection->getClosureThis(),
$scope ? $scope->name : null
);
return call_user_func_array($closure ?? $reflection->getClosure(), $args);
}
public function getCallableReflection(): ReflectionFunctionAbstract
{
return $this->callableReflection;
}
public function getInvocationObject()
{
return $this->invocationObject;
}
public function isInstanceMethod(): bool
{
return $this->isInstanceMethod;
}
}