|
104 | 104 | */ |
105 | 105 | function append($stream, $callback, $read_write = STREAM_FILTER_ALL) |
106 | 106 | { |
107 | | - $ret = @\stream_filter_append($stream, register(), $read_write, $callback); |
| 107 | + $errstr = ''; |
| 108 | + \set_error_handler(function ($_, $error) use (&$errstr) { |
| 109 | + // Match errstr from PHP's warning message. |
| 110 | + // stream_filter_append() expects parameter 1 to be resource,... |
| 111 | + $errstr = $error; |
| 112 | + }); |
| 113 | + |
| 114 | + $ret = \stream_filter_append($stream, register(), $read_write, $callback); |
| 115 | + |
| 116 | + \restore_error_handler(); |
108 | 117 |
|
109 | 118 | // PHP 8 throws above on type errors, older PHP and memory issues can throw here |
110 | 119 | // @codeCoverageIgnoreStart |
111 | 120 | if ($ret === false) { |
112 | | - $error = \error_get_last() + array('message' => ''); |
113 | | - throw new \RuntimeException('Unable to append filter: ' . $error['message']); |
| 121 | + throw new \RuntimeException('Unable to append filter: ' . $errstr); |
114 | 122 | } |
115 | 123 | // @codeCoverageIgnoreEnd |
116 | 124 |
|
@@ -147,13 +155,21 @@ function append($stream, $callback, $read_write = STREAM_FILTER_ALL) |
147 | 155 | */ |
148 | 156 | function prepend($stream, $callback, $read_write = STREAM_FILTER_ALL) |
149 | 157 | { |
150 | | - $ret = @\stream_filter_prepend($stream, register(), $read_write, $callback); |
| 158 | + $errstr = ''; |
| 159 | + \set_error_handler(function ($_, $error) use (&$errstr) { |
| 160 | + // Match errstr from PHP's warning message. |
| 161 | + // stream_filter_prepend() expects parameter 1 to be resource,... |
| 162 | + $errstr = $error; |
| 163 | + }); |
| 164 | + |
| 165 | + $ret = \stream_filter_prepend($stream, register(), $read_write, $callback); |
| 166 | + |
| 167 | + \restore_error_handler(); |
151 | 168 |
|
152 | 169 | // PHP 8 throws above on type errors, older PHP and memory issues can throw here |
153 | 170 | // @codeCoverageIgnoreStart |
154 | 171 | if ($ret === false) { |
155 | | - $error = \error_get_last() + array('message' => ''); |
156 | | - throw new \RuntimeException('Unable to prepend filter: ' . $error['message']); |
| 172 | + throw new \RuntimeException('Unable to prepend filter: ' . $errstr); |
157 | 173 | } |
158 | 174 | // @codeCoverageIgnoreEnd |
159 | 175 |
|
@@ -242,16 +258,25 @@ function prepend($stream, $callback, $read_write = STREAM_FILTER_ALL) |
242 | 258 | function fun($filter, $parameters = null) |
243 | 259 | { |
244 | 260 | $fp = \fopen('php://memory', 'w'); |
| 261 | + |
| 262 | + $errstr = ''; |
| 263 | + \set_error_handler(function ($_, $error) use (&$errstr) { |
| 264 | + // Match errstr from PHP's warning message. |
| 265 | + // stream_filter_append() expects parameter 1 to be resource,... |
| 266 | + $errstr = $error; |
| 267 | + }); |
| 268 | + |
245 | 269 | if (\func_num_args() === 1) { |
246 | | - $filter = @\stream_filter_append($fp, $filter, \STREAM_FILTER_WRITE); |
| 270 | + $filter = \stream_filter_append($fp, $filter, \STREAM_FILTER_WRITE); |
247 | 271 | } else { |
248 | | - $filter = @\stream_filter_append($fp, $filter, \STREAM_FILTER_WRITE, $parameters); |
| 272 | + $filter = \stream_filter_append($fp, $filter, \STREAM_FILTER_WRITE, $parameters); |
249 | 273 | } |
250 | 274 |
|
| 275 | + \restore_error_handler(); |
| 276 | + |
251 | 277 | if ($filter === false) { |
252 | 278 | \fclose($fp); |
253 | | - $error = \error_get_last() + array('message' => ''); |
254 | | - throw new \RuntimeException('Unable to access built-in filter: ' . $error['message']); |
| 279 | + throw new \RuntimeException('Unable to access built-in filter: ' . $errstr); |
255 | 280 | } |
256 | 281 |
|
257 | 282 | // append filter function which buffers internally |
@@ -301,10 +326,20 @@ function fun($filter, $parameters = null) |
301 | 326 | */ |
302 | 327 | function remove($filter) |
303 | 328 | { |
304 | | - if (@\stream_filter_remove($filter) === false) { |
| 329 | + $errstr = ''; |
| 330 | + \set_error_handler(function ($_, $error) use (&$errstr) { |
| 331 | + // Match errstr from PHP's warning message. |
| 332 | + // stream_filter_remove() expects parameter 1 to be resource,... |
| 333 | + $errstr = $error; |
| 334 | + }); |
| 335 | + |
| 336 | + $ret = \stream_filter_remove($filter); |
| 337 | + |
| 338 | + \restore_error_handler(); |
| 339 | + |
| 340 | + if ($ret === false) { |
305 | 341 | // PHP 8 throws above on type errors, older PHP and memory issues can throw here |
306 | | - $error = \error_get_last(); |
307 | | - throw new \RuntimeException('Unable to remove filter: ' . $error['message']); |
| 342 | + throw new \RuntimeException('Unable to remove filter: ' . $errstr); |
308 | 343 | } |
309 | 344 | } |
310 | 345 |
|
|
0 commit comments