Skip to content

Commit 8d47416

Browse files
[10.x] Make $path optional in Filesystem methods (#44395)
* feat(filesystem): make `path` optional * feat(filesystem): make `put` path optional * chore: fix `put` type hints * formatting Co-authored-by: Taylor Otwell <[email protected]>
1 parent b856dc5 commit 8d47416

File tree

3 files changed

+50
-12
lines changed

3 files changed

+50
-12
lines changed

src/Illuminate/Filesystem/FilesystemAdapter.php

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -371,13 +371,17 @@ public function put($path, $contents, $options = [])
371371
/**
372372
* Store the uploaded file on the disk.
373373
*
374-
* @param string $path
375-
* @param \Illuminate\Http\File|\Illuminate\Http\UploadedFile|string $file
374+
* @param \Illuminate\Http\File|\Illuminate\Http\UploadedFile|string $path
375+
* @param \Illuminate\Http\File|\Illuminate\Http\UploadedFile|string|array|null $file
376376
* @param mixed $options
377377
* @return string|false
378378
*/
379-
public function putFile($path, $file, $options = [])
379+
public function putFile($path, $file = null, $options = [])
380380
{
381+
if (is_null($file) || is_array($file)) {
382+
[$path, $file, $options] = ['', $path, $file ?? []];
383+
}
384+
381385
$file = is_string($file) ? new File($file) : $file;
382386

383387
return $this->putFileAs($path, $file, $file->hashName(), $options);
@@ -386,14 +390,18 @@ public function putFile($path, $file, $options = [])
386390
/**
387391
* Store the uploaded file on the disk with a given name.
388392
*
389-
* @param string $path
390-
* @param \Illuminate\Http\File|\Illuminate\Http\UploadedFile|string $file
391-
* @param string $name
393+
* @param \Illuminate\Http\File|\Illuminate\Http\UploadedFile|string $path
394+
* @param \Illuminate\Http\File|\Illuminate\Http\UploadedFile|string|array|null $file
395+
* @param string|array|null $name
392396
* @param mixed $options
393397
* @return string|false
394398
*/
395-
public function putFileAs($path, $file, $name, $options = [])
399+
public function putFileAs($path, $file, $name = null, $options = [])
396400
{
401+
if (is_null($name) || is_array($name)) {
402+
[$path, $file, $name, $options] = ['', $path, $file, $name ?? []];
403+
}
404+
397405
$stream = fopen(is_string($file) ? $file : $file->getRealPath(), 'r');
398406

399407
// Next, we will format the path of the file and store the file using a stream since

src/Illuminate/Http/UploadedFile.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public static function fake()
3131
* @param array|string $options
3232
* @return string|false
3333
*/
34-
public function store($path, $options = [])
34+
public function store($path = '', $options = [])
3535
{
3636
return $this->storeAs($path, $this->hashName(), $this->parseOptions($options));
3737
}
@@ -43,7 +43,7 @@ public function store($path, $options = [])
4343
* @param array|string $options
4444
* @return string|false
4545
*/
46-
public function storePublicly($path, $options = [])
46+
public function storePublicly($path = '', $options = [])
4747
{
4848
$options = $this->parseOptions($options);
4949

@@ -60,8 +60,12 @@ public function storePublicly($path, $options = [])
6060
* @param array|string $options
6161
* @return string|false
6262
*/
63-
public function storePubliclyAs($path, $name, $options = [])
63+
public function storePubliclyAs($path, $name = null, $options = [])
6464
{
65+
if (is_null($name) || is_array($name)) {
66+
[$path, $name, $options] = ['', $path, $options ?? []];
67+
}
68+
6569
$options = $this->parseOptions($options);
6670

6771
$options['visibility'] = 'public';
@@ -73,12 +77,16 @@ public function storePubliclyAs($path, $name, $options = [])
7377
* Store the uploaded file on a filesystem disk.
7478
*
7579
* @param string $path
76-
* @param string $name
80+
* @param string|array $name
7781
* @param array|string $options
7882
* @return string|false
7983
*/
80-
public function storeAs($path, $name, $options = [])
84+
public function storeAs($path, $name = null, $options = [])
8185
{
86+
if (is_null($name) || is_array($name)) {
87+
[$path, $name, $options] = ['', $path, $options ?? []];
88+
}
89+
8290
$options = $this->parseOptions($options);
8391

8492
$disk = Arr::pull($options, 'disk');

tests/Filesystem/FilesystemAdapterTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,17 @@ public function testPutFileAsWithAbsoluteFilePath()
350350
$this->assertSame('normal file content', $filesystemAdapter->read($storagePath));
351351
}
352352

353+
public function testPutFileAsWithoutPath()
354+
{
355+
file_put_contents($filePath = $this->tempDir.'/foo.txt', 'normal file content');
356+
357+
$filesystemAdapter = new FilesystemAdapter($this->filesystem, $this->adapter);
358+
359+
$storagePath = $filesystemAdapter->putFileAs($filePath, 'new.txt');
360+
361+
$this->assertSame('normal file content', $filesystemAdapter->read($storagePath));
362+
}
363+
353364
public function testPutFile()
354365
{
355366
file_put_contents($filePath = $this->tempDir.'/foo.txt', 'uploaded file content');
@@ -390,6 +401,17 @@ public function testPutFileWithAbsoluteFilePath()
390401
);
391402
}
392403

404+
public function testPutFileWithoutPath()
405+
{
406+
file_put_contents($filePath = $this->tempDir.'/foo.txt', 'normal file content');
407+
408+
$filesystemAdapter = new FilesystemAdapter($this->filesystem, $this->adapter);
409+
410+
$storagePath = $filesystemAdapter->putFile($filePath);
411+
412+
$this->assertSame('normal file content', $filesystemAdapter->read($storagePath));
413+
}
414+
393415
/**
394416
* @requires extension ftp
395417
*/

0 commit comments

Comments
 (0)