Completed
Pull Request — master (#1)
by Ryuichi
02:26
created

ConsoleOutputter::write()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.1406

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 6
cts 8
cp 0.75
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 3
nop 1
crap 3.1406
1
<?php
2
namespace WebStream\Log\Outputter;
3
4
/**
5
 * ConsoleOutputter
6
 * @author Ryuichi Tanaka
7
 * @since 2016/01/26
8
 * @version 0.7
9
 */
10
class ConsoleOutputter implements IOutputter, ILazyWriter
11
{
12
    /**
13
     * https://github.com/php/php-src/tree/master/sapi
14
     * PHP7以前のものは対応しない
15
     * @var SAPIリスト
16
     */
17
    private $sapis = [
18
        'apache2handler' => 'http',
19
        'cgi'            => 'http',
20
        'cli'            => 'console',
21
        'fpm'            => 'http',
22
        'embed'          => 'unsupported',
23
        'litespeed'      => 'unsupported',
24
        'phpdbg'         => 'unsupported',
25
        'tests'          => 'unsupported'
26
    ];
27
28
    /**
29
     * @var array<string> ログメッセージリスト
30
     */
31
    private $logMessages;
32
33
    /**
34
     * @var int バッファリングサイズ
35
     */
36
    private $bufferSize;
37
38
    /**
39
     * @var bool 遅延書き出しフラグ
40
     */
41
    private $isLazyWrite;
42
43
    /**
44
     * constructor
45
     */
46 126
    public function __construct($bufferSize = 1000)
47
    {
48 126
        $this->logMessages = [];
49 126
        $this->bufferSize = $bufferSize;
50 126
        $this->isLazyWrite = false;
51 126
    }
52
53
    /**
54
     * destructor
55
     */
56 1
    public function __destruct()
57
    {
58 1
        $this->writeLog(implode("", $this->logMessages));
59 1
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64 1
    public function enableLazyWrite()
65
    {
66 1
        $this->isLazyWrite = true;
67 1
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72 3
    public function enableDirectWrite()
73
    {
74 3
        $this->flush();
75 3
        $this->isLazyWrite = false;
76 3
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81 82
    public function write($message)
82
    {
83 82
        if ($this->isLazyWrite) {
84 1
            if (count($this->logMessages) >= $this->bufferSize) {
85
                $this->flush();
86
                $this->clear();
87
            }
88 1
            $this->logMessages[] = $message;
89
        } else {
90 81
            $this->writeLog($message);
91
        }
92 82
    }
93
94
    /**
95
     * バッファをクリアする
96
     */
97 1
    private function clear()
98
    {
99 1
        $this->logMessages = [];
100 1
    }
101
102
    /**
103
     * バッファをログ出力する
104
     */
105 3
    private function flush()
106
    {
107 3
        if ($this->isLazyWrite && count($this->logMessages) > 0) {
108 1
            $this->writeLog(implode("", $this->logMessages));
109 1
            $this->clear();
110
        }
111 3
    }
112
113
    /**
114
     * ログファイルに書き出す
115
     * @param string $message ログメッセージ
116
     */
117 83
    private function writeLog($message)
118
    {
119 83
        $sapi = php_sapi_name();
120 83
        if (array_key_exists($sapi, $this->sapis) && $this->sapis[$sapi] === 'console') {
121 83
            echo $message;
122
        }
123 83
    }
124
}
125