forked from phalcon/incubator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Environment.php
248 lines (212 loc) · 6.29 KB
/
Environment.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
<?php
/*
+------------------------------------------------------------------------+
| Phalcon Framework |
+------------------------------------------------------------------------+
| Copyright (c) 2011-2016 Phalcon Team (https://www.phalconphp.com) |
+------------------------------------------------------------------------+
| This source file is subject to the New BSD License that is bundled |
| with this package in the file LICENSE.txt. |
| |
| If you did not receive a copy of the license and are unable to |
| obtain it through the world-wide-web, please send an email |
| to license@phalconphp.com so we can send you a copy immediately. |
+------------------------------------------------------------------------+
| Authors: Serghei Iakovlev <serghei@phalconphp.com> |
+------------------------------------------------------------------------+
*/
namespace Phalcon\Cli\Environment;
/**
* Console Environment
*
* @package Phalcon\Cli\Environment
*/
class Environment implements EnvironmentInterface
{
/**
* Terminal dimensions
* @var array
*/
protected $dimensions = null;
/**
* {@inheritdoc}
*
* @return bool
*/
public function isWindows()
{
return 'WIN' === strtoupper(substr(PHP_OS, 0, 3));
}
/**
* When currently running under MS Windows checks if ANSI x3.64 is supported and enabled.
*
* @link http://vt100.net/annarbor/aaa-ug/section13.html
*
* @return bool
*/
public function isAnsicon()
{
return false !== getenv('ANSICON') || 'ON' === getenv('ConEmuANSI');
}
/**
* {@inheritdoc}
*
* @return bool
*/
public function isConsole()
{
return 'cli' === PHP_SAPI;
}
/**
* {@inheritdoc}
*
* @param int|resource $fd File descriptor, must be either a file resource or an integer [Optional]
* @return bool
*/
public function isInteractive($fd = EnvironmentInterface::STDOUT)
{
return function_exists('posix_isatty') && @posix_isatty($fd);
}
/**
* {@inheritdoc}
*
* @return bool
*/
public function hasColorSupport()
{
if ($this->isWindows()) {
return $this->isAnsicon();
}
if (!defined('STDOUT')) {
return false;
}
return $this->isInteractive(STDOUT);
}
/**
* Gets the terminal dimensions based on the current environment.
*
* @return array
*/
public function getDimensions()
{
if (!empty($this->dimensions)) {
return $this->dimensions;
}
if ($this->isWindows()) {
if ($this->isAnsicon() && preg_match('#(?:\d+x\d+)\s+\((\d+)x(\d+)\)#', trim(getenv('ANSICON')), $match)) {
// ANSICON maintains an environment variable which holds the current screen size
// e.g. ANSICON=200x9999 (200x100)
return [(int) $match[1], (int) $match[2]];
}
if (1 === preg_match('/^(\d+)x(\d+)$/', $this->getModeCon(), $match)) {
return [(int) $match[1], (int) $match[2]];
}
} elseif (1 === preg_match('/^(\d+)x(\d+)$/', $this->getSttySize(), $match)) {
return [(int) $match[1], (int) $match[2]];
}
// fallback mode
return [EnvironmentInterface::WIDTH, EnvironmentInterface::HEIGHT];
}
/**
* Sets terminal dimensions.
*
* @param int $width The width
* @param int $height The height
* @return $this
*/
public function setDimensions($width, $height)
{
if ((is_int($width) || ctype_digit($width)) && (is_int($height) || ctype_digit($height))) {
$this->dimensions = [$width, $height];
}
return $this;
}
/**
* Runs and parses Microsoft DOS `MODE CON` command if it's available.
*
* @link https://technet.microsoft.com/en-us/library/bb490932.aspx
*
* @return null|string
*/
public function getModeCon()
{
if (!function_exists('proc_open')) {
return null;
}
$descriptorspec = [
1 => ['pipe', 'w'], // stdout
2 => ['pipe', 'w'] // stderr
];
$process = proc_open(
'MODE CON',
$descriptorspec,
$pipes,
null,
null,
['suppress_errors' => true] // suppressing any error output
);
if (is_resource($process)) {
$info = stream_get_contents($pipes[1]);
fclose($pipes[1]);
fclose($pipes[2]);
proc_close($process);
if (1 === preg_match('/--------+\r?\n.+?(\d+)\r?\n.+?(\d+)\r?\n/', $info, $match)) {
return $match[2].'x'.$match[1];
}
}
return null;
}
/**
* Runs and parses `stty size` command if it's available.
*
* @return null|string
*/
public function getSttySize()
{
if (!function_exists('proc_open')) {
return null;
}
$descriptorspec = [
1 => ['pipe', 'w'], // stdout
2 => ['pipe', 'w'] // stderr
];
$process = proc_open(
'stty size',
$descriptorspec,
$pipes,
null,
null,
['suppress_errors' => true] // suppressing any error output
);
if (is_resource($process)) {
$info = stream_get_contents($pipes[1]);
fclose($pipes[1]);
fclose($pipes[2]);
proc_close($process);
if (1 === preg_match('#(\d+) (\d+)#', $info, $match)) {
return $match[2].'x'.$match[1];
}
}
return null;
}
/**
* {@inheritdoc}
*
* @return int
*/
public function getNumberOfColumns()
{
$dimensions = $this->getdimensions();
return $dimensions[0];
}
/**
* {@inheritdoc}
*
* @return int
*/
public function getNumberOfRows()
{
$dimensions = $this->getdimensions();
return $dimensions[1];
}
}