ConsoleOutputter   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 34
dl 0
loc 112
rs 10
c 0
b 0
f 0
wmc 14

8 Methods

Rating   Name   Duplication   Size   Complexity  
A write() 0 10 3
A __destruct() 0 3 1
A __construct() 0 5 1
A flush() 0 5 3
A enableDirectWrite() 0 4 1
A enableLazyWrite() 0 3 1
A clear() 0 3 1
A writeLog() 0 5 3
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リスト
0 ignored issues
show
Documentation Bug introduced by
The doc comment SAPIリスト at position 0 could not be parsed: Unknown type name 'SAPIリスト' at position 0 in SAPIリスト.
Loading history...
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
    public function __construct($bufferSize = 1000)
47
    {
48
        $this->logMessages = [];
49
        $this->bufferSize = $bufferSize;
50
        $this->isLazyWrite = false;
51
    }
52
53
    /**
54
     * destructor
55
     */
56
    public function __destruct()
57
    {
58
        $this->writeLog(implode("", $this->logMessages));
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64
    public function enableLazyWrite()
65
    {
66
        $this->isLazyWrite = true;
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72
    public function enableDirectWrite()
73
    {
74
        $this->flush();
75
        $this->isLazyWrite = false;
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81
    public function write($message)
82
    {
83
        if ($this->isLazyWrite) {
84
            if (count($this->logMessages) >= $this->bufferSize) {
85
                $this->flush();
86
                $this->clear();
87
            }
88
            $this->logMessages[] = $message;
89
        } else {
90
            $this->writeLog($message);
91
        }
92
    }
93
94
    /**
95
     * バッファをクリアする
96
     */
97
    private function clear()
98
    {
99
        $this->logMessages = [];
100
    }
101
102
    /**
103
     * バッファをログ出力する
104
     */
105
    private function flush()
106
    {
107
        if ($this->isLazyWrite && count($this->logMessages) > 0) {
108
            $this->writeLog(implode("", $this->logMessages));
109
            $this->clear();
110
        }
111
    }
112
113
    /**
114
     * ログファイルに書き出す
115
     * @param string $message ログメッセージ
116
     */
117
    private function writeLog($message)
118
    {
119
        $sapi = php_sapi_name();
120
        if (array_key_exists($sapi, $this->sapis) && $this->sapis[$sapi] === 'console') {
121
            echo $message;
122
        }
123
    }
124
}
125