Completed
Push — master ( 4141d9...f493e8 )
by Andrii
02:47
created

Binary::install()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
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) 2014-2016, HiQDev (http://hiqdev.com/)
10
 */
11
12
namespace hidev\base;
13
14
use yii\base\InvalidConfigException;
15
16
class Binary extends \yii\base\Object
17
{
18
    /**
19
     * @var string command name, eg.: phpunit, composer
20
     */
21
    public $name;
22
23
    /**
24
     * @var string full path to the binary, eg.: /usr/bin/git
25
     */
26
    protected $_path;
27
28
    /**
29
     * @var string full command to run the binary, possibly with added command runner, eg.: /usr/bin/env php /home/user/command.phar
30
     */
31
    protected $_command;
32
33
    /**
34
     * Prepares and runs with passthru.
35
     * @param string|array $args
36
     * @return int exit code
37
     */
38
    public function passthru($args = [])
39
    {
40
        // die(  $this->getCommand() . $this->prepareArguments($args));
0 ignored issues
show
Unused Code Comprehensibility introduced by
65% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
41
        passthru($this->getCommand() . $this->prepareArguments($args), $exitcode);
42
43
        return $exitcode;
44
    }
45
46
    public function install()
47
    {
48
        throw new InvalidConfigException('Don\'t know how to install ' . $this->name);
49
    }
50
51
    /**
52
     * Setter for path.
53
     * @param string $value the path
54
     */
55
    public function setPath($value)
56
    {
57
        $this->_path = $value;
58
    }
59
60
    /**
61
     * Getter for path.
62
     * @return string
63
     */
64
    public function getPath()
65
    {
66
        if (!$this->_path) {
67
            $this->_path = $this->detectPath($this->name);
68
        }
69
70
        return $this->_path;
71
    }
72
73
    /**
74
     * Detects how to run the binary with `which` utility.
75
     * @param string $name
76
     * @return string path to the binary
77
     */
78
    public function detectPath($name)
79
    {
80
        return exec('which ' . $name) ?: null;
81
    }
82
83
    /**
84
     * Setter for command.
85
     * @param string $value the command
86
     */
87
    public function setCommand($value)
88
    {
89
        $this->_command = $value;
90
    }
91
92
    /**
93
     * @return string full command to run the binary
94
     */
95
    public function getCommand()
96
    {
97
        if ($this->_command === null) {
98
            $this->_command = $this->detectCommand($this->getPath());
99
        }
100
101
        return $this->_command;
102
    }
103
104
    /**
105
     * Detects command to run the given path, e.g. /usr/bin/env php /the/dir/command.phar.
106
     * @return string path to the binary
107
     */
108
    public function detectCommand($path)
109
    {
110
        if (!$path) {
111
            $this->install();
112
            $path = $this->getPath();
113
        }
114
        if (!$path || !file_exists($path)) {
115
            throw new InvalidConfigException('Failed to find how to run ' . $this->name);
116
        }
117
118
        return $path;
119
    }
120
121
    /**
122
     * Prepares given command arguments.
123
     * @param string|array $args
124
     * @return string
125
     */
126
    public function prepareArguments($args)
127
    {
128
        if (is_string($args)) {
129
            $res = ' ' . trim($args);
130
        } else {
131
            $res = '';
132
            foreach ($args as $a) {
133
                $res .= ' ' . escapeshellarg($a);
134
            }
135
        }
136
137
        return $res;
138
    }
139
}
140