diff --git a/src/Illuminate/Filesystem/FilesystemAdapter.php b/src/Illuminate/Filesystem/FilesystemAdapter.php index f48f66135096..783ef3dc3a40 100644 --- a/src/Illuminate/Filesystem/FilesystemAdapter.php +++ b/src/Illuminate/Filesystem/FilesystemAdapter.php @@ -236,7 +236,7 @@ public function get($path) try { return $this->driver->read($path); } catch (UnableToReadFile $e) { - // + throw_if($this->throwsExceptions(), $e); } } @@ -330,6 +330,8 @@ public function put($path, $contents, $options = []) ? $this->driver->writeStream($path, $contents, $options) : $this->driver->write($path, $contents, $options); } catch (UnableToWriteFile $e) { + throw_if($this->throwsExceptions(), $e); + return false; } @@ -405,6 +407,8 @@ public function setVisibility($path, $visibility) try { $this->driver->setVisibility($path, $this->parseVisibility($visibility)); } catch (UnableToSetVisibility $e) { + throw_if($this->throwsExceptions(), $e); + return false; } @@ -461,6 +465,8 @@ public function delete($paths) try { $this->driver->delete($path); } catch (UnableToDeleteFile $e) { + throw_if($this->throwsExceptions(), $e); + $success = false; } } @@ -480,6 +486,8 @@ public function copy($from, $to) try { $this->driver->copy($from, $to); } catch (UnableToCopyFile $e) { + throw_if($this->throwsExceptions(), $e); + return false; } @@ -498,6 +506,8 @@ public function move($from, $to) try { $this->driver->move($from, $to); } catch (UnableToMoveFile $e) { + throw_if($this->throwsExceptions(), $e); + return false; } @@ -545,7 +555,7 @@ public function readStream($path) try { return $this->driver->readStream($path); } catch (UnableToReadFile $e) { - // + throw_if($this->throwsExceptions(), $e); } } @@ -557,6 +567,8 @@ public function writeStream($path, $resource, array $options = []) try { $this->driver->writeStream($path, $resource, $options); } catch (UnableToWriteFile $e) { + throw_if($this->throwsExceptions(), $e); + return false; } @@ -753,6 +765,8 @@ public function makeDirectory($path) try { $this->driver->createDirectory($path); } catch (UnableToCreateDirectory $e) { + throw_if($this->throwsExceptions(), $e); + return false; } @@ -770,6 +784,8 @@ public function deleteDirectory($directory) try { $this->driver->deleteDirectory($directory); } catch (UnableToDeleteDirectory $e) { + throw_if($this->throwsExceptions(), $e); + return false; } @@ -838,6 +854,16 @@ public function buildTemporaryUrlsUsing(Closure $callback) $this->temporaryUrlCallback = $callback; } + /** + * Determine if Flysystem exceptions should be thrown. + * + * @return bool + */ + protected function throwsExceptions(): bool + { + return (bool) ($this->config['throw'] ?? false); + } + /** * Pass dynamic methods call onto Flysystem. * diff --git a/tests/Filesystem/FilesystemAdapterTest.php b/tests/Filesystem/FilesystemAdapterTest.php index c1fabc8255d0..f125edbe74ba 100644 --- a/tests/Filesystem/FilesystemAdapterTest.php +++ b/tests/Filesystem/FilesystemAdapterTest.php @@ -13,6 +13,8 @@ use League\Flysystem\Filesystem; use League\Flysystem\Ftp\FtpAdapter; use League\Flysystem\Local\LocalFilesystemAdapter; +use League\Flysystem\UnableToReadFile; +use League\Flysystem\UnableToWriteFile; use Mockery as m; use PHPUnit\Framework\TestCase; use Symfony\Component\HttpFoundation\StreamedResponse; @@ -393,4 +395,52 @@ public function testTemporaryUrlWithCustomCallback() $filesystemAdapter->temporaryUrl($path, $expiration, $options) ); } + + public function testThrowExceptionForGet() + { + $adapter = new FilesystemAdapter($this->filesystem, $this->adapter, ['throw' => true]); + + try { + $adapter->get('/foo.txt'); + } catch (UnableToReadFile $e) { + $this->assertTrue(true); + + return; + } + + $this->fail('Exception was not thrown.'); + } + + public function testThrowExceptionsForReadStream() + { + $adapter = new FilesystemAdapter($this->filesystem, $this->adapter, ['throw' => true]); + + try { + $adapter->readStream('/foo.txt'); + } catch (UnableToReadFile $e) { + $this->assertTrue(true); + + return; + } + + $this->fail('Exception was not thrown.'); + } + + /** @requires OS Linux|Darwin */ + public function testThrowExceptionsForPut() + { + mkdir(__DIR__.'/tmp/bar', 0600); + + $adapter = new FilesystemAdapter($this->filesystem, $this->adapter, ['throw' => true]); + + try { + $adapter->put('/bar/foo.txt', 'Hello World!'); + } catch (UnableToWriteFile $e) { + $this->assertTrue(true); + + return; + } + + $this->fail('Exception was not thrown.'); + } }