1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare (strict_types=1); |
4
|
|
|
namespace HumbugBox451\KevinGH\RequirementChecker; |
5
|
|
|
|
6
|
|
|
use function fstat; |
7
|
|
|
use function function_exists; |
8
|
|
|
use function getenv; |
9
|
|
|
use function implode; |
10
|
|
|
use function posix_isatty; |
11
|
|
|
use function preg_match; |
12
|
|
|
use function preg_quote; |
13
|
|
|
use function sapi_windows_vt100_support; |
14
|
|
|
use function sprintf; |
15
|
|
|
use function str_replace; |
16
|
|
|
use function stream_isatty; |
17
|
|
|
use const DIRECTORY_SEPARATOR; |
18
|
|
|
use const STDOUT; |
19
|
|
|
/** @internal */ |
20
|
|
|
final class IO |
21
|
|
|
{ |
22
|
|
|
public const VERBOSITY_QUIET = 16; |
23
|
|
|
public const VERBOSITY_NORMAL = 32; |
24
|
|
|
public const VERBOSITY_VERBOSE = 64; |
25
|
|
|
public const VERBOSITY_VERY_VERBOSE = 128; |
26
|
|
|
public const VERBOSITY_DEBUG = 256; |
27
|
|
|
private $interactive; |
28
|
|
|
private $verbosity = self::VERBOSITY_NORMAL; |
29
|
|
|
private $colorSupport; |
30
|
|
|
private $options; |
31
|
|
|
public function __construct() |
32
|
|
|
{ |
33
|
|
|
$this->options = implode(' ', $_SERVER['argv']); |
34
|
|
|
$shellVerbosity = $this->configureVerbosity(); |
35
|
|
|
$this->interactive = $this->checkInteractivity($shellVerbosity); |
36
|
|
|
$this->colorSupport = $this->checkColorSupport(); |
37
|
|
|
} |
38
|
|
|
public function isInteractive() : bool |
39
|
|
|
{ |
40
|
|
|
return $this->interactive; |
41
|
|
|
} |
42
|
|
|
public function getVerbosity() : int |
43
|
|
|
{ |
44
|
|
|
return $this->verbosity; |
45
|
|
|
} |
46
|
|
|
public function hasColorSupport() : bool |
47
|
|
|
{ |
48
|
|
|
return $this->colorSupport; |
49
|
|
|
} |
50
|
|
|
public function hasParameter($values) : bool |
51
|
|
|
{ |
52
|
|
|
$values = (array) $values; |
53
|
|
|
foreach ($values as $value) { |
54
|
|
|
$regexp = sprintf('/\\s%s\\b/', str_replace(' ', '\\s+', preg_quote($value, '/'))); |
55
|
|
|
if (1 === preg_match($regexp, $this->options)) { |
56
|
|
|
return \true; |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
return \false; |
60
|
|
|
} |
61
|
|
|
private function checkInteractivity(int $shellVerbosity) : bool |
62
|
|
|
{ |
63
|
|
|
if (-1 === $shellVerbosity) { |
64
|
|
|
return \false; |
65
|
|
|
} |
66
|
|
|
if (\true === $this->hasParameter(['--no-interaction', '-n'])) { |
67
|
|
|
return \false; |
68
|
|
|
} |
69
|
|
|
if (function_exists('posix_isatty') && !@posix_isatty(STDOUT) && \false === getenv('SHELL_INTERACTIVE')) { |
70
|
|
|
return \false; |
71
|
|
|
} |
72
|
|
|
return \true; |
73
|
|
|
} |
74
|
|
|
private function configureVerbosity() : int |
75
|
|
|
{ |
76
|
|
|
switch ($shellVerbosity = (int) getenv('SHELL_VERBOSITY')) { |
77
|
|
|
case -1: |
78
|
|
|
$this->verbosity = self::VERBOSITY_QUIET; |
79
|
|
|
break; |
80
|
|
|
case 1: |
81
|
|
|
$this->verbosity = self::VERBOSITY_VERBOSE; |
82
|
|
|
break; |
83
|
|
|
case 2: |
84
|
|
|
$this->verbosity = self::VERBOSITY_VERY_VERBOSE; |
85
|
|
|
break; |
86
|
|
|
case 3: |
87
|
|
|
$this->verbosity = self::VERBOSITY_DEBUG; |
88
|
|
|
break; |
89
|
|
|
default: |
90
|
|
|
$shellVerbosity = 0; |
91
|
|
|
break; |
92
|
|
|
} |
93
|
|
|
if ($this->hasParameter(['--quiet', '-q'])) { |
94
|
|
|
$this->verbosity = self::VERBOSITY_QUIET; |
95
|
|
|
$shellVerbosity = -1; |
96
|
|
|
} elseif ($this->hasParameter(['-vvv', '--verbose=3', '--verbose 3'])) { |
97
|
|
|
$this->verbosity = self::VERBOSITY_DEBUG; |
98
|
|
|
$shellVerbosity = 3; |
99
|
|
|
} elseif ($this->hasParameter(['-vv', '--verbose=2', '--verbose 2'])) { |
100
|
|
|
$this->verbosity = self::VERBOSITY_VERY_VERBOSE; |
101
|
|
|
$shellVerbosity = 2; |
102
|
|
|
} elseif ($this->hasParameter(['-v', '--verbose=1', '--verbose 1', '--verbose'])) { |
103
|
|
|
$this->verbosity = self::VERBOSITY_VERBOSE; |
104
|
|
|
$shellVerbosity = 1; |
105
|
|
|
} |
106
|
|
|
return $shellVerbosity; |
107
|
|
|
} |
108
|
|
|
private function checkColorSupport() : bool |
109
|
|
|
{ |
110
|
|
|
if ($this->hasParameter(['--ansi'])) { |
111
|
|
|
return \true; |
112
|
|
|
} |
113
|
|
|
if ($this->hasParameter(['--no-ansi'])) { |
114
|
|
|
return \false; |
115
|
|
|
} |
116
|
|
|
if (DIRECTORY_SEPARATOR === '\\') { |
117
|
|
|
return function_exists('sapi_windows_vt100_support') && sapi_windows_vt100_support(STDOUT) || \false !== getenv('ANSICON') || 'ON' === getenv('ConEmuANSI') || 'xterm' === getenv('TERM'); |
118
|
|
|
} |
119
|
|
|
if (function_exists('stream_isatty')) { |
120
|
|
|
return stream_isatty(STDOUT); |
121
|
|
|
} |
122
|
|
|
if (function_exists('posix_isatty')) { |
123
|
|
|
return posix_isatty(STDOUT); |
124
|
|
|
} |
125
|
|
|
$stat = fstat(STDOUT); |
126
|
|
|
return $stat ? 020000 === ($stat['mode'] & 0170000) : \false; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|