Skip to content

Commit 19bf8ae

Browse files
committed
Allow Storage::put to accept a Psr StreamInterface
Addresses: laravel/ideas#1252
1 parent 88adf85 commit 19bf8ae

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"opis/closure": "^3.1",
3030
"psr/container": "^1.0",
3131
"psr/simple-cache": "^1.0",
32+
"psr/http-message": "^1.0",
3233
"ramsey/uuid": "^3.7",
3334
"swiftmailer/swiftmailer": "^6.0",
3435
"symfony/console": "^4.3.4",

src/Illuminate/Filesystem/FilesystemAdapter.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use League\Flysystem\FileNotFoundException;
2121
use League\Flysystem\FilesystemInterface;
2222
use PHPUnit\Framework\Assert as PHPUnit;
23+
use Psr\Http\Message\StreamInterface;
2324
use RuntimeException;
2425
use Symfony\Component\HttpFoundation\StreamedResponse;
2526

@@ -203,6 +204,10 @@ public function put($path, $contents, $options = [])
203204
return $this->putFile($path, $contents, $options);
204205
}
205206

207+
if ($contents instanceof StreamInterface) {
208+
return $this->driver->putStream($path, $contents->detach(), $options);
209+
}
210+
206211
return is_resource($contents)
207212
? $this->driver->putStream($path, $contents, $options)
208213
: $this->driver->put($path, $contents, $options);

tests/Filesystem/FilesystemAdapterTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@
22

33
namespace Illuminate\Tests\Filesystem;
44

5+
use GuzzleHttp\Psr7\Stream;
56
use Illuminate\Contracts\Filesystem\FileExistsException;
67
use Illuminate\Contracts\Filesystem\FileNotFoundException;
78
use Illuminate\Filesystem\FilesystemAdapter;
89
use InvalidArgumentException;
910
use League\Flysystem\Adapter\Local;
1011
use League\Flysystem\Filesystem;
12+
use Mockery as m;
1113
use PHPUnit\Framework\TestCase;
14+
use Psr\Http\Message\StreamInterface;
1215
use Symfony\Component\HttpFoundation\StreamedResponse;
1316

1417
class FilesystemAdapterTest extends TestCase
@@ -26,6 +29,7 @@ protected function tearDown(): void
2629
{
2730
$filesystem = new Filesystem(new Local(dirname($this->tempDir)));
2831
$filesystem->deleteDir(basename($this->tempDir));
32+
m::close();
2933
}
3034

3135
public function testResponse()
@@ -218,4 +222,17 @@ public function testStreamInvalidResourceThrows()
218222
$filesystemAdapter = new FilesystemAdapter($this->filesystem);
219223
$filesystemAdapter->writeStream('file.txt', 'foo bar');
220224
}
225+
226+
public function testPutWithStreamInterface()
227+
{
228+
file_put_contents($this->tempDir.'/foo.txt', 'some-data');
229+
$spy = m::spy($this->filesystem);
230+
231+
$filesystemAdapter = new FilesystemAdapter($spy);
232+
$stream = new Stream(fopen($this->tempDir.'/foo.txt', 'r'));
233+
$filesystemAdapter->put('bar.txt', $stream);
234+
235+
$spy->shouldHaveReceived('putStream');
236+
$this->assertEquals('some-data', $filesystemAdapter->get('bar.txt'));
237+
}
221238
}

0 commit comments

Comments
 (0)