Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[10.x] Make $path optional in Filesystem methods #44395

Merged
merged 4 commits into from
Nov 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions src/Illuminate/Filesystem/FilesystemAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -370,13 +370,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 @@ -385,14 +389,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