Completed
Pull Request — master (#116)
by Théo
02:32
created

IO   A

Complexity

Total Complexity 31

Size/Duplication

Total Lines 171
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 31
dl 0
loc 171
rs 9.8
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A isInteractive() 0 3 1
A getVerbosity() 0 3 1
A __construct() 0 8 1
A parseInput() 0 3 1
B checkInteractivity() 0 17 6
A hasParameter() 0 14 3
A hasColorSupport() 0 3 1
B checkColorSupport() 0 19 8
D configureVerbosity() 0 37 9
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
13
namespace KevinGH\Box\RequirementChecker;
14
15
/**
16
 * The code in this file must be PHP 5.3+ compatible as is used to know if the application can be run.
17
 *
18
 * @private
19
 */
20
final class IO
21
{
22
    const VERBOSITY_QUIET = 16;
23
    const VERBOSITY_NORMAL = 32;
24
    const VERBOSITY_VERBOSE = 64;
25
    const VERBOSITY_VERY_VERBOSE = 128;
26
    const VERBOSITY_DEBUG = 256;
27
28
    private $interactive;
29
    private $verbosity = self::VERBOSITY_NORMAL;
30
    private $colorSupport;
31
    private $options;
32
33
    public function __construct()
34
    {
35
        $this->parseInput();
36
37
        $shellVerbosity = $this->configureVerbosity();
38
39
        $this->interactive = $this->checkInteractivity($shellVerbosity);
40
        $this->colorSupport = $this->checkColorSupport();
41
    }
42
43
    /**
44
     * @return bool
45
     */
46
    public function isInteractive()
47
    {
48
        return $this->interactive;
49
    }
50
51
    /**
52
     * @return int
53
     */
54
    public function getVerbosity()
55
    {
56
        return $this->verbosity;
57
    }
58
59
    /**
60
     * @return bool
61
     */
62
    public function hasColorSupport()
63
    {
64
        return $this->colorSupport;
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70
    public function hasParameter($values)
71
    {
72
        $values = (array) $values;
73
74
        foreach ($values as $value) {
75
            $regexp = sprintf(
76
                '/\s%s\b/',
77
                str_replace(' ', '\s+', preg_quote($value, '/'))
78
            );
79
            if (preg_match($regexp, $this->options) === 1) {
80
                return true;
81
            }
82
        }
83
        return false;
84
    }
85
86
    /**
87
     * @return array
88
     */
89
    private function parseInput()
90
    {
91
        $this->options = implode(' ', $_SERVER['argv']);
92
    }
93
94
    /**
95
     * @param int $shellVerbosity
96
     *
97
     * @return bool
98
     */
99
    private function checkInteractivity($shellVerbosity)
100
    {
101
        if (-1 === $shellVerbosity) {
102
            return false;
103
        }
104
105
        if (true === $this->hasParameter(array('--no-interaction', '-n'))) {
106
            return false;
107
        }
108
109
        if (function_exists('posix_isatty')) {
110
            if (!@posix_isatty(STDOUT) && false === getenv('SHELL_INTERACTIVE')) {
111
                return false;
112
            }
113
        }
114
115
        return true;
116
    }
117
118
    /**
119
     * @return int
120
     */
121
    private function configureVerbosity()
122
    {
123
        switch ($shellVerbosity = (int) getenv('SHELL_VERBOSITY')) {
124
            case -1:
125
                $this->verbosity = self::VERBOSITY_QUIET;
126
                break;
127
            case 1:
128
                $this->verbosity = self::VERBOSITY_VERBOSE;
129
                break;
130
            case 2:
131
                $this->verbosity = self::VERBOSITY_VERY_VERBOSE;
132
                break;
133
            case 3:
134
                $this->verbosity = self::VERBOSITY_DEBUG;
135
                break;
136
            default:
137
                $shellVerbosity = 0;
138
                break;
139
        }
140
141
        if ($this->hasParameter(array('--quiet', '-q'))) {
142
            $this->verbosity = self::VERBOSITY_QUIET;
143
            $shellVerbosity = -1;
144
        } else {
145
            if ($this->hasParameter(array('-vvv', '--verbose=3', '--verbose 3'))) {
146
                $this->verbosity = self::VERBOSITY_DEBUG;
147
                $shellVerbosity = 3;
148
            } elseif ($this->hasParameter(array('-vv', '--verbose=2', '--verbose 2'))) {
149
                $this->verbosity = self::VERBOSITY_VERY_VERBOSE;
150
                $shellVerbosity = 2;
151
            } elseif ($this->hasParameter(array('-v', '--verbose=1', '--verbose 1', '--verbose'))) {
152
                $this->verbosity = self::VERBOSITY_VERBOSE;
153
                $shellVerbosity = 1;
154
            }
155
        }
156
157
        return $shellVerbosity;
158
    }
159
160
    /**
161
     * Returns true if the stream supports colorization.
162
     *
163
     * Colorization is disabled if not supported by the stream:
164
     *
165
     *  -  Windows != 10.0.10586 without Ansicon, ConEmu or Mintty
166
     *  -  non tty consoles
167
     *
168
     * @return bool true if the stream supports colorization, false otherwise
169
     *
170
     * @see \Symfony\Component\Console\Output\StreamOutput
171
     */
172
    private function checkColorSupport()
173
    {
174
        if ($this->hasParameter(array('--ansi'))) {
175
            return true;
176
        }
177
178
        if ($this->hasParameter(array('--no-ansi'))) {
179
            return false;
180
        }
181
182
        if (DIRECTORY_SEPARATOR === '\\') {
183
            return
184
                '10.0.10586' === PHP_WINDOWS_VERSION_MAJOR . '.' . PHP_WINDOWS_VERSION_MINOR . '.' . PHP_WINDOWS_VERSION_BUILD
185
                || false !== getenv('ANSICON')
186
                || 'ON' === getenv('ConEmuANSI')
187
                || 'xterm' === getenv('TERM');
188
        }
189
190
        return function_exists('posix_isatty') && @posix_isatty(STDOUT);
191
    }
192
}