|
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
|
|
|
public function getObjectInstance():object |
|
61
|
|
|
{ |
|
62
|
|
|
if (\is_object($this->object)) { |
|
63
|
|
|
return $this->object; |
|
64
|
|
|
} |
|
65
|
|
|
if (!is_null($this->requireFile) && !\class_exists($this->object)) { |
|
66
|
|
|
require_once $this->requireFile; |
|
67
|
|
|
} |
|
68
|
|
|
$classRef= new ReflectionClass($this->object); |
|
69
|
|
|
return $classRef->newInstanceArgs($this->constructParameter); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function getRunnableTarget() |
|
73
|
|
|
{ |
|
74
|
|
|
if (is_null($this->runnableTarget)) { |
|
75
|
|
|
if ($this->isStatic() || \is_object($this->object)) { |
|
76
|
|
|
$this->runnableTarget = [$this->object, $this->method]; |
|
77
|
|
|
} |
|
78
|
|
|
$this->runnableTarget = [$this->getObjectInstance(), $this->method]; |
|
79
|
|
|
} |
|
80
|
|
|
return $this->runnableTarget; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* 判断是否为静态方法 |
|
85
|
|
|
* |
|
86
|
|
|
* @return boolean |
|
87
|
|
|
*/ |
|
88
|
|
|
public function isStatic():bool |
|
89
|
|
|
{ |
|
90
|
|
|
return !\is_object($this->object) && \is_null($this->constructParameter); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Set 需要的文件 |
|
95
|
|
|
* |
|
96
|
|
|
* @param string|null $requireFile 需要的文件 |
|
97
|
|
|
* |
|
98
|
|
|
* @return self |
|
99
|
|
|
*/ |
|
100
|
|
|
public function setRequireFile($requireFile) |
|
101
|
|
|
{ |
|
102
|
|
|
if (is_null($this->requireFile)) { |
|
103
|
|
|
$this->requireFile = $requireFile; |
|
104
|
|
|
$this->name = $this->name.'@'.$requireFile; |
|
105
|
|
|
} |
|
106
|
|
|
return $this; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* 是否可执行 |
|
111
|
|
|
* |
|
112
|
|
|
* @return boolean |
|
113
|
|
|
*/ |
|
114
|
|
|
public function isVaild():bool |
|
115
|
|
|
{ |
|
116
|
|
|
return is_object($this->object) || \class_exists($this->object) || parent::isVaild(); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* 执行代码 |
|
121
|
|
|
* |
|
122
|
|
|
* @param array $args |
|
123
|
|
|
* @return mixed |
|
124
|
|
|
*/ |
|
125
|
|
|
public function invoke(array $parameter) |
|
126
|
|
|
{ |
|
127
|
|
|
$runnable = $this->getRunnableTarget(); |
|
128
|
|
|
$method = new \ReflectionMethod($runnable[0], $runnable[1]); |
|
129
|
|
|
if (!$method->isPublic()) { |
|
130
|
|
|
$method->setAccessible(true); |
|
131
|
|
|
} |
|
132
|
|
|
if (count($parameter) == 0) { |
|
133
|
|
|
$parameter = $this->getParameter(); |
|
134
|
|
|
} |
|
135
|
|
|
if (is_object($runnable[0])) { |
|
136
|
|
|
return $method->invokeArgs($runnable[0], $parameter); |
|
137
|
|
|
} else { |
|
138
|
|
|
return $method->invokeArgs(null, $parameter); |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Get 类方法 |
|
144
|
|
|
* |
|
145
|
|
|
* @return string |
|
146
|
|
|
*/ |
|
147
|
|
|
public function getMethod() |
|
148
|
|
|
{ |
|
149
|
|
|
return $this->method; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* Get 构建方法参数 |
|
154
|
|
|
* |
|
155
|
|
|
* @return array |
|
156
|
|
|
*/ |
|
157
|
|
|
public function getConstructParameter() |
|
158
|
|
|
{ |
|
159
|
|
|
return $this->constructParameter; |
|
160
|
|
|
} |
|
161
|
|
|
} |
|
162
|
|
|
|