Completed
Push — master ( d24069...59bb94 )
by Andrii
03:18
created

Binary::prepareArguments()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12
Metric Value
dl 0
loc 13
ccs 0
cts 12
cp 0
rs 9.4285
cc 3
eloc 8
nc 2
nop 1
crap 12
1
<?php
2
3
/*
4
 * Task runner, code generator and build tool for easier continuos integration
5
 *
6
 * @link      https://github.com/hiqdev/hidev
7
 * @package   hidev
8
 * @license   BSD-3-Clause
9
 * @copyright Copyright (c) 2015-2016, HiQDev (http://hiqdev.com/)
10
 */
11
12
namespace hidev\base;
13
14
use hidev\modifiers\ModifierInterface;
15
use yii\base\InvalidConfigException;
16
17
class Binary extends \yii\base\Object
18
{
19
    /**
20
     * @var string command name, eg.: phpunit, composer
21
     */
22
    public $name;
23
24
    /**
25
     * @var string full path to the binary, eg.: /usr/bin/git
26
     */
27
    protected $_path;
28
29
    /**
30
     * @var string full command to run the binary, possibly with added command runner, eg.: /usr/bin/env php /home/user/command.phar
31
     */
32
    protected $_command;
33
34
    /**
35
     * Prepares and runs with passthru, returns exit code.
36
     * @param string|array $args
37
     * @return int exit code
38
     */
39
    public function passthru($args = [])
40
    {
41
        passthru($this->prepareCommand($args), $exitcode);
42
43
        return $exitcode;
44
    }
45
46
    /**
47
     * Prepares and runs with exec, returns stdout array.
48
     * @param string|array $args
49
     * @param bool $returnExitCode default false
50
     * @return array|int stdout or exit code
51
     */
52
    public function exec($args = [], $returnExitCode = false)
53
    {
54
        exec($this->prepareCommand($args), $array, $exitcode);
55
56
        return $returnExitCode ? $exitcode : $array;
57
    }
58
59
    /**
60
     * Prepare full command line ready for execution.
61
     * @param string|array $args
62
     * @return string
63
     */
64
    public function prepareCommand($args)
65
    {
66
        $command = $this->getCommand();
67
        if (is_string($args)) {
68
            return $command . ' ' . trim($args);
69
        }
70
71
        foreach ($args as $arg) {
72
            if ($arg instanceof ModifierInterface) {
73
                $command = $arg->modify($command);
74
            } else {
75
                $command .= ' ' . escapeshellarg($arg);
76
            }
77
        }
78
79
        return $command;
80
    }
81
82
    public function install()
83
    {
84
        throw new InvalidConfigException('Don\'t know how to install ' . $this->name);
85
    }
86
87
    /**
88
     * Setter for path.
89
     * @param string $value the path
90
     */
91
    public function setPath($value)
92
    {
93
        $this->_path = $value;
94
    }
95
96
    /**
97
     * Getter for path.
98
     * @return string
99
     */
100
    public function getPath()
101
    {
102
        if (!$this->_path) {
103
            $this->_path = $this->detectPath($this->name);
104
        }
105
106
        return $this->_path;
107
    }
108
109
    /**
110
     * Detects how to run the binary with `which` utility.
111
     * @param string $name
112
     * @return string path to the binary
113
     */
114
    public function detectPath($name)
115
    {
116
        return exec('which ' . $name) ?: null;
117
    }
118
119
    /**
120
     * Setter for command.
121
     * @param string $value the command
122
     */
123
    public function setCommand($value)
124
    {
125
        $this->_command = $value;
126
    }
127
128
    /**
129
     * @return string full command to run the binary
130
     */
131
    public function getCommand()
132
    {
133
        if ($this->_command === null) {
134
            $this->_command = $this->detectCommand($this->getPath());
135
        }
136
137
        return $this->_command;
138
    }
139
140
    /**
141
     * Detects command to run the given path, e.g. /usr/bin/env php /the/dir/command.phar.
142
     * @return string path to the binary
143
     */
144
    public function detectCommand($path)
145
    {
146
        if (!$path) {
147
            $this->install();
148
            $path = $this->getPath();
149
        }
150
        if (!$path || !file_exists($path)) {
151
            throw new InvalidConfigException('Failed to find how to run ' . $this->name);
152
        }
153
154
        return $path;
155
    }
156
}
157