GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Push — master ( 6e41ae...f215c9 )
by t
06:13 queued 12s
created

Console   A

Complexity

Total Complexity 39

Size/Duplication

Total Lines 519
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 9
Bugs 2 Features 0
Metric Value
eloc 82
c 9
b 2
f 0
dl 0
loc 519
ccs 0
cts 149
cp 0
rs 9.28
wmc 39

30 Methods

Rating   Name   Duplication   Size   Complexity  
A ansiFormat() 0 5 2
A restoreCursorPosition() 0 3 1
A clearScreenBeforeCursor() 0 3 1
A scrollDown() 0 3 1
A moveCursorForward() 0 3 1
A clearScreen() 0 3 1
A clearScreenAfterCursor() 0 3 1
A moveCursorTo() 0 6 2
A stdin() 0 3 1
A moveCursorBackward() 0 3 1
A stderr() 0 3 1
A showCursor() 0 3 1
A stripAnsiFormat() 0 3 1
A stdout() 0 3 1
A clearLine() 0 3 1
A exec() 0 16 4
A clearLineBeforeCursor() 0 3 1
A scrollUp() 0 3 1
A output() 0 6 2
A clearLineAfterCursor() 0 3 1
A moveCursorUp() 0 3 1
A moveCursorDown() 0 3 1
A ansiStrlen() 0 3 1
A moveCursorNextLine() 0 3 1
A hideCursor() 0 3 1
A input() 0 11 3
A outputList() 0 4 2
A get() 0 4 1
A saveCursorPosition() 0 3 1
A moveCursorPrevLine() 0 3 1
1
<?php
2
/**
3
 * Class Console
4
 *
5
 * @link https://www.icy2003.com/
6
 * @author icy2003 <[email protected]>
7
 * @copyright Copyright (c) 2017, icy2003
8
 */
9
namespace icy2003\php\ihelpers;
10
11
use Exception;
12
13
/**
14
 * 控制台类
15
 *
16
 * 文字输出和命令执行
17
 */
18
class Console
19
{
20
21
    /**
22
     * 字体颜色控制码:黑色
23
     */
24
    const FG_BLACK = 30;
25
    /**
26
     * 字体颜色控制码:红色
27
     */
28
    const FG_RED = 31;
29
    /**
30
     * 字体颜色控制码:绿色
31
     */
32
    const FG_GREEN = 32;
33
    /**
34
     * 字体颜色控制码:黄色
35
     */
36
    const FG_YELLOW = 33;
37
    /**
38
     * 字体颜色控制码:蓝色
39
     */
40
    const FG_BLUE = 34;
41
    /**
42
     * 字体颜色控制码:紫色
43
     */
44
    const FG_PURPLE = 35;
45
    /**
46
     * 字体颜色控制码:青色
47
     */
48
    const FG_CYAN = 36;
49
    /**
50
     * 字体颜色控制码:灰色
51
     */
52
    const FG_GREY = 37;
53
    /**
54
     * 背景色控制码:黑色
55
     */
56
    const BG_BLACK = 40;
57
    /**
58
     * 背景色控制码:红色
59
     */
60
    const BG_RED = 41;
61
    /**
62
     * 背景色控制码:绿色
63
     */
64
    const BG_GREEN = 42;
65
    /**
66
     * 背景色控制码:黄色
67
     */
68
    const BG_YELLOW = 43;
69
    /**
70
     * 背景色控制码:蓝色
71
     */
72
    const BG_BLUE = 44;
73
    /**
74
     * 背景色控制码:紫色
75
     */
76
    const BG_PURPLE = 45;
77
    /**
78
     * 背景色控制码:青色
79
     */
80
    const BG_CYAN = 46;
81
    /**
82
     * 背景色控制码:灰色
83
     */
84
    const BG_GREY = 47;
85
    /**
86
     * 字体样式控制码:重置
87
     */
88
    const RESET = 0;
89
    /**
90
     * 字体样式控制码:普通
91
     */
92
    const NORMAL = 0;
93
    /**
94
     * 字体样式控制码:加粗
95
     */
96
    const BOLD = 1;
97
    /**
98
     * 字体样式控制码:斜体
99
     */
100
    const ITALIC = 3;
101
    /**
102
     * 字体样式控制码:下划线
103
     */
104
    const UNDERLINE = 4;
105
    /**
106
     * 字体样式控制码:闪烁
107
     */
108
    const BLINK = 5;
109
    /**
110
     * 字体样式控制码:
111
     */
112
    const NEGATIVE = 7;
113
    /**
114
     * 字体样式控制码:隐藏
115
     */
116
    const CONCEALED = 8;
117
    /**
118
     * 字体样式控制码:交叉输出
119
     */
120
    const CROSSED_OUT = 9;
121
    /**
122
     * 字体样式控制码:边框
123
     */
124
    const FRAMED = 51;
125
    /**
126
     * 字体样式控制码:环绕
127
     */
128
    const ENCIRCLED = 52;
129
    /**
130
     * 字体样式控制码:
131
     */
132
    const OVERLINED = 53;
133
134
    /**
135
     * 标准命令行输出
136
     *
137
     * @param string $string 输出文字
138
     *
139
     * @return integer|false
140
     */
141
    public static function stdout($string)
142
    {
143
        return fwrite(\STDOUT, $string);
144
    }
145
146
    /**
147
     * 标准命令行输入
148
     *
149
     * @return string
150
     */
151
    public static function stdin()
152
    {
153
        return rtrim((string) fgets(\STDIN), PHP_EOL);
154
    }
155
156
    /**
157
     * 标准命令行错误输出
158
     *
159
     * @param string $string 错误文字
160
     *
161
     * @return integer|false
162
     */
163
    public static function stderr($string)
164
    {
165
        return fwrite(\STDERR, $string);
166
    }
167
168
    /**
169
     * 输入提示
170
     *
171
     * @param string $prompt 输入提示
172
     * @param mixed $defaultValue 默认值
173
     *
174
     * @return string
175
     */
176
    public static function input($prompt = null, $defaultValue = '')
177
    {
178
        if (isset($prompt)) {
179
            self::output($prompt);
180
        }
181
182
        $input = self::stdin();
183
        if ('' === $input) {
184
            return (string) $defaultValue;
185
        }
186
        return $input;
187
    }
188
189
    /**
190
     * 输出提示
191
     *
192
     * @param string $string 提示文字
193
     * @param array $format 格式化文字数组
194
     *
195
     * @return integer|false
196
     */
197
    public static function output($string = null, $format = [])
198
    {
199
        if (!empty($format)) {
200
            $string = self::ansiFormat($string, $format);
201
        }
202
        return self::stdout($string . PHP_EOL);
203
    }
204
205
    /**
206
     * 输出列表
207
     *
208
     * @param array $array
209
     *
210
     * @return void
211
     */
212
    public static function outputList($array)
213
    {
214
        foreach ($array as $string) {
215
            self::output($string);
216
        }
217
    }
218
219
    /**
220
     * ANSI 格式化
221
     *
222
     * @param string $string ANSI 格式文本
223
     * @param array $format 格式代码数组
224
     *
225
     * @return string
226
     */
227
    public static function ansiFormat($string, $format = [])
228
    {
229
        $code = implode(';', $format);
230
231
        return "\033[0m" . ($code !== '' ? "\033[" . $code . 'm' : '') . $string . "\033[0m";
232
    }
233
234
    /**
235
     * 向终端发送 ANSI 控制代码 CUU,上移光标 n 行
236
     *
237
     * 如果光标已经在屏幕边缘,则此操作无效
238
     *
239
     * @param integer $rows 光标应向上移动的行数
240
     *
241
     * @return void
242
     */
243
    public static function moveCursorUp($rows = 1)
244
    {
245
        echo '\033[' . (int) $rows . 'A';
246
    }
247
248
    /**
249
     * 通过向终端发送 ANSI 控制代码 CUD,向下移动终端光标
250
     *
251
     * 如果光标已经在屏幕边缘,则此操作无效
252
     *
253
     * @param integer $rows 光标应向下移动的行数
254
     *
255
     * @return void
256
     */
257
    public static function moveCursorDown($rows = 1)
258
    {
259
        echo '\033[' . (int) $rows . 'B';
260
    }
261
262
    /**
263
     * 通过向终端发送 ANSI 控制代码 CUF,向前移动终端光标
264
     *
265
     * 如果光标已经在屏幕边缘,则此操作无效
266
     *
267
     * @param integer $steps 光标应向前移动的步数
268
     *
269
     * @return void
270
     */
271
    public static function moveCursorForward($steps = 1)
272
    {
273
        echo '\033[' . (int) $steps . 'C';
274
    }
275
276
    /**
277
     * 通过向终端发送 ANSI 控制代码 CUB,向后移动终端光标
278
     *
279
     * 如果光标已经在屏幕边缘,则此操作无效
280
     *
281
     * @param integer $steps 光标应向后移动的步数
282
     *
283
     * @return void
284
     */
285
    public static function moveCursorBackward($steps = 1)
286
    {
287
        echo '\033[' . (int) $steps . 'D';
288
    }
289
290
    /**
291
     * 向终端发送 ANSI 控制代码 CNL,让光标移到下 n 行的开头
292
     *
293
     * @param integer $lines 光标应向下移动的行数
294
     *
295
     * @return void
296
     */
297
    public static function moveCursorNextLine($lines = 1)
298
    {
299
        echo '\033[' . (int) $lines . 'E';
300
    }
301
302
    /**
303
     * 向终端发送 ANSI 控制代码 CPL,让光标移到上 n 行的开头
304
     *
305
     * @param integer $lines 光标应向上移动的行数
306
     *
307
     * @return void
308
     */
309
    public static function moveCursorPrevLine($lines = 1)
310
    {
311
        echo '\033[' . (int) $lines . 'F';
312
    }
313
314
    /**
315
     * 通过向终端发送 ANSI 控制代码 CUP 或 CPA,将光标移动到给定行和列的绝对位置上
316
     *
317
     * @param int $column 基于 1 的列号,1 是屏幕的左边缘
318
     * @param int $row 行 基于 1 的行数,1 是屏幕的上边缘。如果未设置,将只在当前行中移动光标
319
     *
320
     * @return void
321
     */
322
    public static function moveCursorTo($column, $row = null)
323
    {
324
        if ($row === null) {
325
            echo '\033[' . (int) $column . 'G';
326
        } else {
327
            echo '\033[' . (int) $row . ';' . (int) $column . 'H';
328
        }
329
    }
330
331
    /**
332
     * 通过向终端发送 ANSI 控制代码 SU 来向上滚动整页
333
     *
334
     * 在底部添加新行。在 Windows 中使用的 ANSI.SYS 不支持此操作
335
     *
336
     * @param integer $lines 要向上滚动的行数
337
     *
338
     * @return void
339
     */
340
    public static function scrollUp($lines = 1)
341
    {
342
        echo '\033[' . (int) $lines . 'S';
343
    }
344
345
    /**
346
     * 通过向终端发送 ANSI 控制代码 SD,向下滚动整页
347
     *
348
     * 在顶部添加新行。在 Windows 中使用的 ANSI.SYS 不支持此操作
349
     *
350
     * @param integer $lines 要向下滚动的行数
351
     *
352
     * @return void
353
     */
354
    public static function scrollDown($lines = 1)
355
    {
356
        echo '\033[' . (int) $lines . 'T';
357
    }
358
359
    /**
360
     * 通过向终端发送 ANSI 控制代码 SCP 来保存当前光标位置
361
     *
362
     * 然后可以使用 RestoreCursorPosition 恢复位置
363
     *
364
     * @return void
365
     */
366
    public static function saveCursorPosition()
367
    {
368
        echo '\033[s';
369
    }
370
371
    /**
372
     * 通过向终端发送 ANSI 控制代码 RCP,恢复用 SaveCursorPosition 保存的光标位置
373
     *
374
     * @return void
375
     */
376
    public static function restoreCursorPosition()
377
    {
378
        echo '\033[u';
379
    }
380
381
    /**
382
     * 通过发送 ANSI DECTCEM 代码隐藏光标到终端
383
     *
384
     * 使用 ShowCursor 将其带回
385
     *
386
     * 当应用程序退出时,不要忘记显示光标。退出后光标可能还隐藏在终端中
387
     *
388
     * @return void
389
     */
390
    public static function hideCursor()
391
    {
392
        echo '\033[?25l';
393
    }
394
395
    /**
396
     * 当被光标被 hideCursor 隐藏时,通过发送 ANSI DECTCEM 代码将光标显示到终端
397
     *
398
     * @return void
399
     */
400
    public static function showCursor()
401
    {
402
        echo '\033[?25h';
403
    }
404
405
    /**
406
     * 通过向终端发送带参数 2 的 ANSI 控制代码 ED,清除整个屏幕内容
407
     *
408
     * 不会更改光标位置
409
     *
410
     * 注意:在 Windows 中使用的 ANSI.SYS 实现将光标位置重置为屏幕的左上角
411
     *
412
     * @return void
413
     */
414
    public static function clearScreen()
415
    {
416
        echo '\033[2J';
417
    }
418
419
    /**
420
     * 通过将带参数 1 的ANSI控制代码 ED 发送到终端,清除从光标到屏幕开头的文本
421
     *
422
     * 不会更改光标位置
423
     *
424
     * @return void
425
     */
426
    public static function clearScreenBeforeCursor()
427
    {
428
        echo '\033[1J';
429
    }
430
431
    /**
432
     * 通过将带参数 0 的 ANSI 控制代码 ED 发送到终端,清除从光标到屏幕结尾的文本
433
     *
434
     * 不会更改光标位置
435
     *
436
     * @return void
437
     */
438
    public static function clearScreenAfterCursor()
439
    {
440
        echo '\033[0J';
441
    }
442
443
    /**
444
     * 清除行,通过向终端发送带参数 2 的 ANSI 控制代码 EL,光标当前处于打开状态
445
     *
446
     * 不会更改光标位置
447
     *
448
     * @return void
449
     */
450
    public static function clearLine()
451
    {
452
        echo '\033[2K';
453
    }
454
455
    /**
456
     * 通过将带参数 1 的 ANSI 控制代码 EL 发送到终端,清除从光标位置到行首的文本
457
     *
458
     * 不会更改光标位置
459
     *
460
     * @return void
461
     */
462
    public static function clearLineBeforeCursor()
463
    {
464
        echo '\033[1K';
465
    }
466
467
    /**
468
     * 通过将参数为 0 的 ANSI 控制代码 EL 发送到终端,清除从光标位置到行尾的文本
469
     *
470
     * 不会更改光标位置
471
     *
472
     * @return void
473
     */
474
    public static function clearLineAfterCursor()
475
    {
476
        echo '\033[0K';
477
    }
478
479
    /**
480
     * 从字符串中删除 ANSI 控制代码
481
     *
482
     * @param string $string 待处理的字符串
483
     *
484
     * @return string
485
     */
486
    public static function stripAnsiFormat($string)
487
    {
488
        return preg_replace('/\033\[[\d;?]*\w/', '', $string);
489
    }
490
491
    /**
492
     * 返回不带 ANSI 颜色代码的字符串的长度
493
     *
494
     * @param string $string 待测量的字符串
495
     *
496
     * @return int
497
     */
498
    public static function ansiStrlen($string)
499
    {
500
        return Strings::length(static::stripAnsiFormat($string));
501
    }
502
503
    /**
504
     * CLI 下获取命令参数
505
     *
506
     * @return array
507
     */
508
    public static function get()
509
    {
510
        global $argv;
511
        return array_slice($argv, 1);
512
    }
513
514
    /**
515
     * 执行一个命令并输出结果
516
     *
517
     * @param string|array $command
518
     *
519
     * @return string|false
520
     */
521
    public static function exec($command)
522
    {
523
        if (class_exists('\Symfony\Component\Process\Process')) {
524
            try{
525
                $process = new \Symfony\Component\Process\Process($command);
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Process\Process was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
526
            }catch(Exception $e){
527
                $process = new \Symfony\Component\Process\Process(Strings::toArray($command, ' '));
528
            }
529
530
            $process->run();
531
            if (false === $process->isSuccessful()) {
532
                return false;
533
            }
534
            return $process->getOutput();
535
        }else{
536
            return exec($command);
0 ignored issues
show
Bug introduced by
It seems like $command can also be of type array; however, parameter $command of exec() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

536
            return exec(/** @scrutinizer ignore-type */ $command);
Loading history...
537
        }
538
    }
539
}
540