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-putcontentsviawritestream.php
30 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WordPress\Filesystem\Mixin; |
| 4 | |
| 5 | use WordPress\ByteStream\ReadStream\ByteReadStream; |
| 6 | use WordPress\Filesystem\FilesystemException; |
| 7 | |
| 8 | /** |
| 9 | * Implements put_contents() using the write stream provided by the open_write_stream() method. |
| 10 | */ |
| 11 | trait PutContentsViaWriteStream { |
| 12 | |
| 13 | public function put_contents( $path, $data, $options = array() ) { |
| 14 | $stream = $this->open_write_stream( $path ); |
| 15 | try { |
| 16 | if ( is_string( $data ) ) { |
| 17 | $stream->append_bytes( $data ); |
| 18 | } elseif ( is_object( $data ) && $data instanceof ByteReadStream ) { |
| 19 | while ( $data->pull() ) { |
| 20 | $stream->append_bytes( $data->peek() ); |
| 21 | } |
| 22 | } else { |
| 23 | throw new FilesystemException( 'Invalid $data argument provided. Expected a string or a Byte_Reader instance. Received: ' . gettype( $data ) ); |
| 24 | } |
| 25 | } finally { |
| 26 | $stream->close_writing(); |
| 27 | } |
| 28 | } |
| 29 | } |
| 30 |