Passed
Pull Request — master (#155)
by Théo
02:31
created

IO::checkInteractivity()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 8.8571
c 0
b 0
f 0
cc 6
eloc 8
nc 5
nop 1
1
<?php
2
3
/*
4
 * This file is part of the box project.
5
 *
6
 * (c) Kevin Herrera <[email protected]>
7
 *     Théo Fidry <[email protected]>
8
 *
9
 * This source file is subject to the MIT license that is bundled
10
 * with this source code in the file LICENSE.
11
 */
12
namespace _HumbugBox5addf3ce683e7\KevinGH\RequirementChecker;
13
14
/**
15
 * The code in this file must be PHP 5.3+ compatible as is used to know if the application can be run.
16
 *
17
 * @private
18
 */
19
final class IO
20
{
21
    const VERBOSITY_QUIET = 16;
22
    const VERBOSITY_NORMAL = 32;
23
    const VERBOSITY_VERBOSE = 64;
24
    const VERBOSITY_VERY_VERBOSE = 128;
25
    const VERBOSITY_DEBUG = 256;
26
    private $interactive;
27
    private $verbosity = self::VERBOSITY_NORMAL;
28
    private $colorSupport;
29
    private $options;
30
    public function __construct()
31
    {
32
        $this->options = \implode(' ', $_SERVER['argv']);
33
        $shellVerbosity = $this->configureVerbosity();
34
        $this->interactive = $this->checkInteractivity($shellVerbosity);
35
        $this->colorSupport = $this->checkColorSupport();
36
    }
37
    /**
38
     * @return bool
39
     */
40
    public function isInteractive()
41
    {
42
        return $this->interactive;
43
    }
44
    /**
45
     * @return int
46
     */
47
    public function getVerbosity()
48
    {
49
        return $this->verbosity;
50
    }
51
    /**
52
     * @return bool
53
     */
54
    public function hasColorSupport()
55
    {
56
        return $this->colorSupport;
57
    }
58
    /**
59
     * @param mixed
60
     * @param mixed $values
61
     *
62
     * @return bool
63
     */
64
    public function hasParameter($values)
65
    {
66
        $values = (array) $values;
67
        foreach ($values as $value) {
68
            $regexp = \sprintf('/\\s%s\\b/', \str_replace(' ', '\\s+', \preg_quote($value, '/')));
69
            if (1 === \preg_match($regexp, $this->options)) {
70
                return \true;
71
            }
72
        }
73
        return \false;
74
    }
75
    /**
76
     * @param int $shellVerbosity
77
     *
78
     * @return bool
79
     */
80
    private function checkInteractivity($shellVerbosity)
81
    {
82
        if (-1 === $shellVerbosity) {
83
            return \false;
84
        }
85
        if (\true === $this->hasParameter(array('--no-interaction', '-n'))) {
86
            return \false;
87
        }
88
        if (\function_exists('posix_isatty')) {
89
            if (!@\posix_isatty(\STDOUT) && \false === \getenv('SHELL_INTERACTIVE')) {
90
                return \false;
91
            }
92
        }
93
        return \true;
94
    }
95
    /**
96
     * @return int
97
     */
98
    private function configureVerbosity()
99
    {
100
        switch ($shellVerbosity = (int) \getenv('SHELL_VERBOSITY')) {
101
            case -1:
102
                $this->verbosity = self::VERBOSITY_QUIET;
103
                break;
104
            case 1:
105
                $this->verbosity = self::VERBOSITY_VERBOSE;
106
                break;
107
            case 2:
108
                $this->verbosity = self::VERBOSITY_VERY_VERBOSE;
109
                break;
110
            case 3:
111
                $this->verbosity = self::VERBOSITY_DEBUG;
112
                break;
113
            default:
114
                $shellVerbosity = 0;
115
                break;
116
        }
117
        if ($this->hasParameter(array('--quiet', '-q'))) {
118
            $this->verbosity = self::VERBOSITY_QUIET;
119
            $shellVerbosity = -1;
120
        } else {
121
            if ($this->hasParameter(array('-vvv', '--verbose=3', '--verbose 3'))) {
122
                $this->verbosity = self::VERBOSITY_DEBUG;
123
                $shellVerbosity = 3;
124
            } elseif ($this->hasParameter(array('-vv', '--verbose=2', '--verbose 2'))) {
125
                $this->verbosity = self::VERBOSITY_VERY_VERBOSE;
126
                $shellVerbosity = 2;
127
            } elseif ($this->hasParameter(array('-v', '--verbose=1', '--verbose 1', '--verbose'))) {
128
                $this->verbosity = self::VERBOSITY_VERBOSE;
129
                $shellVerbosity = 1;
130
            }
131
        }
132
        return $shellVerbosity;
133
    }
134
    /**
135
     * Returns true if the stream supports colorization.
136
     *
137
     * Colorization is disabled if not supported by the stream:
138
     *
139
     *  -  Windows != 10.0.10586 without Ansicon, ConEmu or Mintty
140
     *  -  non tty consoles
141
     *
142
     * @return bool true if the stream supports colorization, false otherwise
143
     *
144
     * @see \Symfony\Component\Console\Output\StreamOutput
145
     */
146
    private function checkColorSupport()
147
    {
148
        if ($this->hasParameter(array('--ansi'))) {
149
            return \true;
150
        }
151
        if ($this->hasParameter(array('--no-ansi'))) {
152
            return \false;
153
        }
154
        if (\DIRECTORY_SEPARATOR === '\\') {
155
            return \function_exists('sapi_windows_vt100_support') && sapi_windows_vt100_support(\STDOUT) || \false !== \getenv('ANSICON') || 'ON' === \getenv('ConEmuANSI') || 'xterm' === \getenv('TERM');
156
        }
157
        if (\function_exists('stream_isatty')) {
158
            return \stream_isatty(\STDOUT);
159
        }
160
        if (\function_exists('posix_isatty')) {
161
            return \posix_isatty(\STDOUT);
162
        }
163
        $stat = \fstat(\STDOUT);
164
        // Check if formatted mode is S_IFCHR
165
        return $stat ? 020000 === ($stat['mode'] & 0170000) : \false;
166
    }
167
}
168