Completed
Push — master ( dde429...22637e )
by Andrii
20:00
created

Binary::detectPath()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 2
eloc 2
nc 2
nop 1
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
        return $exitcode;
43
    }
44
45
    /**
46
     * Setter for path.
47
     * @param string $value the path
48
     */
49
    public function setPath($value)
50
    {
51
        $this->_path = $value;
52
    }
53
54
    /**
55
     * Getter for path.
56
     * @return string
57
     */
58
    public function getPath()
59
    {
60
        if ($this->_path === null) {
61
            $this->_path = $this->detectPath($this->name);
62
        }
63
64
        return $this->_path;
65
    }
66
67
    /**
68
     * Detects how to run the binary with `which` utility.
69
     * @param string $name
70
     * @return string path to the binary
71
     */
72
    public function detectPath($name)
73
    {
74
        return exec('which ' . $name) ?: null;
75
    }
76
77
    /**
78
     * Setter for command.
79
     * @param string $value the command
80
     */
81
    public function setCommand($value)
82
    {
83
        $this->_command = $value;
84
    }
85
86
    /**
87
     * @return string full command to run the binary
88
     */
89
    public function getCommand()
90
    {
91
        if ($this->_command === null) {
92
            $this->_command = $this->detectCommand($this->getPath());
93
        }
94
95
        return $this->_command;
96
    }
97
98
    /**
99
     * Detects command to run the given path, e.g. /usr/bin/env php /the/dir/command.phar.
100
     * @return string path to the binary
101
     */
102
    public function detectCommand($path)
103
    {
104
        if (!$path || !file_exists($path)) {
105
            throw new InvalidConfigException('Failed to find how to run ' . $this->name);
106
        }
107
108
        return $path;
109
    }
110
111
    /**
112
     * Prepares given command arguments.
113
     * @param string|array $args
114
     * @return string
115
     */
116
    public function prepareArguments($args)
117
    {
118
        if (is_string($args)) {
119
            $res = ' ' . trim($args);
120
        } else {
121
            $res = '';
122
            foreach ($args as $a) {
123
                $res .= ' ' . escapeshellarg($a);
124
            }
125
        }
126
127
        return $res;
128
    }
129
}
130