Skip to content

Commit cfe6b42

Browse files
Gman98ishtaylorotwell
authored andcommitted
[6.x] Allow Storage::put to accept a Psr StreamInterface (#30179)
* Allow Storage::put to accept a Psr StreamInterface Addresses: laravel/ideas#1252 * Moved psr/http-message to a suggested dependency * Fixed error in filesystem composer.json
1 parent 4aed56f commit cfe6b42

File tree

4 files changed

+24
-1
lines changed

4 files changed

+24
-1
lines changed

composer.json

+1
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@
127127
"league/flysystem-aws-s3-v3": "Required to use the Flysystem S3 driver (^1.0).",
128128
"league/flysystem-cached-adapter": "Required to use the Flysystem cache (^1.0).",
129129
"league/flysystem-sftp": "Required to use the Flysystem SFTP driver (^1.0).",
130+
"psr/http-message": "Required to allow Storage::put to accept a StreamInterface (^1.0)",
130131
"moontoast/math": "Required to use ordered UUIDs (^1.1).",
131132
"pda/pheanstalk": "Required to use the beanstalk queue driver (^4.0).",
132133
"pusher/pusher-php-server": "Required to use the Pusher broadcast driver (^4.0).",

src/Illuminate/Filesystem/FilesystemAdapter.php

+5
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);

src/Illuminate/Filesystem/composer.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@
3333
"league/flysystem": "Required to use the Flysystem local and FTP drivers (^1.0).",
3434
"league/flysystem-aws-s3-v3": "Required to use the Flysystem S3 driver (^1.0).",
3535
"league/flysystem-cached-adapter": "Required to use the Flysystem cache (^1.0).",
36-
"league/flysystem-sftp": "Required to use the Flysystem SFTP driver (^1.0)."
36+
"league/flysystem-sftp": "Required to use the Flysystem SFTP driver (^1.0).",
37+
"psr/http-message": "Required to allow Storage::put to accept a StreamInterface (^1.0)"
3738
},
3839
"config": {
3940
"sort-packages": true

tests/Filesystem/FilesystemAdapterTest.php

+16
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
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;
1214
use Symfony\Component\HttpFoundation\StreamedResponse;
1315

@@ -26,6 +28,7 @@ protected function tearDown(): void
2628
{
2729
$filesystem = new Filesystem(new Local(dirname($this->tempDir)));
2830
$filesystem->deleteDir(basename($this->tempDir));
31+
m::close();
2932
}
3033

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

0 commit comments

Comments
 (0)