Completed
Push — master ( 5955f6...652d4a )
by Andrii
03:36
created

Binary::setVcsignore()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
/*
4
 * Automation tool mixed with code generator for easier continuous development
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 BinariesController parent controller
21
     */
22
    public $controller;
23
24
    /**
25
     * @var string command name, eg.: phpunit, composer
26
     */
27
    public $name;
28
29
    /**
30
     * @var string full path to the binary, eg.: /usr/bin/git
31
     */
32
    protected $_path;
33
34
    /**
35
     * @var string full command to run the binary, possibly with added command runner, eg.: /usr/bin/env php /home/user/command.phar
36
     */
37
    protected $_command;
38
39
    /**
40
     * @var string package full name, e.g. pytest
41
     */
42
    public $package;
43
44
    /**
45
     * @var string package version constraint, e.g. ^1.1
46
     */
47
    public $version;
48
49
    /**
50
     * @var string installer URL
51
     */
52
    public $installer;
53
54
    /**
55
     * @var array installer options
56
     */
57
    public $installer_options = [];
58
59
    /**
60
     * @var string URL to download binary
61
     */
62
    public $download;
63
64
    /**
65
     * @var string filename to be added to VCS ignore file
66
     */
67
    protected $_vcsignore;
68
69
    /**
70
     * Prepares and runs with passthru, returns exit code.
71
     * @param string|array $args
72
     * @return int exit code
73
     */
74
    public function passthru($args = [])
75
    {
76
        passthru($this->prepareCommand($args), $exitcode);
77
78
        return $exitcode;
79
    }
80
81
    /**
82
     * Prepares and runs with exec, returns stdout array.
83
     * @param string|array $args
84
     * @param bool $returnExitCode default false
85
     * @return array|int stdout or exit code
86
     */
87
    public function exec($args = [], $returnExitCode = false)
88
    {
89
        exec($this->prepareCommand($args), $array, $exitcode);
90
91
        return $returnExitCode ? $exitcode : $array;
92
    }
93
94
    /**
95
     * Prepare full command line ready for execution.
96
     * @param string|array $args
97
     * @return string
98
     */
99
    public function prepareCommand($args)
100
    {
101
        $command = $this->getCommand();
102
        if (is_string($args)) {
103
            return $command . ' ' . trim($args);
104
        }
105
106
        foreach ($args as $arg) {
107
            if ($arg instanceof ModifierInterface) {
108
                $command = $arg->modify($command);
109
            } else {
110
                $command .= ' ' . escapeshellarg($arg);
111
            }
112
        }
113
114
        return $command;
115
    }
116
117
    public function install()
118
    {
119
        throw new InvalidConfigException('Don\'t know how to install ' . $this->name);
120
    }
121
122
    /**
123
     * Setter for path.
124
     * @param string $value the path
125
     */
126
    public function setPath($value)
127
    {
128
        $this->_path = $value;
129
    }
130
131
    /**
132
     * Getter for path.
133
     * @return string
134
     */
135
    public function getPath()
136
    {
137
        if (!$this->_path) {
138
            $this->_path = $this->detectPath($this->name);
139
        }
140
141
        return $this->_path;
142
    }
143
144
    /**
145
     * Detects how to run the binary with `which` utility.
146
     * @param string $name
147
     * @return string path to the binary
148
     */
149
    public function detectPath($name)
150
    {
151
        return exec('which ' . $name) ?: null;
152
    }
153
154
    /**
155
     * Setter for command.
156
     * @param string $value the command
157
     */
158
    public function setCommand($value)
159
    {
160
        $this->_command = $value;
161
    }
162
163
    /**
164
     * @return string full command to run the binary
165
     */
166
    public function getCommand()
167
    {
168
        if ($this->_command === null) {
169
            $this->_command = $this->detectCommand($this->getPath());
170
        }
171
172
        return $this->_command;
173
    }
174
175
    /**
176
     * Detects command to run the given path, e.g. /usr/bin/env php /the/dir/command.phar.
177
     * @return string path to the binary
178
     */
179
    public function detectCommand($path)
180
    {
181
        if (!$path) {
182
            $this->install();
183
            $path = $this->getPath();
184
        }
185
        if (!$path || !file_exists($path)) {
186
            throw new InvalidConfigException('Failed to find how to run ' . $this->name);
187
        }
188
189
        return $path;
190
    }
191
    public function setVcsignore($value)
192
    {
193
        $this->_vcsignore = $value;
194
    }
195
196
    public function getVcsignore()
197
    {
198
        return $this->_vcsignore;
199
    }
200
}
201