Passed
Push — master ( f2e76c...2ddbad )
by 世昌
03:02
created

FileTarget   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 69
rs 10
c 0
b 0
f 0
wmc 8

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getRequireFile() 0 3 1
A __construct() 0 3 1
A getRunnableTarget() 0 3 1
A isVaild() 0 3 2
A setRequireFile() 0 5 1
A apply() 0 9 2
1
<?php
2
namespace nebula\runnable\target;
3
4
use nebula\runnable\target\RunnableTarget;
5
6
/**
7
 * 可执行命令:文件类型
8
 * 
9
 */
10
class FileTarget extends RunnableTarget
11
{
12
13
    /**
14
     * 需要的文件
15
     *
16
     * @var string|null
17
     */
18
    protected $requireFile=null;
19
    
20
    public function __construct(string $path, array $parameter =[]) {
21
        $this->setRequireFile($path);
22
        $this->setParameter($parameter);
23
    }
24
25
    public function getRunnableTarget()
26
    {
27
        return null;
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->name ='@'.$requireFile;
51
        return $this;
52
    }
53
54
    /**
55
     * 是否可执行
56
     *
57
     * @return boolean
58
     */
59
    public function isVaild():bool
60
    {
61
        return $this->requireFile !== null && file_exists($this->requireFile);
62
    }
63
64
    /**
65
     * 执行代码
66
     *
67
     * @param array $args
68
     * @return mixed
69
     */
70
    public function apply(array $args)
71
    {
72
        if (count($args) == 0) {
73
            $args = $this->getParameter();
74
        }
75
        array_unshift($args, $this->requireFile);
76
        $_SERVER['argv']=$args;
77
        $_SERVER['args']=count($args);
78
        return include $this->requireFile;
79
    }
80
}
81