File.php 834 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. <?php
  2. namespace think\swoole\rpc\packer;
  3. class File
  4. {
  5. protected $name;
  6. protected $handle;
  7. protected $length;
  8. public function __construct($length)
  9. {
  10. $this->length = $length;
  11. }
  12. public function write(&$data)
  13. {
  14. if (!$this->handle) {
  15. $this->name = tempnam(sys_get_temp_dir(), 'swoole_rpc_');
  16. $this->handle = fopen($this->name, 'ab');
  17. }
  18. $size = fstat($this->handle)['size'];
  19. $string = substr($data, 0, $this->length - $size);
  20. fwrite($this->handle, $string);
  21. if (strlen($data) >= $this->length - $size) {
  22. fclose($this->handle);
  23. $data = substr($data, $this->length - $size);
  24. return new \think\swoole\rpc\File($this->name);
  25. } else {
  26. $data = '';
  27. }
  28. }
  29. }