jetpack
/
vendor
/
wp-php-toolkit
/
filesystem
/
Mixin
/
trait-bufferedwritestreamviaputcontents.php
Interfaces
3 weeks ago
trait-bufferedwritestreamviaputcontents.php
3 weeks ago
trait-copydirectoryrecursive.php
3 weeks ago
trait-copyfileviastreaming.php
3 weeks ago
trait-copyrecursiveviastreaming.php
3 weeks ago
trait-getcontentsviareadstream.php
3 weeks ago
trait-internalizewritestream.php
3 weeks ago
trait-mkdirrecursive.php
3 weeks ago
trait-putcontentsviawritestream.php
3 weeks ago
trait-readonlyfilesystem.php
3 weeks ago
trait-renamefileviacopyandrm.php
3 weeks ago
trait-rmdirrecursive.php
3 weeks ago
trait-bufferedwritestreamviaputcontents.php
35 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WordPress\Filesystem\Mixin; |
| 4 | |
| 5 | use WordPress\ByteStream\MemoryPipe; |
| 6 | use WordPress\ByteStream\WriteStream\ByteWriteStream; |
| 7 | |
| 8 | /** |
| 9 | * Implements open_write_stream() as a buffered write stream that, upon closing, |
| 10 | * writes the contents to the filesystem using the put_contents() method. |
| 11 | */ |
| 12 | trait BufferedWriteStreamViaPutContents { |
| 13 | |
| 14 | public function open_write_stream( $path ): ByteWriteStream { |
| 15 | $fs = $this; |
| 16 | |
| 17 | return new class( $fs, $path ) extends MemoryPipe { |
| 18 | private $fs; |
| 19 | private $path; |
| 20 | |
| 21 | public function __construct( $fs, $path ) { |
| 22 | $this->fs = $fs; |
| 23 | $this->path = $path; |
| 24 | } |
| 25 | |
| 26 | public function close_writing(): void { |
| 27 | parent::close_writing(); |
| 28 | $pipe_contents = $this->consume_all(); |
| 29 | parent::close_reading(); |
| 30 | $this->fs->put_contents( $this->path, $pipe_contents ); |
| 31 | } |
| 32 | }; |
| 33 | } |
| 34 | } |
| 35 |