Console.php
2.12 KB
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
<?php
/**
 * Created by PhpStorm.
 * User: Hanson
 * Date: 2016/12/9
 * Time: 22:51
 */
namespace Hanson\Vbot\Support;
use Carbon\Carbon;
use PHPQRCode\QRcode;
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
use Symfony\Component\Console\Output\ConsoleOutput;
use Symfony\Component\Console\Output\OutputInterface;
class Console
{
    const INFO = 'INFO';
    const WARNING = 'WARNING';
    const ERROR = 'ERROR';
    /**
     * 输出字符串
     *
     * @param $str
     * @param string $level
     */
    public static function log($str, $level = 'INFO')
    {
        echo '[' . Carbon::now()->toDateTimeString() . ']' . "[{$level}] " . $str . PHP_EOL;
    }
    /**
     * debug 模式下调试输出
     *
     * @param $str
     */
    public static function debug($str)
    {
        if (server()->config['debug']) {
            static::log($str, 'DEBUG');
        }
    }
    /**
     * 初始化二维码style
     *
     * @param OutputInterface $output
     */
    private static function initQrcodeStyle(OutputInterface $output) {
        $style = new OutputFormatterStyle('black', 'black', array('bold'));
        $output->getFormatter()->setStyle('blackc', $style);
        $style = new OutputFormatterStyle('white', 'white', array('bold'));
        $output->getFormatter()->setStyle('whitec', $style);
    }
    /**
     * 控制台显示二维码
     *
     * @param $text
     */
    public static function showQrCode($text)
    {
        $output = new ConsoleOutput();
        static::initQrcodeStyle($output);
        if(System::isWin()){
            $pxMap = ['<whitec>mm</whitec>', '<blackc>  </blackc>'];
            system('cls');
        }else{
            $pxMap = ['<whitec>  </whitec>', '<blackc>  </blackc>'];
            system('clear');
        }
        $text   = QRcode::text($text);
        $length = strlen($text[0]);
        foreach ($text as $line) {
            $output->write($pxMap[0]);
            for ($i = 0; $i < $length; $i++) {
                $type = substr($line, $i, 1);
                $output->write($pxMap[$type]);
            }
            $output->writeln($pxMap[0]);
        }
    }
}