Skip to content

Commit

Permalink
[10.x] Make $path optional in Filesystem methods (#44395)
Browse files Browse the repository at this point in the history
* feat(filesystem): make `path` optional

* feat(filesystem): make `put` path optional

* chore: fix `put` type hints

* formatting

Co-authored-by: Taylor Otwell <[email protected]>
  • Loading branch information
innocenzi and taylorotwell authored Nov 2, 2022
1 parent b856dc5 commit 8d47416
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 12 deletions.
22 changes: 15 additions & 7 deletions src/Illuminate/Filesystem/FilesystemAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -371,13 +371,17 @@ public function put($path, $contents, $options = [])
/**
* Store the uploaded file on the disk.
*
* @param string $path
* @param \Illuminate\Http\File|\Illuminate\Http\UploadedFile|string $file
* @param \Illuminate\Http\File|\Illuminate\Http\UploadedFile|string $path
* @param \Illuminate\Http\File|\Illuminate\Http\UploadedFile|string|array|null $file
* @param mixed $options
* @return string|false
*/
public function putFile($path, $file, $options = [])
public function putFile($path, $file = null, $options = [])
{
if (is_null($file) || is_array($file)) {
[$path, $file, $options] = ['', $path, $file ?? []];
}

$file = is_string($file) ? new File($file) : $file;

return $this->putFileAs($path, $file, $file->hashName(), $options);
Expand All @@ -386,14 +390,18 @@ public function putFile($path, $file, $options = [])
/**
* Store the uploaded file on the disk with a given name.
*
* @param string $path
* @param \Illuminate\Http\File|\Illuminate\Http\UploadedFile|string $file
* @param string $name
* @param \Illuminate\Http\File|\Illuminate\Http\UploadedFile|string $path
* @param \Illuminate\Http\File|\Illuminate\Http\UploadedFile|string|array|null $file
* @param string|array|null $name
* @param mixed $options
* @return string|false
*/
public function putFileAs($path, $file, $name, $options = [])
public function putFileAs($path, $file, $name = null, $options = [])
{
if (is_null($name) || is_array($name)) {
[$path, $file, $name, $options] = ['', $path, $file, $name ?? []];
}

$stream = fopen(is_string($file) ? $file : $file->getRealPath(), 'r');

// Next, we will format the path of the file and store the file using a stream since
Expand Down
18 changes: 13 additions & 5 deletions src/Illuminate/Http/UploadedFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public static function fake()
* @param array|string $options
* @return string|false
*/
public function store($path, $options = [])
public function store($path = '', $options = [])
{
return $this->storeAs($path, $this->hashName(), $this->parseOptions($options));
}
Expand All @@ -43,7 +43,7 @@ public function store($path, $options = [])
* @param array|string $options
* @return string|false
*/
public function storePublicly($path, $options = [])
public function storePublicly($path = '', $options = [])
{
$options = $this->parseOptions($options);

Expand All @@ -60,8 +60,12 @@ public function storePublicly($path, $options = [])
* @param array|string $options
* @return string|false
*/
public function storePubliclyAs($path, $name, $options = [])
public function storePubliclyAs($path, $name = null, $options = [])
{
if (is_null($name) || is_array($name)) {
[$path, $name, $options] = ['', $path, $options ?? []];
}

$options = $this->parseOptions($options);

$options['visibility'] = 'public';
Expand All @@ -73,12 +77,16 @@ public function storePubliclyAs($path, $name, $options = [])
* Store the uploaded file on a filesystem disk.
*
* @param string $path
* @param string $name
* @param string|array $name
* @param array|string $options
* @return string|false
*/
public function storeAs($path, $name, $options = [])
public function storeAs($path, $name = null, $options = [])
{
if (is_null($name) || is_array($name)) {
[$path, $name, $options] = ['', $path, $options ?? []];
}

$options = $this->parseOptions($options);

$disk = Arr::pull($options, 'disk');
Expand Down
22 changes: 22 additions & 0 deletions tests/Filesystem/FilesystemAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,17 @@ public function testPutFileAsWithAbsoluteFilePath()
$this->assertSame('normal file content', $filesystemAdapter->read($storagePath));
}

public function testPutFileAsWithoutPath()
{
file_put_contents($filePath = $this->tempDir.'/foo.txt', 'normal file content');

$filesystemAdapter = new FilesystemAdapter($this->filesystem, $this->adapter);

$storagePath = $filesystemAdapter->putFileAs($filePath, 'new.txt');

$this->assertSame('normal file content', $filesystemAdapter->read($storagePath));
}

public function testPutFile()
{
file_put_contents($filePath = $this->tempDir.'/foo.txt', 'uploaded file content');
Expand Down Expand Up @@ -390,6 +401,17 @@ public function testPutFileWithAbsoluteFilePath()
);
}

public function testPutFileWithoutPath()
{
file_put_contents($filePath = $this->tempDir.'/foo.txt', 'normal file content');

$filesystemAdapter = new FilesystemAdapter($this->filesystem, $this->adapter);

$storagePath = $filesystemAdapter->putFile($filePath);

$this->assertSame('normal file content', $filesystemAdapter->read($storagePath));
}

/**
* @requires extension ftp
*/
Expand Down

0 comments on commit 8d47416

Please sign in to comment.