Code Duplication    Length = 70-74 lines in 2 locations

src/base/BinaryPhp.php 1 location

@@ 16-85 (lines=70) @@
13
14
use Yii;
15
16
class BinaryPhp extends Binary
17
{
18
    /**
19
     * @var string package full name, e.g. fabpot/php-cs-fixer
20
     */
21
    public $package;
22
23
    /**
24
     * @var string package version constraint, e.g. ^1.1
25
     */
26
    public $version;
27
28
    /**
29
     * @var string installer URL
30
     */
31
    public $installer;
32
33
    /**
34
     * @var string URL to download PHAR
35
     */
36
    public $download;
37
38
    /**
39
     * Detects how to run the binary.
40
     * Searches in this order:
41
     * 1. PHAR in project's root directory
42
     * 2. projects's vendor/bin directory
43
     * 3. composer global vendor/bin directory.
44
     *
45
     * @param string $name
46
     * @return string path to the binary
47
     */
48
    public function detectPath($name)
49
    {
50
        $paths = [Yii::getAlias("@root/$name.phar", false), Yii::getAlias("@root/vendor/bin/$name", false), "$_SERVER[HOME]/.composer/vendor/bin/$name"];
51
        foreach ($paths as $path) {
52
            if (file_exists($path)) {
53
                return $path;
54
            }
55
        }
56
57
        return parent::detectPath($name);
58
    }
59
60
    /**
61
     * Detect command execution string.
62
     * @param mixed $path
63
     * @return string
64
     */
65
    public function detectCommand($path)
66
    {
67
        $path = parent::detectCommand($path);
68
69
        return is_executable($path) ? $path : '/usr/bin/env php ' . $path;
70
    }
71
72
    public function install()
73
    {
74
        if ($this->installer) {
75
            passthru('/usr/bin/env wget ' . escapeshellarg($this->installer) . ' -O- | /usr/bin/env php', $exitcode);
76
        } elseif ($this->download) {
77
            $dest = Yii::getAlias('@root/' . $this->name . '.phar', false);
78
            passthru('/usr/bin/env wget ' . escapeshellarg($this->download) . ' -O ' . $dest, $exitcode);
79
        } else {
80
            return parent::install();
81
        }
82
83
        return $exitcode;
84
    }
85
}
86

src/base/BinaryPython.php 1 location

@@ 16-89 (lines=74) @@
13
14
use Yii;
15
16
class BinaryPython extends Binary
17
{
18
    /**
19
     * @var string package full name, e.g. pytest
20
     */
21
    public $package;
22
23
    /**
24
     * @var string package version constraint, e.g. ^1.1
25
     */
26
    public $version;
27
28
    /**
29
     * @var string installer URL
30
     */
31
    public $installer;
32
33
    /**
34
     * @var string URL to download PHAR
35
     */
36
    public $download;
37
38
    /**
39
     * Detects how to run the binary.
40
     * Searches in this order:
41
     * 1. exexcutable in project's root directory
42
     * 2. XXX ??? vendor directory
43
     * 3. /home/user/.local/bin
44
     *
45
     * @param string $name
46
     * @return string path to the binary
47
     */
48
    public function detectPath($name)
49
    {
50
        $paths = [
51
            Yii::getAlias("@root/$name", false),
52
            Yii::getAlias("@root/env/bin/$name", false),
53
            "$_SERVER[HOME]/.local/bin/$name",
54
        ];
55
        foreach ($paths as $path) {
56
            if (file_exists($path)) {
57
                return $path;
58
            }
59
        }
60
61
        return parent::detectPath($name);
62
    }
63
64
    /**
65
     * Detect command execution string.
66
     * @param mixed $path
67
     * @return string
68
     */
69
    public function detectCommand($path)
70
    {
71
        $path = parent::detectCommand($path);
72
73
        return is_executable($path) ? $path : '/usr/bin/env python ' . $path;
74
    }
75
76
    public function install()
77
    {
78
        if ($this->installer) {
79
            passthru('/usr/bin/env wget ' . escapeshellarg($this->installer) . ' -O- | /usr/bin/env python', $exitcode);
80
        } elseif ($this->download) {
81
            $dest = Yii::getAlias('@root/' . $this->name, false);
82
            passthru('/usr/bin/env wget ' . escapeshellarg($this->download) . ' -O ' . $dest, $exitcode);
83
        } else {
84
            passthru("/usr/bin/pip install {$this->package}", $exitcode);
85
        }
86
87
        return $exitcode;
88
    }
89
}
90