From 481f76000c861e3e2540dcdda986fb44622ccbbe Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Tue, 29 Nov 2016 09:20:09 -0600 Subject: [PATCH] allow array of options on filesystem operations --- .../Filesystem/FilesystemAdapter.php | 30 ++++++------- src/Illuminate/Http/UploadedFile.php | 44 +++++++++++++------ 2 files changed, 45 insertions(+), 29 deletions(-) diff --git a/src/Illuminate/Filesystem/FilesystemAdapter.php b/src/Illuminate/Filesystem/FilesystemAdapter.php index c62e008ea479..d67b739ca015 100644 --- a/src/Illuminate/Filesystem/FilesystemAdapter.php +++ b/src/Illuminate/Filesystem/FilesystemAdapter.php @@ -70,25 +70,23 @@ public function get($path) * * @param string $path * @param string|resource $contents - * @param string $visibility + * @param array $options * @return bool */ - public function put($path, $contents, $visibility = null) + public function put($path, $contents, $options = []) { - if ($contents instanceof File || $contents instanceof UploadedFile) { - return $this->putFile($path, $contents, $visibility); + if (is_string($options)) { + $options = ['visibility' => $options]; } - if ($visibility = $this->parseVisibility($visibility)) { - $config = ['visibility' => $visibility]; - } else { - $config = []; + if ($contents instanceof File || $contents instanceof UploadedFile) { + return $this->putFile($path, $contents, $options); } if (is_resource($contents)) { - return $this->driver->putStream($path, $contents, $config); + return $this->driver->putStream($path, $contents, $options); } else { - return $this->driver->put($path, $contents, $config); + return $this->driver->put($path, $contents, $options); } } @@ -97,12 +95,12 @@ public function put($path, $contents, $visibility = null) * * @param string $path * @param \Illuminate\Http\UploadedFile $file - * @param string $visibility + * @param array $options * @return string|false */ - public function putFile($path, $file, $visibility = null) + public function putFile($path, $file, $options = []) { - return $this->putFileAs($path, $file, $file->hashName(), $visibility); + return $this->putFileAs($path, $file, $file->hashName(), $options); } /** @@ -111,14 +109,14 @@ public function putFile($path, $file, $visibility = null) * @param string $path * @param \Illuminate\Http\File|\Illuminate\Http\UploadedFile $file * @param string $name - * @param string $visibility + * @param array $options * @return string|false */ - public function putFileAs($path, $file, $name, $visibility = null) + public function putFileAs($path, $file, $name, $options = []) { $stream = fopen($file->getRealPath(), 'r+'); - $result = $this->put($path = trim($path.'/'.$name, '/'), $stream, $visibility); + $result = $this->put($path = trim($path.'/'.$name, '/'), $stream, $options); if (is_resource($stream)) { fclose($stream); diff --git a/src/Illuminate/Http/UploadedFile.php b/src/Illuminate/Http/UploadedFile.php index 2421168fbbe2..d91f61ed36ef 100644 --- a/src/Illuminate/Http/UploadedFile.php +++ b/src/Illuminate/Http/UploadedFile.php @@ -2,6 +2,7 @@ namespace Illuminate\Http; +use Illuminate\Support\Arr; use Illuminate\Container\Container; use Illuminate\Support\Traits\Macroable; use Illuminate\Contracts\Filesystem\Factory as FilesystemFactory; @@ -15,24 +16,34 @@ class UploadedFile extends SymfonyUploadedFile * Store the uploaded file on a filesystem disk. * * @param string $path - * @param string|null $disk + * @param array $options * @return string|false */ - public function store($path, $disk = null) + public function store($path, $options = []) { - return $this->storeAs($path, $this->hashName(), $disk); + if (is_string($options)) { + $options = ['disk' => $options]; + } + + return $this->storeAs($path, $this->hashName(), $options); } /** * Store the uploaded file on a filesystem disk with public visibility. * * @param string $path - * @param string|null $disk + * @param array $options * @return string|false */ - public function storePublicly($path, $disk = null) + public function storePublicly($path, $options = []) { - return $this->storeAs($path, $this->hashName(), $disk, 'public'); + if (is_string($options)) { + $options = ['disk' => $options]; + } + + $options['visibility'] = 'public'; + + return $this->storeAs($path, $this->hashName(), $options); } /** @@ -40,12 +51,18 @@ public function storePublicly($path, $disk = null) * * @param string $path * @param string $name - * @param string|null $disk + * @param array $options * @return string|false */ - public function storePubliclyAs($path, $name, $disk = null) + public function storePubliclyAs($path, $name, $options = []) { - return $this->storeAs($path, $name, $disk, 'public'); + if (is_string($options)) { + $options = ['disk' => $options]; + } + + $options['visibility'] = 'public'; + + return $this->storeAs($path, $name, $options); } /** @@ -53,15 +70,16 @@ public function storePubliclyAs($path, $name, $disk = null) * * @param string $path * @param string $name - * @param string|null $disk - * @param string|null $visibility + * @param array $options * @return string|false */ - public function storeAs($path, $name, $disk = null, $visibility = null) + public function storeAs($path, $name, $options = []) { $factory = Container::getInstance()->make(FilesystemFactory::class); - return $factory->disk($disk)->putFileAs($path, $this, $name, $visibility); + $disk = Arr::pull($options, 'disk'); + + return $factory->disk($disk)->putFileAs($path, $this, $name, $options); } /**