Passed
Push — master ( fa7991...10a1b7 )
by 世昌
02:08
created

FunctionTarget::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
namespace nebula\runnable\target;
3
4
use nebula\runnable\target\FileTarget;
5
6
/**
7
 * 可执行命令:函数名
8
 * 
9
 */
10
class FunctionTarget extends FileTarget
11
{
12
13
    /**
14
     * 可执行函数名
15
     *
16
     * @var string
17
     */
18
    protected $function;
19
 
20
    public function __construct(string $name, array $parameter =[]) {
21
        $this->setParameter($parameter);
22
        $this->function = $name;
23
    }
24
    
25
    public function getRunnableTarget()
26
    {
27
        return $this->function;
28
    }
29
30
    /**
31
     * Get 需要的文件
32
     *
33
     * @return  string|null
34
     */
35
    public function getRequireFile()
36
    {
37
        return $this->requireFile;
38
    }
39
40
    /**
41
     * Set 需要的文件
42
     *
43
     * @param  string|null  $requireFile  需要的文件
44
     *
45
     * @return  self
46
     */
47
    public function setRequireFile($requireFile)
48
    {
49
        $this->requireFile = $requireFile;
50
        $this->setName($this->function.'@'.$requireFile);
51
        return $this;
52
    }
53
54
    /**
55
     * 是否可执行
56
     *
57
     * @return boolean
58
     */
59
    public function isVaild():bool
60
    {
61
        return  function_exists($this->function) || parent::isVaild();
62
    }
63
64
    /**
65
     * 执行代码
66
     *
67
     * @param array $parameter
68
     * @return mixed
69
     */
70
    public function invoke(array $parameter)
71
    {
72
        if (count($parameter) == 0) {
73
            $parameter = $this->getParameter();
74
        }
75
        if (!is_null($this->requireFile) && !function_exists($this->function)) {
76
            require_once $this->requireFile;
77
        }
78
        return forward_static_call_array($this->function, $parameter);
79
    }
80
}
81