Completed
Push — feature/0.7.0 ( 5bdbd1...cdc406 )
by Ryuichi
03:10
created

ConsoleOutputter::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
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
    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 View Code Duplication
    public function write($message)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 View Code Duplication
    private function flush()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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