Completed
Push — master ( e6b8db...c0bc47 )
by Christian
03:22
created

OperatingSystem::isBashCompatibleShell()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
/*
3
 * this file is part of magerun
4
 *
5
 * @author Tom Klingenberg <https://github.com/ktomk>
6
 */
7
8
namespace N98\Util;
9
10
/**
11
 * Class OperatingSystem
12
 *
13
 * @package N98\Util
14
 */
15
class OperatingSystem
16
{
17
    /**
18
     * @var int
19
     */
20
    const UID_ROOT = 0;
21
22
    /**
23
     * Returns true if operating system is
24
     * based on GNU linux.
25
     *
26
     * @return boolean
27
     */
28
    public static function isLinux()
29
    {
30
        return (bool) stristr(PHP_OS, 'linux');
31
    }
32
33
    /**
34
     * Returns true if operating system is
35
     * based on Microsoft Windows.
36
     *
37
     * @return boolean
38
     */
39
    public static function isWindows()
40
    {
41
        return strtolower(substr(PHP_OS, 0, 3)) === 'win';
42
    }
43
44
    /**
45
     * Returns true if operating system is
46
     * based on novell netware.
47
     *
48
     * @return boolean
49
     */
50
    public static function isNetware()
51
    {
52
        return (bool) stristr(PHP_OS, 'netware');
53
    }
54
55
    /**
56
     * Returns true if operating system is
57
     * based on apple MacOS.
58
     *
59
     * @return boolean
60
     */
61
    public static function isMacOs()
62
    {
63
        return stristr(PHP_OS, 'darwin') || stristr(PHP_OS, 'mac');
64
    }
65
66
    /**
67
     * @param string $program
68
     * @return bool
69
     */
70
    public static function isProgramInstalled($program)
71
    {
72
        if (self::isWindows()) {
73
            return WindowsSystem::isProgramInstalled($program);
74
        }
75
76
        return '' !== self::locateProgram($program);
77
    }
78
79
    /**
80
     * Returns the absolute path to the program that should be located or an empty string if the programm
81
     * could not be found.
82
     *
83
     * @param string $program
84
     * @return string
85
     */
86
    public static function locateProgram($program)
87
    {
88
        if (self::isWindows()) {
89
            return WindowsSystem::locateProgram($program);
90
        }
91
92
        $out = null;
93
        $return = null;
94
        @exec('which ' . $program, $out, $return);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
95
        return ($return === 0 && isset($out[0])) ? $out[0] : '';
96
    }
97
98
    /**
99
     * Home directory of the current user
100
     *
101
     * @return string|false false in case there is no environment variable related to the home directory
102
     */
103
    public static function getHomeDir()
104
    {
105
        if (self::isWindows()) {
106
            return getenv('USERPROFILE');
107
        }
108
109
        return getenv('HOME');
110
    }
111
112
    /**
113
     * Test for Root UID on a POSIX system if posix_getuid() is available.
114
     *
115
     * Returns false negatives if posix_getuid() is not available.
116
     *
117
     * @return bool
118
     */
119
    public static function isRoot()
120
    {
121
        return function_exists('posix_getuid') && posix_getuid() === self::UID_ROOT;
122
    }
123
124
    /**
125
     * get current working directory
126
     *
127
     * @return string the current working directory on success, or false on failure.
128
     */
129
    public static function getCwd()
130
    {
131
        return getcwd();
132
    }
133
134
    /**
135
     * Retrieve path to php binary
136
     *
137
     * @return string
138
     */
139
    public static function getPhpBinary()
140
    {
141
        // PHP_BINARY (>= php 5.4)
142
        if (defined('PHP_BINARY')) {
143
            return PHP_BINARY;
144
        }
145
146
        if (self::isWindows()) {
147
            return 'php';
148
        }
149
150
        return '/usr/bin/env php';
151
    }
152
153
    /**
154
     * Retrieve path to current php binary.
155
     *
156
     * @return string
157
     */
158
    public static function getCurrentPhpBinary()
159
    {
160
        if (isset($_SERVER['_'])) {
161
            return $_SERVER['_'];
162
        }
163
164
        return self::getPhpBinary();
165
    }
166
167
    /**
168
     * @return bool
169
     */
170
    public static function isBashCompatibleShell()
171
    {
172
        return in_array(
173
            basename(getenv('SHELL')),
174
            ['bash', 'zsh']
175
        );
176
    }
177
}
178