-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbstractStream.php
More file actions
142 lines (132 loc) · 3.76 KB
/
Copy pathAbstractStream.php
File metadata and controls
142 lines (132 loc) · 3.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<?php
/**
* This file is part of HttpMessage
*
* @package bdk/http-message
* @author Brad Kent <bkfake-github@yahoo.com>
* @license http://opensource.org/licenses/MIT MIT
* @copyright 2014-2024 Brad Kent
* @version v1.0
*/
namespace bdk\HttpMessage;
use InvalidArgumentException;
use RuntimeException;
/**
* Extended by Stream
*/
abstract class AbstractStream
{
/** @var array<string,string> */
protected $strings = array(
'detached' => 'Stream is detached',
'fopenFail' => 'The file %s cannot be opened.',
'posUnknown' => 'Unable to determine stream position',
'readFail' => 'Unable to read from stream',
'readFailNonReadable' => 'Unable to read from non-readable stream',
'readLengthNegative' => 'Length parameter cannot be negative',
'resourceInvalidType' => 'Expected resource, filename, or string. %s provided.',
'seekFail' => 'Unable to seek to stream position %s with whence %s',
'seekNonSeekable' => 'Stream is not seekable',
'writeFail' => 'Unable to write to stream',
'writeFailNonWritable' => 'Unable to write to a non-writable stream',
);
/** @var resource|closed-resource|null A resource reference */
protected $resource;
/**
* Gets the type name of a variable in a way that is suitable for debugging
*
* @param mixed $value The value being type checked
*
* @return string
*/
protected static function getDebugType($value)
{
return \is_object($value)
? \get_class($value)
: \gettype($value);
}
/**
* Safely test if value is a file
*
* @param mixed $value The value to check
*
* @return bool
*
* @psalm-assert-if-true non-empty-string $value
*/
protected static function isFile($value)
{
return \is_string($value)
&& \preg_match('#(://|[\r\n\x00])#', $value) !== 1
&& \is_file($value);
}
/**
* Is resource open?
*
* @return bool
*
* @psalm-assert-if-true resource $this->resource
*/
protected function isResourceOpen()
{
return isset($this->resource) && \is_resource($this->resource);
}
/**
* Set resource
*
* @param mixed $value Resource, filepath, or string content to wrap.
*
* @return void
*
* @throws InvalidArgumentException
* @throws RuntimeException
*/
protected function setResource($value)
{
if ($value === null) {
$this->resource = \fopen('php://temp', 'wb+');
return;
}
if (\is_resource($value)) {
$this->resource = $value;
return;
}
if ($this->isFile($value)) {
$this->setResourceFile($value);
return;
}
if (\is_string($value)) {
$this->resource = \fopen('php://temp', 'wb+');
\fwrite($this->resource, $value);
\rewind($this->resource);
return;
}
throw new InvalidArgumentException(\sprintf(
$this->strings['resourceInvalidType'],
$this->getDebugType($value)
));
}
/**
* Set resource to the specified file
*
* @param string $file filepath
*
* @return void
*
* @throws RuntimeException
*/
protected function setResourceFile($file)
{
\set_error_handler(static function () {
return true; // Don't execute PHP internal error handler
});
$this->resource = \fopen($file, 'r');
\restore_error_handler();
if ($this->resource === false) {
throw new RuntimeException(\sprintf(
$this->strings['fopenFail'],
$file
));
}
}
}