From 85c505f85536c43547a8a865031d07bfbd180340 Mon Sep 17 00:00:00 2001 From: Dries Vints Date: Thu, 3 Mar 2022 10:56:07 +0100 Subject: [PATCH 1/7] Optionally cascade thrown Flysystem exceptions --- .../Filesystem/FilesystemAdapter.php | 51 ++++++++++++++++++- tests/Filesystem/FilesystemAdapterTest.php | 49 ++++++++++++++++++ 2 files changed, 98 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/Filesystem/FilesystemAdapter.php b/src/Illuminate/Filesystem/FilesystemAdapter.php index f48f66135096..de6e5b08180f 100644 --- a/src/Illuminate/Filesystem/FilesystemAdapter.php +++ b/src/Illuminate/Filesystem/FilesystemAdapter.php @@ -236,7 +236,9 @@ public function get($path) try { return $this->driver->read($path); } catch (UnableToReadFile $e) { - // + if ($this->throwsExceptions()) { + throw $e; + } } } @@ -330,6 +332,10 @@ public function put($path, $contents, $options = []) ? $this->driver->writeStream($path, $contents, $options) : $this->driver->write($path, $contents, $options); } catch (UnableToWriteFile $e) { + if ($this->throwsExceptions()) { + throw $e; + } + return false; } @@ -405,6 +411,10 @@ public function setVisibility($path, $visibility) try { $this->driver->setVisibility($path, $this->parseVisibility($visibility)); } catch (UnableToSetVisibility $e) { + if ($this->throwsExceptions()) { + throw $e; + } + return false; } @@ -461,6 +471,10 @@ public function delete($paths) try { $this->driver->delete($path); } catch (UnableToDeleteFile $e) { + if ($this->throwsExceptions()) { + throw $e; + } + $success = false; } } @@ -480,6 +494,10 @@ public function copy($from, $to) try { $this->driver->copy($from, $to); } catch (UnableToCopyFile $e) { + if ($this->throwsExceptions()) { + throw $e; + } + return false; } @@ -498,6 +516,10 @@ public function move($from, $to) try { $this->driver->move($from, $to); } catch (UnableToMoveFile $e) { + if ($this->throwsExceptions()) { + throw $e; + } + return false; } @@ -545,7 +567,9 @@ public function readStream($path) try { return $this->driver->readStream($path); } catch (UnableToReadFile $e) { - // + if ($this->throwsExceptions()) { + throw $e; + } } } @@ -557,6 +581,10 @@ public function writeStream($path, $resource, array $options = []) try { $this->driver->writeStream($path, $resource, $options); } catch (UnableToWriteFile $e) { + if ($this->throwsExceptions()) { + throw $e; + } + return false; } @@ -753,6 +781,10 @@ public function makeDirectory($path) try { $this->driver->createDirectory($path); } catch (UnableToCreateDirectory $e) { + if ($this->throwsExceptions()) { + throw $e; + } + return false; } @@ -770,6 +802,10 @@ public function deleteDirectory($directory) try { $this->driver->deleteDirectory($directory); } catch (UnableToDeleteDirectory $e) { + if ($this->throwsExceptions()) { + throw $e; + } + return false; } @@ -838,6 +874,17 @@ 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['throws_exceptions'] ?? false); + } + /** * Pass dynamic methods call onto Flysystem. * diff --git a/tests/Filesystem/FilesystemAdapterTest.php b/tests/Filesystem/FilesystemAdapterTest.php index c1fabc8255d0..f5a8bf39f8f2 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,51 @@ public function testTemporaryUrlWithCustomCallback() $filesystemAdapter->temporaryUrl($path, $expiration, $options) ); } + + public function testThrowExceptionForGet() + { + $adapter = new FilesystemAdapter($this->filesystem, $this->adapter, ['throws_exceptions' => 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, ['throws_exceptions' => true]); + + try { + $adapter->readStream('/foo.txt'); + } catch (UnableToReadFile $e) { + $this->assertTrue(true); + + return; + } + + $this->fail('Exception was not thrown.'); + } + + public function testThrowExceptionsForPut() + { + mkdir(__DIR__.'/tmp/bar', 0600); + + $adapter = new FilesystemAdapter($this->filesystem, $this->adapter, ['throws_exceptions' => true]); + + try { + $adapter->put('/bar/foo.txt', 'Hello World!'); + } catch (UnableToWriteFile $e) { + $this->assertTrue(true); + + return; + } + + $this->fail('Exception was not thrown.'); + } } From c005788104520045b4eb41c1b68027d96ecc574d Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Thu, 3 Mar 2022 09:56:40 +0000 Subject: [PATCH 2/7] Apply fixes from StyleCI --- src/Illuminate/Filesystem/FilesystemAdapter.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Illuminate/Filesystem/FilesystemAdapter.php b/src/Illuminate/Filesystem/FilesystemAdapter.php index de6e5b08180f..043f11e4d1de 100644 --- a/src/Illuminate/Filesystem/FilesystemAdapter.php +++ b/src/Illuminate/Filesystem/FilesystemAdapter.php @@ -874,7 +874,6 @@ public function buildTemporaryUrlsUsing(Closure $callback) $this->temporaryUrlCallback = $callback; } - /** * Determine if Flysystem exceptions should be thrown. * From 4d18aff94c6783c2510a8fd35635870a822deda6 Mon Sep 17 00:00:00 2001 From: Dries Vints Date: Thu, 3 Mar 2022 11:00:52 +0100 Subject: [PATCH 3/7] wip --- tests/Filesystem/FilesystemAdapterTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Filesystem/FilesystemAdapterTest.php b/tests/Filesystem/FilesystemAdapterTest.php index f5a8bf39f8f2..79e5daeeb906 100644 --- a/tests/Filesystem/FilesystemAdapterTest.php +++ b/tests/Filesystem/FilesystemAdapterTest.php @@ -428,7 +428,7 @@ public function testThrowExceptionsForReadStream() public function testThrowExceptionsForPut() { - mkdir(__DIR__.'/tmp/bar', 0600); + mkdir(__DIR__.DIRECTORY_SEPARATOR.'tmp'.DIRECTORY_SEPARATOR.'bar', 0600); $adapter = new FilesystemAdapter($this->filesystem, $this->adapter, ['throws_exceptions' => true]); From dfebc333dbe7f2b5863ff09e44ad6ec9d1e085a2 Mon Sep 17 00:00:00 2001 From: Dries Vints Date: Thu, 3 Mar 2022 11:07:26 +0100 Subject: [PATCH 4/7] wip --- tests/Filesystem/FilesystemAdapterTest.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/Filesystem/FilesystemAdapterTest.php b/tests/Filesystem/FilesystemAdapterTest.php index 79e5daeeb906..2d5ca5941241 100644 --- a/tests/Filesystem/FilesystemAdapterTest.php +++ b/tests/Filesystem/FilesystemAdapterTest.php @@ -426,9 +426,10 @@ public function testThrowExceptionsForReadStream() $this->fail('Exception was not thrown.'); } + /** @requires OS Linux|Darwin */ public function testThrowExceptionsForPut() { - mkdir(__DIR__.DIRECTORY_SEPARATOR.'tmp'.DIRECTORY_SEPARATOR.'bar', 0600); + mkdir(__DIR__.'/tmp/bar', 0600); $adapter = new FilesystemAdapter($this->filesystem, $this->adapter, ['throws_exceptions' => true]); From 1e11a6872e05b55ec60cbb9405f5b5ef0a14f64b Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Thu, 3 Mar 2022 08:35:48 -0600 Subject: [PATCH 5/7] use helper - rename option --- .../Filesystem/FilesystemAdapter.php | 42 +++++-------------- tests/Filesystem/FilesystemAdapterTest.php | 6 +-- 2 files changed, 14 insertions(+), 34 deletions(-) diff --git a/src/Illuminate/Filesystem/FilesystemAdapter.php b/src/Illuminate/Filesystem/FilesystemAdapter.php index 043f11e4d1de..783ef3dc3a40 100644 --- a/src/Illuminate/Filesystem/FilesystemAdapter.php +++ b/src/Illuminate/Filesystem/FilesystemAdapter.php @@ -236,9 +236,7 @@ public function get($path) try { return $this->driver->read($path); } catch (UnableToReadFile $e) { - if ($this->throwsExceptions()) { - throw $e; - } + throw_if($this->throwsExceptions(), $e); } } @@ -332,9 +330,7 @@ public function put($path, $contents, $options = []) ? $this->driver->writeStream($path, $contents, $options) : $this->driver->write($path, $contents, $options); } catch (UnableToWriteFile $e) { - if ($this->throwsExceptions()) { - throw $e; - } + throw_if($this->throwsExceptions(), $e); return false; } @@ -411,9 +407,7 @@ public function setVisibility($path, $visibility) try { $this->driver->setVisibility($path, $this->parseVisibility($visibility)); } catch (UnableToSetVisibility $e) { - if ($this->throwsExceptions()) { - throw $e; - } + throw_if($this->throwsExceptions(), $e); return false; } @@ -471,9 +465,7 @@ public function delete($paths) try { $this->driver->delete($path); } catch (UnableToDeleteFile $e) { - if ($this->throwsExceptions()) { - throw $e; - } + throw_if($this->throwsExceptions(), $e); $success = false; } @@ -494,9 +486,7 @@ public function copy($from, $to) try { $this->driver->copy($from, $to); } catch (UnableToCopyFile $e) { - if ($this->throwsExceptions()) { - throw $e; - } + throw_if($this->throwsExceptions(), $e); return false; } @@ -516,9 +506,7 @@ public function move($from, $to) try { $this->driver->move($from, $to); } catch (UnableToMoveFile $e) { - if ($this->throwsExceptions()) { - throw $e; - } + throw_if($this->throwsExceptions(), $e); return false; } @@ -567,9 +555,7 @@ public function readStream($path) try { return $this->driver->readStream($path); } catch (UnableToReadFile $e) { - if ($this->throwsExceptions()) { - throw $e; - } + throw_if($this->throwsExceptions(), $e); } } @@ -581,9 +567,7 @@ public function writeStream($path, $resource, array $options = []) try { $this->driver->writeStream($path, $resource, $options); } catch (UnableToWriteFile $e) { - if ($this->throwsExceptions()) { - throw $e; - } + throw_if($this->throwsExceptions(), $e); return false; } @@ -781,9 +765,7 @@ public function makeDirectory($path) try { $this->driver->createDirectory($path); } catch (UnableToCreateDirectory $e) { - if ($this->throwsExceptions()) { - throw $e; - } + throw_if($this->throwsExceptions(), $e); return false; } @@ -802,9 +784,7 @@ public function deleteDirectory($directory) try { $this->driver->deleteDirectory($directory); } catch (UnableToDeleteDirectory $e) { - if ($this->throwsExceptions()) { - throw $e; - } + throw_if($this->throwsExceptions(), $e); return false; } @@ -881,7 +861,7 @@ public function buildTemporaryUrlsUsing(Closure $callback) */ protected function throwsExceptions(): bool { - return (bool) ($this->config['throws_exceptions'] ?? false); + return (bool) ($this->config['throw'] ?? false); } /** diff --git a/tests/Filesystem/FilesystemAdapterTest.php b/tests/Filesystem/FilesystemAdapterTest.php index 2d5ca5941241..f125edbe74ba 100644 --- a/tests/Filesystem/FilesystemAdapterTest.php +++ b/tests/Filesystem/FilesystemAdapterTest.php @@ -398,7 +398,7 @@ public function testTemporaryUrlWithCustomCallback() public function testThrowExceptionForGet() { - $adapter = new FilesystemAdapter($this->filesystem, $this->adapter, ['throws_exceptions' => true]); + $adapter = new FilesystemAdapter($this->filesystem, $this->adapter, ['throw' => true]); try { $adapter->get('/foo.txt'); @@ -413,7 +413,7 @@ public function testThrowExceptionForGet() public function testThrowExceptionsForReadStream() { - $adapter = new FilesystemAdapter($this->filesystem, $this->adapter, ['throws_exceptions' => true]); + $adapter = new FilesystemAdapter($this->filesystem, $this->adapter, ['throw' => true]); try { $adapter->readStream('/foo.txt'); @@ -431,7 +431,7 @@ public function testThrowExceptionsForPut() { mkdir(__DIR__.'/tmp/bar', 0600); - $adapter = new FilesystemAdapter($this->filesystem, $this->adapter, ['throws_exceptions' => true]); + $adapter = new FilesystemAdapter($this->filesystem, $this->adapter, ['throw' => true]); try { $adapter->put('/bar/foo.txt', 'Hello World!'); From dc22a28cf6266e96cf75577ec342f66e4900d2f7 Mon Sep 17 00:00:00 2001 From: Dries Vints Date: Thu, 3 Mar 2022 15:41:47 +0100 Subject: [PATCH 6/7] wip --- tests/Filesystem/FilesystemAdapterTest.php | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/tests/Filesystem/FilesystemAdapterTest.php b/tests/Filesystem/FilesystemAdapterTest.php index f125edbe74ba..e8969213e85a 100644 --- a/tests/Filesystem/FilesystemAdapterTest.php +++ b/tests/Filesystem/FilesystemAdapterTest.php @@ -408,7 +408,7 @@ public function testThrowExceptionForGet() return; } - $this->fail('Exception was not thrown.'); + $this->fail('UnableToReadFile exception was not thrown.'); } public function testThrowExceptionsForReadStream() @@ -423,24 +423,25 @@ public function testThrowExceptionsForReadStream() return; } - $this->fail('Exception was not thrown.'); + $this->fail('UnableToReadFile exception was not thrown.'); } - /** @requires OS Linux|Darwin */ public function testThrowExceptionsForPut() { - mkdir(__DIR__.'/tmp/bar', 0600); + $this->filesystem->write('foo.txt', 'Hello World'); - $adapter = new FilesystemAdapter($this->filesystem, $this->adapter, ['throw' => true]); + chmod(__DIR__.'/tmp/foo.txt', 0400); + + $adapter = new FilesystemAdapter($this->filesystem, $this->adapter, ['throws_exceptions' => true]); try { - $adapter->put('/bar/foo.txt', 'Hello World!'); + $adapter->put('foo.txt', 'Hello World!'); + + $this->fail('UnableToWriteFile exception was not thrown.'); } catch (UnableToWriteFile $e) { $this->assertTrue(true); - - return; + } finally { + chmod(__DIR__.'/tmp/foo.txt', 0600); } - - $this->fail('Exception was not thrown.'); } } From 33749abe72fec7e1ee1fefb47dc6f23f871a3003 Mon Sep 17 00:00:00 2001 From: Dries Vints Date: Thu, 3 Mar 2022 15:49:03 +0100 Subject: [PATCH 7/7] Revert "wip" This reverts commit dc22a28cf6266e96cf75577ec342f66e4900d2f7. --- tests/Filesystem/FilesystemAdapterTest.php | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/tests/Filesystem/FilesystemAdapterTest.php b/tests/Filesystem/FilesystemAdapterTest.php index e8969213e85a..f125edbe74ba 100644 --- a/tests/Filesystem/FilesystemAdapterTest.php +++ b/tests/Filesystem/FilesystemAdapterTest.php @@ -408,7 +408,7 @@ public function testThrowExceptionForGet() return; } - $this->fail('UnableToReadFile exception was not thrown.'); + $this->fail('Exception was not thrown.'); } public function testThrowExceptionsForReadStream() @@ -423,25 +423,24 @@ public function testThrowExceptionsForReadStream() return; } - $this->fail('UnableToReadFile exception was not thrown.'); + $this->fail('Exception was not thrown.'); } + /** @requires OS Linux|Darwin */ public function testThrowExceptionsForPut() { - $this->filesystem->write('foo.txt', 'Hello World'); - - chmod(__DIR__.'/tmp/foo.txt', 0400); + mkdir(__DIR__.'/tmp/bar', 0600); - $adapter = new FilesystemAdapter($this->filesystem, $this->adapter, ['throws_exceptions' => true]); + $adapter = new FilesystemAdapter($this->filesystem, $this->adapter, ['throw' => true]); try { - $adapter->put('foo.txt', 'Hello World!'); - - $this->fail('UnableToWriteFile exception was not thrown.'); + $adapter->put('/bar/foo.txt', 'Hello World!'); } catch (UnableToWriteFile $e) { $this->assertTrue(true); - } finally { - chmod(__DIR__.'/tmp/foo.txt', 0600); + + return; } + + $this->fail('Exception was not thrown.'); } }