Skip to content

Commit 31a801d

Browse files
committed
fet: add Stream::putContents()
1 parent e8e76e4 commit 31a801d

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

src/Stream.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,20 @@ public function contents(?int $length = null, int $offset = -1): string
120120
return $contents;
121121
}
122122

123+
/**
124+
* @see \file_put_contents()
125+
*
126+
* @param resource|null $context
127+
*/
128+
public function putContents(string $filename, int $flags = 0, $context = null): self
129+
{
130+
if (false === @\file_put_contents($filename, $this->get(), $flags, $context)) {
131+
throw new \RuntimeException(\sprintf('Unable to dump contents of stream to "%s".', $filename));
132+
}
133+
134+
return $this;
135+
}
136+
123137
/**
124138
* @see \rewind()
125139
*/

tests/StreamTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,4 +191,21 @@ public function is_open_closed(): void
191191
$this->assertFalse($stream->isOpen());
192192
$this->assertTrue($stream->isClosed());
193193
}
194+
195+
/**
196+
* @test
197+
*/
198+
public function put_contents(): void
199+
{
200+
$file = Stream::tempFile()->uri();
201+
202+
\file_put_contents($file, '');
203+
204+
$this->assertFileExists($file);
205+
$this->assertSame('', \file_get_contents($file));
206+
207+
Stream::wrap('content')->putContents($file);
208+
209+
$this->assertSame('content', \file_get_contents($file));
210+
}
194211
}

0 commit comments

Comments
 (0)