-
Notifications
You must be signed in to change notification settings - Fork 356
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
StorageFailure and FileNotFound exceptions for Flysystem adapter #519
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,8 +3,11 @@ | |
namespace Gaufrette\Adapter; | ||
|
||
use Gaufrette\Adapter; | ||
use Gaufrette\Exception\FileNotFound; | ||
use Gaufrette\Exception\StorageFailure; | ||
use Gaufrette\Exception\UnsupportedAdapterMethodException; | ||
use League\Flysystem\AdapterInterface; | ||
use League\Flysystem\FileNotFoundException; | ||
use League\Flysystem\Util; | ||
|
||
class Flysystem implements Adapter, ListKeysAware | ||
|
@@ -34,33 +37,70 @@ public function __construct(AdapterInterface $adapter, $config = null) | |
*/ | ||
public function read($key) | ||
{ | ||
return $this->adapter->read($key)['contents']; | ||
try { | ||
$result = $this->adapter->read($key); | ||
} catch (\Exception $e) { | ||
if ($e instanceof FileNotFoundException) { | ||
throw new FileNotFound($key, $e->getCode(), $e); | ||
} | ||
|
||
throw StorageFailure::unexpectedFailure('read', ['key' => $key], $e); | ||
} | ||
|
||
if (false === $result) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So Flysystem adapters are less stricter than us? Funny :) |
||
throw StorageFailure::unexpectedFailure('read', ['key' => $key]); | ||
} | ||
|
||
return $result['contents']; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function write($key, $content) | ||
{ | ||
return $this->adapter->write($key, $content, $this->config); | ||
try { | ||
$result = $this->adapter->write($key, $content, $this->config); | ||
} catch (\Exception $e) { | ||
throw StorageFailure::unexpectedFailure( | ||
'write', | ||
['key' => $key, 'content' => $content], | ||
$e | ||
); | ||
} | ||
|
||
if (false === $result) { | ||
throw StorageFailure::unexpectedFailure( | ||
'write', | ||
['key' => $key, 'content' => $content] | ||
); | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function exists($key) | ||
{ | ||
return $this->adapter->has($key); | ||
try { | ||
return $this->adapter->has($key); | ||
} catch (\Exception $e) { | ||
throw StorageFailure::unexpectedFailure('exists', ['key' => $key], $e); | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function keys() | ||
{ | ||
return array_map(function ($content) { | ||
return $content['path']; | ||
}, $this->adapter->listContents()); | ||
try { | ||
return array_map(function ($content) { | ||
return $content['path']; | ||
}, $this->adapter->listContents()); | ||
} catch (\Exception $e) { | ||
throw StorageFailure::unexpectedFailure('keys', [], $e); | ||
} | ||
} | ||
|
||
/** | ||
|
@@ -71,14 +111,22 @@ public function listKeys($prefix = '') | |
$dirs = []; | ||
$keys = []; | ||
|
||
foreach ($this->adapter->listContents() as $content) { | ||
if (empty($prefix) || 0 === strpos($content['path'], $prefix)) { | ||
if ('dir' === $content['type']) { | ||
$dirs[] = $content['path']; | ||
} else { | ||
$keys[] = $content['path']; | ||
try { | ||
foreach ($this->adapter->listContents() as $content) { | ||
if (empty($prefix) || 0 === strpos($content['path'], $prefix)) { | ||
if ('dir' === $content['type']) { | ||
$dirs[] = $content['path']; | ||
} else { | ||
$keys[] = $content['path']; | ||
} | ||
} | ||
} | ||
} catch (\Exception $e) { | ||
throw StorageFailure::unexpectedFailure( | ||
'listKeys', | ||
['prefix' => $prefix], | ||
$e | ||
); | ||
} | ||
|
||
return [ | ||
|
@@ -92,23 +140,68 @@ public function listKeys($prefix = '') | |
*/ | ||
public function mtime($key) | ||
{ | ||
return $this->adapter->getTimestamp($key); | ||
try { | ||
$result = $this->adapter->getTimestamp($key); | ||
} catch (\Exception $e) { | ||
if ($e instanceof FileNotFoundException) { | ||
throw new FileNotFound($key, $e->getCode(), $e); | ||
} | ||
|
||
throw StorageFailure::unexpectedFailure('mtime', ['key' => $key], $e); | ||
} | ||
|
||
if (false === $result) { | ||
throw StorageFailure::unexpectedFailure('mtime', ['key' => $key]); | ||
} | ||
|
||
return $result; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function delete($key) | ||
{ | ||
return $this->adapter->delete($key); | ||
try { | ||
$result = $this->adapter->delete($key); | ||
} catch (\Exception $e) { | ||
if ($e instanceof FileNotFoundException) { | ||
throw new FileNotFound($key, $e->getCode(), $e); | ||
} | ||
|
||
throw StorageFailure::unexpectedFailure('delete', ['key' => $key], $e); | ||
} | ||
|
||
if (false === $result) { | ||
throw StorageFailure::unexpectedFailure('delete', ['key' => $key]); | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function rename($sourceKey, $targetKey) | ||
{ | ||
return $this->adapter->rename($sourceKey, $targetKey); | ||
try { | ||
$result = $this->adapter->rename($sourceKey, $targetKey); | ||
} catch (\Exception $e) { | ||
if ($e instanceof FileNotFoundException) { | ||
throw new FileNotFound($sourceKey, $e->getCode(), $e); | ||
} | ||
|
||
throw StorageFailure::unexpectedFailure( | ||
'rename', | ||
['sourceKey' => $sourceKey, 'targetKey' => $targetKey], | ||
$e | ||
); | ||
} | ||
|
||
if (false === $result) { | ||
throw StorageFailure::unexpectedFailure( | ||
'rename', | ||
['sourceKey' => $sourceKey, 'targetKey' => $targetKey] | ||
); | ||
} | ||
} | ||
|
||
/** | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the lower "r" a mistake or is it due to the pretty old version of phpspec?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks surprising but yes, it's because of an old version of phpspec (2.4). BTW I ran into issues when trying to run the phpspec tests : the
phpspec.yml
file has a dependency onAkeneo\SkipExampleExtension
but I couldn't get it installed via composer (unresolvable set of packages). So I had to comment that file to run the tests on my machine 😕 . I'm still wondering how the tests can be executed on the CI .It would be a good point to bump up the tests tools to their latest version 😉
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mh, that's strange! The
akeneo/phpspec-skip-example-extension
package is in our composer.json for some time now. Was there any error message/stacktrace?Yep, I already upgraded phpunit to 5.7 and there's a task about upgrading phpspec in the github project ;)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was no error during
composer install
, but the akeneo directory was not present in thevendor
. However, when trying to manually install the package, I got the following error :I haven't noticed the phpspec upgrade card, I'll try to give it a look 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Have you try to delete your local
composer.lock
first?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shame on me 🔨 removing the
composer.lock
file and runningcomposer install
again did the trick :) Thank you 👍