|
4 | 4 |
|
5 | 5 | use DateTime;
|
6 | 6 | use Exception;
|
| 7 | +use Throwable; |
7 | 8 | use React\EventLoop\LoopInterface;
|
8 | 9 | use React\Filesystem\ObjectStream;
|
9 | 10 | use React\Filesystem\ObjectStreamSink;
|
@@ -181,6 +182,10 @@ public function callFilesystem($function, $args, $errorResultCode = -1)
|
181 | 182 | return $this->pool->rpc(Factory::rpc($function, $args))->then(function (Payload $payload) {
|
182 | 183 | return \React\Promise\resolve($payload->getPayload());
|
183 | 184 | }, function ($payload) {
|
| 185 | + if ($payload instanceof Throwable) { |
| 186 | + return \React\Promise\reject($payload); |
| 187 | + } |
| 188 | + |
184 | 189 | return \React\Promise\reject(new Exception($payload['error']['message']));
|
185 | 190 | });
|
186 | 191 | }
|
@@ -280,7 +285,76 @@ public function close($fd)
|
280 | 285 | return $fileDescriptor->softTerminate();
|
281 | 286 | });
|
282 | 287 | }
|
283 |
| - |
| 288 | + |
| 289 | + /** |
| 290 | + * Reads the entire file. |
| 291 | + * |
| 292 | + * This is an optimization for adapters which can optimize |
| 293 | + * the open -> (seek ->) read -> close sequence into one call. |
| 294 | + * |
| 295 | + * @param string $path |
| 296 | + * @param int $offset |
| 297 | + * @param int|null $length |
| 298 | + * @return PromiseInterface |
| 299 | + */ |
| 300 | + public function getContents($path, $offset = 0, $length = null) |
| 301 | + { |
| 302 | + return $this->invoker->invokeCall('getContents', [ |
| 303 | + 'path' => $path, |
| 304 | + 'offset' => $offset, |
| 305 | + 'maxlen' => $length, |
| 306 | + ])->then(function ($payload) { |
| 307 | + return \React\Promise\resolve(base64_decode($payload['chunk'])); |
| 308 | + }); |
| 309 | + } |
| 310 | + |
| 311 | + /** |
| 312 | + * Writes the given content to the specified file. |
| 313 | + * If the file exists, the file is truncated. |
| 314 | + * If the file does not exist, the file will be created. |
| 315 | + * |
| 316 | + * This is an optimization for adapters which can optimize |
| 317 | + * the open -> write -> close sequence into one call. |
| 318 | + * |
| 319 | + * @param string $path |
| 320 | + * @param string $content |
| 321 | + * @return PromiseInterface |
| 322 | + * @see AdapterInterface::appendContents() |
| 323 | + */ |
| 324 | + public function putContents($path, $content) |
| 325 | + { |
| 326 | + return $this->invoker->invokeCall('putContents', [ |
| 327 | + 'path' => $path, |
| 328 | + 'chunk' => base64_encode($content), |
| 329 | + 'flags' => 0, |
| 330 | + ])->then(function ($payload) { |
| 331 | + return \React\Promise\resolve($payload['written']); |
| 332 | + }); |
| 333 | + } |
| 334 | + |
| 335 | + /** |
| 336 | + * Appends the given content to the specified file. |
| 337 | + * If the file does not exist, the file will be created. |
| 338 | + * |
| 339 | + * This is an optimization for adapters which can optimize |
| 340 | + * the open -> write -> close sequence into one call. |
| 341 | + * |
| 342 | + * @param string $path |
| 343 | + * @param string $content |
| 344 | + * @return PromiseInterface |
| 345 | + * @see AdapterInterface::putContents() |
| 346 | + */ |
| 347 | + public function appendContents($path, $content) |
| 348 | + { |
| 349 | + return $this->invoker->invokeCall('putContents', [ |
| 350 | + 'path' => $path, |
| 351 | + 'chunk' => base64_encode($content), |
| 352 | + 'flags' => FILE_APPEND, |
| 353 | + ])->then(function ($payload) { |
| 354 | + return \React\Promise\resolve($payload['written']); |
| 355 | + }); |
| 356 | + } |
| 357 | + |
284 | 358 | /**
|
285 | 359 | * @param string $path
|
286 | 360 | * @return PromiseInterface
|
|
0 commit comments