Completed
Push — feature/0.7.0 ( d06a69...2e2153 )
by Ryuichi
04:48
created

FileWriter::__construct()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 11
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 11
rs 9.4285
cc 3
eloc 4
nc 2
nop 2
1
<?php
2
namespace WebStream\IO\Writer;
3
4
use WebStream\IO\FileOutputStream;
5
use WebStream\Exception\Extend\IOException;
6
7
/**
8
 * FileWriter
9
 * @author Ryuichi TANAKA.
10
 * @since 2016/02/24
11
 * @version 0.7
12
 */
13
class FileWriter extends OutputStreamWriter
14
{
15
    /**
16
     * constructor
17
     * @param mixed $file ファイルオブジェクトまたはファイルパス
18
     */
19
    public function __construct($file, $bufferSize = null)
20
    {
21
        parent::__construct(new FileOutputStream($file));
22
23
        // fwriteのデフォルトバッファリングサイズは8KBなので、指定無しの場合は8KBになる
24
        // また、同じストリームに対して出力を行うプロセスが複数ある場合、8KBごとに停止する
25
        // see: http://php.net/manual/ja/function.stream-set-write-buffer.php
26
        if ($bufferSize !== null && stream_set_write_buffer($this->stream, $bufferSize) !== 0) {
27
            throw new IOException("Failed to change the buffer size.");
28
        }
29
    }
30
31
    /**
32
     * ファイルに書き込む
33
     * @param mixed $data 書き込みデータ
34
     */
35
    public function write($data)
36
    {
37
        $this->stream->write($data);
38
    }
39
}
40