|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace RouterOS\Streams; |
|
4
|
|
|
|
|
5
|
|
|
use RouterOS\Interfaces\StreamInterface; |
|
6
|
|
|
use RouterOS\Exceptions\StreamException; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* class ResourceStream |
|
10
|
|
|
* |
|
11
|
|
|
* Stream using a resource (socket, file, pipe etc.) |
|
12
|
|
|
* |
|
13
|
|
|
* @package RouterOS |
|
14
|
|
|
* @since 0.9 |
|
15
|
|
|
*/ |
|
16
|
|
|
class ResourceStream implements StreamInterface |
|
17
|
|
|
{ |
|
18
|
|
|
protected $stream; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* ResourceStream constructor. |
|
22
|
|
|
* |
|
23
|
|
|
* @param $stream |
|
24
|
|
|
*/ |
|
25
|
23 |
|
public function __construct($stream) |
|
26
|
|
|
{ |
|
27
|
23 |
|
if (!is_resource($stream)) { |
|
28
|
5 |
|
throw new \InvalidArgumentException( |
|
29
|
5 |
|
sprintf( |
|
30
|
5 |
|
'Argument must be a valid resource type. %s given.', |
|
31
|
5 |
|
gettype($stream) |
|
32
|
|
|
) |
|
33
|
|
|
); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
// TODO: Should we verify the resource type? |
|
37
|
18 |
|
$this->stream = $stream; |
|
38
|
18 |
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param int $length |
|
42
|
|
|
* @return string |
|
43
|
|
|
* @throws \RouterOS\Exceptions\StreamException |
|
44
|
|
|
* @throws \InvalidArgumentException |
|
45
|
|
|
*/ |
|
46
|
20 |
|
public function read(int $length): string |
|
47
|
|
|
{ |
|
48
|
20 |
|
if ($length <= 0) { |
|
49
|
2 |
|
throw new \InvalidArgumentException('Cannot read zero ot negative count of bytes from a stream'); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
// TODO: Ignore errors here, but why? |
|
53
|
18 |
|
$result = @fread($this->stream, $length); |
|
54
|
|
|
|
|
55
|
18 |
|
if (false === $result) { |
|
56
|
1 |
|
throw new StreamException("Error reading $length bytes"); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
17 |
|
return $result; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Writes a string to a stream |
|
64
|
|
|
* |
|
65
|
|
|
* Write $length bytes of string, if not mentioned, write all the string |
|
66
|
|
|
* Must be binary safe (as fread). |
|
67
|
|
|
* if $length is greater than string length, write all string and return number of writen bytes |
|
68
|
|
|
* if $length os smaller than string length, remaining bytes are losts. |
|
69
|
|
|
* |
|
70
|
|
|
* @param string $string |
|
71
|
|
|
* @param int|null $length the numer of bytes to read |
|
72
|
|
|
* @return int the number of written bytes |
|
73
|
|
|
* @throws \RouterOS\Exceptions\StreamException |
|
74
|
|
|
*/ |
|
75
|
18 |
|
public function write(string $string, int $length = null): int |
|
76
|
|
|
{ |
|
77
|
18 |
|
if (null === $length) { |
|
78
|
18 |
|
$length = strlen($string); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
// TODO: Ignore errors here, but why? |
|
82
|
18 |
|
$result = @fwrite($this->stream, $string, $length); |
|
83
|
|
|
|
|
84
|
18 |
|
if (false === $result) { |
|
85
|
2 |
|
throw new StreamException("Error writing $length bytes"); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
16 |
|
return $result; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Close stream connection |
|
93
|
|
|
* |
|
94
|
|
|
* @return void |
|
95
|
|
|
* @throws \RouterOS\Exceptions\StreamException |
|
96
|
|
|
*/ |
|
97
|
2 |
|
public function close() |
|
98
|
|
|
{ |
|
99
|
2 |
|
$hasBeenClosed = false; |
|
100
|
|
|
|
|
101
|
2 |
|
if (null !== $this->stream) { |
|
102
|
2 |
|
$hasBeenClosed = @fclose($this->stream); |
|
103
|
2 |
|
$this->stream = null; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
2 |
|
if (false === $hasBeenClosed) { |
|
107
|
1 |
|
throw new StreamException('Error closing stream'); |
|
108
|
|
|
} |
|
109
|
2 |
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|