1
|
|
|
<?php |
2
|
|
|
namespace WebStream\IO; |
3
|
|
|
|
4
|
|
|
use WebStream\Exception\Extend\InvalidArgumentException; |
5
|
|
|
use WebStream\Exception\Extend\IOException; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* FileOutputStream |
9
|
|
|
* @author Ryuichi TANAKA. |
10
|
|
|
* @since 2016/02/24 |
11
|
|
|
* @version 0.7 |
12
|
|
|
*/ |
13
|
|
|
class FileOutputStream extends OutputStream |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var File ファイルオブジェクト |
17
|
|
|
*/ |
18
|
|
|
protected $file; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* constructor |
22
|
|
|
* @param mixed $file ファイルオブジェクトまたはファイルパス |
23
|
|
|
* @throws InvalidArgumentException |
24
|
|
|
* @throws IOException |
25
|
|
|
*/ |
26
|
|
|
public function __construct($file, bool $isAppend = false) |
27
|
|
|
{ |
28
|
|
View Code Duplication |
if ($file instanceof File) { |
|
|
|
|
29
|
|
|
$this->file = $file; |
30
|
|
|
} elseif (is_string($file)) { |
31
|
|
|
$this->file = new File($file); |
32
|
|
|
} else { |
33
|
|
|
throw new InvalidArgumentException("Invalid argument type: " . $file); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
$mode = $isAppend ? 'ab' : 'wb'; |
37
|
|
|
$stream = @fopen($this->file->getAbsoluteFilePath(), $mode); |
38
|
|
|
|
39
|
|
View Code Duplication |
if (!is_resource($stream) || $stream === false) { |
|
|
|
|
40
|
|
|
throw new IOException("Unable open " . $this->file->getAbsoluteFilePath()); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
parent::__construct($stream); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* {@inheritdoc} |
48
|
|
|
*/ |
49
|
|
|
public function write($buf, int $off = null, int $len = null) |
50
|
|
|
{ |
51
|
|
|
$data = null; |
|
|
|
|
52
|
|
|
if ($off === null && $len === null) { |
53
|
|
|
$data = $buf; |
54
|
|
|
} elseif ($off !== null && $len === null) { |
55
|
|
|
$data = substr($buf, $off); |
56
|
|
|
} elseif ($off === null && $len !== null) { |
57
|
|
|
$data = substr($buf, 0, $len); |
58
|
|
|
} else { |
59
|
|
|
$data = substr($buf, $off, $len); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
if (@fwrite($this->stream, $data) === false) { |
63
|
|
|
throw new IOException("Failed to write stream."); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* {@inheritdoc} |
69
|
|
|
*/ |
70
|
|
View Code Duplication |
public function close() |
|
|
|
|
71
|
|
|
{ |
72
|
|
|
if ($this->stream === null) { |
73
|
|
|
return; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$this->flush(); |
77
|
|
|
|
78
|
|
|
if (get_resource_type($this->stream) !== 'Unknown' && fclose($this->stream) === false) { |
79
|
|
|
throw new IOException("Cannot close output stream."); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$this->stream = null; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* {@inheritdoc} |
87
|
|
|
*/ |
88
|
|
|
public function flush() |
89
|
|
|
{ |
90
|
|
|
if (@fflush($this->stream) === false) { |
91
|
|
|
throw new IOException("Failed to flush."); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
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.