comparison vendor/sebastian/environment/src/Console.php @ 14:1fec387a4317

Update Drupal core to 8.5.2 via Composer
author Chris Cannam
date Mon, 23 Apr 2018 09:46:53 +0100
parents 4c8ae668cc8c
children
comparison
equal deleted inserted replaced
13:5fb285c0d0e3 14:1fec387a4317
1 <?php 1 <?php
2 /* 2 /*
3 * This file is part of the Environment package. 3 * This file is part of sebastian/environment.
4 * 4 *
5 * (c) Sebastian Bergmann <sebastian@phpunit.de> 5 * (c) Sebastian Bergmann <sebastian@phpunit.de>
6 * 6 *
7 * For the full copyright and license information, please view the LICENSE 7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code. 8 * file that was distributed with this source code.
9 */ 9 */
10 10
11 declare(strict_types=1);
12
11 namespace SebastianBergmann\Environment; 13 namespace SebastianBergmann\Environment;
12 14
13 /** 15 final class Console
14 */
15 class Console
16 { 16 {
17 /**
18 * @var int
19 */
17 const STDIN = 0; 20 const STDIN = 0;
21
22 /**
23 * @var int
24 */
18 const STDOUT = 1; 25 const STDOUT = 1;
26
27 /**
28 * @var int
29 */
19 const STDERR = 2; 30 const STDERR = 2;
20 31
21 /** 32 /**
22 * Returns true if STDOUT supports colorization. 33 * Returns true if STDOUT supports colorization.
23 * 34 *
24 * This code has been copied and adapted from 35 * This code has been copied and adapted from
25 * Symfony\Component\Console\Output\OutputStream. 36 * Symfony\Component\Console\Output\OutputStream.
26 *
27 * @return bool
28 */ 37 */
29 public function hasColorSupport() 38 public function hasColorSupport(): bool
30 { 39 {
31 if (DIRECTORY_SEPARATOR == '\\') { 40 if ($this->isWindows()) {
32 return false !== getenv('ANSICON') || 'ON' === getenv('ConEmuANSI') || 'xterm' === getenv('TERM'); 41 // @codeCoverageIgnoreStart
42 return false !== \getenv('ANSICON') || 'ON' === \getenv('ConEmuANSI') || 'xterm' === \getenv('TERM');
43 // @codeCoverageIgnoreEnd
33 } 44 }
34 45
35 if (!defined('STDOUT')) { 46 if (!\defined('STDOUT')) {
47 // @codeCoverageIgnoreStart
36 return false; 48 return false;
49 // @codeCoverageIgnoreEnd
37 } 50 }
38 51
39 return $this->isInteractive(STDOUT); 52 return $this->isInteractive(STDOUT);
40 } 53 }
41 54
42 /** 55 /**
43 * Returns the number of columns of the terminal. 56 * Returns the number of columns of the terminal.
44 * 57 *
45 * @return int 58 * @codeCoverageIgnore
46 */ 59 */
47 public function getNumberOfColumns() 60 public function getNumberOfColumns(): int
48 { 61 {
49 if (DIRECTORY_SEPARATOR == '\\') { 62 if ($this->isWindows()) {
50 $columns = 80; 63 return $this->getNumberOfColumnsWindows();
51
52 if (preg_match('/^(\d+)x\d+ \(\d+x(\d+)\)$/', trim(getenv('ANSICON')), $matches)) {
53 $columns = $matches[1];
54 } elseif (function_exists('proc_open')) {
55 $process = proc_open(
56 'mode CON',
57 array(
58 1 => array('pipe', 'w'),
59 2 => array('pipe', 'w')
60 ),
61 $pipes,
62 null,
63 null,
64 array('suppress_errors' => true)
65 );
66
67 if (is_resource($process)) {
68 $info = stream_get_contents($pipes[1]);
69
70 fclose($pipes[1]);
71 fclose($pipes[2]);
72 proc_close($process);
73
74 if (preg_match('/--------+\r?\n.+?(\d+)\r?\n.+?(\d+)\r?\n/', $info, $matches)) {
75 $columns = $matches[2];
76 }
77 }
78 }
79
80 return $columns - 1;
81 } 64 }
82 65
83 if (!$this->isInteractive(self::STDIN)) { 66 if (!$this->isInteractive(self::STDIN)) {
84 return 80; 67 return 80;
85 } 68 }
86 69
87 if (function_exists('shell_exec') && preg_match('#\d+ (\d+)#', shell_exec('stty size'), $match) === 1) { 70 return $this->getNumberOfColumnsInteractive();
71 }
72
73 /**
74 * Returns if the file descriptor is an interactive terminal or not.
75 *
76 * @param int|resource $fileDescriptor
77 */
78 public function isInteractive($fileDescriptor = self::STDOUT): bool
79 {
80 return \function_exists('posix_isatty') && @\posix_isatty($fileDescriptor);
81 }
82
83 private function isWindows(): bool
84 {
85 return DIRECTORY_SEPARATOR === '\\';
86 }
87
88 /**
89 * @codeCoverageIgnore
90 */
91 private function getNumberOfColumnsInteractive(): int
92 {
93 if (\function_exists('shell_exec') && \preg_match('#\d+ (\d+)#', \shell_exec('stty size') ?? '', $match) === 1) {
88 if ((int) $match[1] > 0) { 94 if ((int) $match[1] > 0) {
89 return (int) $match[1]; 95 return (int) $match[1];
90 } 96 }
91 } 97 }
92 98
93 if (function_exists('shell_exec') && preg_match('#columns = (\d+);#', shell_exec('stty'), $match) === 1) { 99 if (\function_exists('shell_exec') && \preg_match('#columns = (\d+);#', \shell_exec('stty') ?? '', $match) === 1) {
94 if ((int) $match[1] > 0) { 100 if ((int) $match[1] > 0) {
95 return (int) $match[1]; 101 return (int) $match[1];
96 } 102 }
97 } 103 }
98 104
99 return 80; 105 return 80;
100 } 106 }
101 107
102 /** 108 /**
103 * Returns if the file descriptor is an interactive terminal or not. 109 * @codeCoverageIgnore
104 *
105 * @param int|resource $fileDescriptor
106 *
107 * @return bool
108 */ 110 */
109 public function isInteractive($fileDescriptor = self::STDOUT) 111 private function getNumberOfColumnsWindows(): int
110 { 112 {
111 return function_exists('posix_isatty') && @posix_isatty($fileDescriptor); 113 $ansicon = \getenv('ANSICON');
114 $columns = 80;
115
116 if (\is_string($ansicon) && \preg_match('/^(\d+)x\d+ \(\d+x(\d+)\)$/', \trim($ansicon), $matches)) {
117 $columns = $matches[1];
118 } elseif (\function_exists('proc_open')) {
119 $process = \proc_open(
120 'mode CON',
121 [
122 1 => ['pipe', 'w'],
123 2 => ['pipe', 'w']
124 ],
125 $pipes,
126 null,
127 null,
128 ['suppress_errors' => true]
129 );
130
131 if (\is_resource($process)) {
132 $info = \stream_get_contents($pipes[1]);
133
134 \fclose($pipes[1]);
135 \fclose($pipes[2]);
136 \proc_close($process);
137
138 if (\preg_match('/--------+\r?\n.+?(\d+)\r?\n.+?(\d+)\r?\n/', $info, $matches)) {
139 $columns = $matches[2];
140 }
141 }
142 }
143
144 return $columns - 1;
112 } 145 }
113 } 146 }