Passed
Push — master ( 67aa31...d47bdf )
by Kirill
03:20
created

System::isCLI()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 12
rs 10
cc 3
nc 3
nop 0
1
<?php
2
3
/**
4
 * Spiral Framework.
5
 *
6
 * @license   MIT
7
 * @author    Anton Titov (Wolfy-J)
8
 */
9
10
declare(strict_types=1);
11
12
namespace Spiral\Debug;
13
14
/**
15
 * Describes the env PHP script is running within.
16
 */
17
final class System
18
{
19
    /**
20
     * Return true if PHP running in CLI mode.
21
     *
22
     * @codeCoverageIgnore
23
     * @return bool
24
     */
25
    public static function isCLI(): bool
26
    {
27
        if (!empty(getenv('RR'))) {
28
            // Do not treat RoadRunner as CLI.
29
            return false;
30
        }
31
32
        if (php_sapi_name() === 'cli') {
33
            return true;
34
        }
35
36
        return false;
37
    }
38
39
    /**
40
     * Returns true if the STDOUT supports colorization.
41
     *
42
     * @codeCoverageIgnore
43
     * @link https://github.com/symfony/Console/blob/master/Output/StreamOutput.php#L94
44
     * @param mixed $stream
45
     * @return bool
46
     */
47
    public static function isColorsSupported($stream = STDOUT): bool
48
    {
49
        if ('Hyper' === getenv('TERM_PROGRAM')) {
50
            return true;
51
        }
52
53
        try {
54
            if (\DIRECTORY_SEPARATOR === '\\') {
55
                return (
56
                        function_exists('sapi_windows_vt100_support')
57
                        && @sapi_windows_vt100_support($stream)
58
                    ) || getenv('ANSICON') !== false
59
                    || getenv('ConEmuANSI') == 'ON'
60
                    || getenv('TERM') == 'xterm';
61
            }
62
63
            if (\function_exists('stream_isatty')) {
64
                return @stream_isatty($stream);
65
            }
66
67
            if (\function_exists('posix_isatty')) {
68
                return @posix_isatty($stream);
69
            }
70
71
            $stat = @fstat($stream);
72
73
            // Check if formatted mode is S_IFCHR
74
            return $stat ? 0020000 === ($stat['mode'] & 0170000) : false;
75
        } catch (\Throwable $e) {
76
            return false;
77
        }
78
    }
79
}
80