1
|
|
|
<?php |
2
|
|
|
namespace nebula\runnable\target; |
3
|
|
|
|
4
|
|
|
use ReflectionClass; |
5
|
|
|
use nebula\runnable\target\FileTarget; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* 可执行命令:类方法 |
9
|
|
|
* |
10
|
|
|
*/ |
11
|
|
|
class MethodTarget extends FileTarget |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* 构建方法参数 |
15
|
|
|
* |
16
|
|
|
* @var array |
17
|
|
|
*/ |
18
|
|
|
private $constructParameter; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* 类对象 |
22
|
|
|
* |
23
|
|
|
* @var object|string |
24
|
|
|
*/ |
25
|
|
|
private $object; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* 类方法 |
29
|
|
|
* |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
private $method; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* 可执行对象 |
36
|
|
|
* |
37
|
|
|
* @var array|null |
38
|
|
|
*/ |
39
|
|
|
protected $runnableTarget; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* 构造对象 |
43
|
|
|
* |
44
|
|
|
* @param string|object $object |
45
|
|
|
* @param array|null $constructParameter |
46
|
|
|
* @param string $method |
47
|
|
|
* @param array $parameter |
48
|
|
|
*/ |
49
|
|
|
public function __construct($object, ?array $constructParameter=null, string $method, array $parameter = []) |
50
|
|
|
{ |
51
|
|
|
$this->object = $object; |
52
|
|
|
$this->constructParameter = $constructParameter; |
53
|
|
|
$this->method = $method; |
54
|
|
|
$this->parameter = $parameter; |
55
|
|
|
$static = $this->isStatic() ? '->' : '::'; |
56
|
|
|
$name = \is_object($object)? \get_class($object) : $object; |
57
|
|
|
$this->name = $name.$static.$method; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* 获取对象实例 |
62
|
|
|
* |
63
|
|
|
* @return object|null 动态类可获取 |
64
|
|
|
*/ |
65
|
|
|
public function getObjectInstance():?object |
66
|
|
|
{ |
67
|
|
|
if (\is_object($this->object)) { |
68
|
|
|
return $this->object; |
69
|
|
|
} |
70
|
|
|
if (is_null($this->constructParameter)) { |
|
|
|
|
71
|
|
|
return null; |
72
|
|
|
} |
73
|
|
|
if (!is_null($this->requireFile) && !\class_exists($this->object)) { |
74
|
|
|
require_once $this->requireFile; |
75
|
|
|
} |
76
|
|
|
$classRef= new ReflectionClass($this->object); |
77
|
|
|
return $classRef->newInstanceArgs($this->constructParameter); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function getRunnableTarget() |
81
|
|
|
{ |
82
|
|
|
if (is_null($this->runnableTarget)) { |
83
|
|
|
if ($this->isStatic() || \is_object($this->object)) { |
84
|
|
|
$this->runnableTarget = [$this->object, $this->method]; |
85
|
|
|
} |
86
|
|
|
$this->runnableTarget = [$this->getObjectInstance() ?? $this->object, $this->method]; |
87
|
|
|
} |
88
|
|
|
return $this->runnableTarget; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* 判断是否为静态方法 |
93
|
|
|
* |
94
|
|
|
* @return boolean |
95
|
|
|
*/ |
96
|
|
|
public function isStatic():bool |
97
|
|
|
{ |
98
|
|
|
return !\is_object($this->object) && \is_null($this->constructParameter); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Set 需要的文件 |
103
|
|
|
* |
104
|
|
|
* @param string|null $requireFile 需要的文件 |
105
|
|
|
* |
106
|
|
|
* @return self |
107
|
|
|
*/ |
108
|
|
|
public function setRequireFile($requireFile) |
109
|
|
|
{ |
110
|
|
|
if (is_null($this->requireFile)) { |
111
|
|
|
$this->requireFile = $requireFile; |
112
|
|
|
$this->name = $this->name.'@'.$requireFile; |
113
|
|
|
} |
114
|
|
|
return $this; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* 是否可执行 |
119
|
|
|
* |
120
|
|
|
* @return boolean |
121
|
|
|
*/ |
122
|
|
|
public function isVaild():bool |
123
|
|
|
{ |
124
|
|
|
return is_object($this->object) || \class_exists($this->object) || parent::isVaild(); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* 执行代码 |
129
|
|
|
* |
130
|
|
|
* @param array $args |
131
|
|
|
* @return mixed |
132
|
|
|
*/ |
133
|
|
|
public function apply(array $parameter) |
134
|
|
|
{ |
135
|
|
|
$runnable = $this->getRunnableTarget(); |
136
|
|
|
$method = new \ReflectionMethod($runnable[0], $runnable[1]); |
137
|
|
|
if (!$method->isPublic()) { |
138
|
|
|
$method->setAccessible(true); |
139
|
|
|
} |
140
|
|
|
if (count($parameter) == 0) { |
141
|
|
|
$parameter = $this->getParameter(); |
142
|
|
|
} |
143
|
|
|
if (is_object($runnable[0])) { |
144
|
|
|
return $method->invokeArgs($runnable[0], $parameter); |
145
|
|
|
} else { |
146
|
|
|
return $method->invokeArgs(null, $parameter); |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Get 类方法 |
152
|
|
|
* |
153
|
|
|
* @return string |
154
|
|
|
*/ |
155
|
|
|
public function getMethod() |
156
|
|
|
{ |
157
|
|
|
return $this->method; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Get 构建方法参数 |
162
|
|
|
* |
163
|
|
|
* @return array |
164
|
|
|
*/ |
165
|
|
|
public function getConstructParameter() |
166
|
|
|
{ |
167
|
|
|
return $this->constructParameter; |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|