|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* Copyright (c) 2011-2015, Celestino Diaz <[email protected]> |
|
5
|
|
|
* |
|
6
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|
7
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
|
8
|
|
|
* in the Software without restriction, including without limitation the rights |
|
9
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
10
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
|
11
|
|
|
* furnished to do so, subject to the following conditions: |
|
12
|
|
|
* |
|
13
|
|
|
* The above copyright notice and this permission notice shall be included in |
|
14
|
|
|
* all copies or substantial portions of the Software. |
|
15
|
|
|
* |
|
16
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
17
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
18
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
19
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
20
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
21
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
22
|
|
|
* THE SOFTWARE. |
|
23
|
|
|
*/ |
|
24
|
|
|
|
|
25
|
|
|
namespace Brickoo\Component\IO\Stream; |
|
26
|
|
|
|
|
27
|
|
|
use Brickoo\Component\IO\Stream\Exception\AccessModeUnknownException; |
|
28
|
|
|
use Brickoo\Component\IO\Stream\Exception\UnableToCreateResourceHandleException; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* FileStream |
|
32
|
|
|
* |
|
33
|
|
|
* Implementation of a stream based on a file resource. |
|
34
|
|
|
* @author Celestino Diaz <[email protected]> |
|
35
|
|
|
*/ |
|
36
|
|
|
class FileStream implements Stream { |
|
37
|
|
|
|
|
38
|
|
|
/** @cons file flags */ |
|
39
|
|
|
const MODE_READ = 1; |
|
40
|
|
|
const MODE_WRITE = 2; |
|
41
|
|
|
const MODE_APPEND = 4; |
|
42
|
|
|
const MODE_RESET = 8; |
|
43
|
|
|
const MODE_COOP = 16; |
|
44
|
|
|
|
|
45
|
|
|
/** @var resource */ |
|
46
|
|
|
private $resource; |
|
47
|
|
|
|
|
48
|
|
|
/** @var \Brickoo\Component\IO\Stream\FileStreamConfig */ |
|
49
|
|
|
private $streamConfig; |
|
50
|
|
|
|
|
51
|
|
|
/** @var array */ |
|
52
|
|
|
private $availableModes; |
|
53
|
|
|
|
|
54
|
|
|
/** @param \Brickoo\Component\IO\Stream\FileStreamConfig $streamConfig */ |
|
55
|
1 |
|
public function __construct(FileStreamConfig $streamConfig) { |
|
56
|
1 |
|
$this->streamConfig = $streamConfig; |
|
57
|
|
|
|
|
58
|
1 |
|
$this->availableModes = [ |
|
59
|
1 |
|
self::MODE_READ => "rb", |
|
60
|
1 |
|
self::MODE_WRITE => "wb", |
|
61
|
1 |
|
self::MODE_READ + self::MODE_WRITE => "rb+", |
|
62
|
1 |
|
self::MODE_WRITE + self::MODE_RESET => "wb+", |
|
63
|
1 |
|
self::MODE_WRITE + self::MODE_APPEND => "ab", |
|
64
|
1 |
|
self::MODE_READ + self::MODE_WRITE + self::MODE_APPEND => "ab+", |
|
65
|
1 |
|
self::MODE_WRITE + self::MODE_COOP => "cb", |
|
66
|
1 |
|
self::MODE_READ + self::MODE_WRITE + self::MODE_COOP => "cb+" |
|
67
|
1 |
|
]; |
|
68
|
1 |
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Returns the file stream configuration. |
|
72
|
|
|
* @return FileStreamConfig |
|
73
|
|
|
*/ |
|
74
|
1 |
|
public function getConfiguration() { |
|
75
|
1 |
|
return $this->streamConfig; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Reconfigure the file stream context. |
|
80
|
|
|
* @param FileStreamConfig $streamConfig |
|
81
|
|
|
* @return \Brickoo\Component\IO\Stream\FileStream |
|
82
|
|
|
*/ |
|
83
|
1 |
|
public function reconfigure(FileStreamConfig $streamConfig) { |
|
84
|
1 |
|
$this->streamConfig = $streamConfig; |
|
85
|
1 |
|
return $this; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** {@inheritdoc} */ |
|
89
|
6 |
|
public function open() { |
|
90
|
6 |
|
if ($this->hasResource()) { |
|
91
|
1 |
|
return $this->resource; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
6 |
|
$configuration = $this->getConfiguration(); |
|
95
|
|
|
|
|
96
|
6 |
|
if (!($resource = @fopen( |
|
97
|
6 |
|
$configuration->getFilename(), |
|
98
|
6 |
|
$this->resolveMode($configuration->getMode()), |
|
99
|
5 |
|
$configuration->shouldUseIncludePath(), |
|
100
|
5 |
|
stream_context_create($configuration->getContext()) |
|
101
|
5 |
|
))) { |
|
102
|
1 |
|
throw new UnableToCreateResourceHandleException($this->getConfiguration()->getFilename()); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
4 |
|
$this->resource = $resource; |
|
106
|
4 |
|
return $this->resource; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** {@inheritdoc} */ |
|
110
|
2 |
|
public function close() { |
|
111
|
2 |
|
if ($this->hasResource()) { |
|
112
|
2 |
|
fclose($this->resource); |
|
113
|
2 |
|
$this->resource = null; |
|
114
|
2 |
|
} |
|
115
|
2 |
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** Release stream resource on destruction. */ |
|
118
|
1 |
|
public function __destruct() { |
|
119
|
1 |
|
$this->close(); |
|
120
|
1 |
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Returns the corresponding file access mode,,. |
|
124
|
|
|
* @param integer $mode |
|
125
|
|
|
* @throws \Brickoo\Component\IO\Stream\Exception\AccessModeUnknownException |
|
126
|
|
|
* @return string the resolved mode |
|
127
|
|
|
*/ |
|
128
|
2 |
|
private function resolveMode($mode) { |
|
129
|
2 |
|
if (!isset($this->availableModes[$mode])) { |
|
130
|
1 |
|
throw new AccessModeUnknownException($mode); |
|
131
|
|
|
} |
|
132
|
1 |
|
return $this->availableModes[$mode]; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Checks if the resource has been already created. |
|
137
|
|
|
* @return boolean check result |
|
138
|
|
|
*/ |
|
139
|
2 |
|
private function hasResource() { |
|
140
|
2 |
|
return is_resource($this->resource); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
} |
|
144
|
|
|
|