Skip to content

Commit

Permalink
Issue #1 Laravel 8/Flysystem 3 support
Browse files Browse the repository at this point in the history
  • Loading branch information
judgej committed May 30, 2022
1 parent 65eea40 commit 4e639c6
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 16 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
],
"minimum-stability": "dev",
"require": {
"illuminate/support": "~5.6|^6.0|^7.0|^8.0",
"php": "^7.3|^8.0"
"illuminate/support": "^9.0",
"php": "^8.0"
},
"require-dev": {
},
Expand Down
35 changes: 21 additions & 14 deletions src/Console/Commands/ListStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,14 @@
namespace Consilience\Laravel\Ls\Console\Commands;

/**
*
* This command uses the Flysystem driver methods rather than the
* laravel wrapper methods. It makes sense to change this, for example
* to use $disk::directories() rather than $disk::listContents(), as
* it provides better laravel version consistency. For now it is
* what it is, and the Laravel 9 version is a breaking change due to
* using Flysystem 3.0 only methods. The main reason for this is the
* lack of caching for the laravel wrapper, with multiple returns to
* the storage to fetch each item of metadata.
*/

use Illuminate\Console\Command;
Expand Down Expand Up @@ -50,7 +57,7 @@ public function handle()
$selectedDisk = $this->option('disk') ?? '';
$defaultDisk = config('filesystems.default');

if ($selectedDisk === '' && strpos($selectedDir, ':') !== false) {
if ($selectedDisk === '' && strpos($selectedDir, ':') !== false) {
// User may be using the "disk:directory" format.

[$diskSplit, $dirSplit] = explode(':', $selectedDir);
Expand Down Expand Up @@ -126,31 +133,31 @@ protected function listDirectory(
$dt = new DateTimeImmutable();

foreach ($content as $item) {
$basename = $item['basename'] ?? 'unknown';
$dirname = $item['dirname'] ?? '/';
$basename = basename($item->path()) ?? 'unknown';
$dirname = dirname($item->path()) ?? '/';

$pathname = $dirname . '/' . $basename;

$size = $item['size'] ?? 0;

$type = $item['type'] ?? static::TYPE_FILE;
$size = $item['fileSize'] ?? 0;

// Some drivers do not supply the file size by default,
// so make another call to get it.

if ($size === 0 && $type === static::TYPE_FILE && $longFormat) {
if ($size === 0 && $item->isFile() && $longFormat) {

try {
$size = Storage::disk($disk)->getSize($pathname);
$size = Storage::disk($disk)->fileSize($pathname);

} catch (Throwable $e) {
// Some drivers throw exceptions in some circumstances.
// We just catch and ignore.
}
}

// Format the timestamp if present.
// Just going down the seconds for now, and UTC is implied.
// Just going down to seconds for now, and UTC is implied.

$timestamp = $item['timestamp'] ?? null;
$timestamp = $item['lastModified'] ?? null;

if ($timestamp !== null) {
$datetime = $dt
Expand All @@ -166,15 +173,15 @@ protected function listDirectory(
if ($longFormat) {
$this->line(sprintf(
'%1s %10d %s %s',
$type === static::TYPE_DIR ? 'd' : '-',
$item->isDir() ? 'd' : '-',
$size,
$datetime,
$basename
));
} else {
$message = sprintf('%s', $basename);

if ($type === static::TYPE_FILE) {
if ($item->isFile()) {
$this->info($message);
} else {
$this->warn($message);
Expand All @@ -183,7 +190,7 @@ protected function listDirectory(

// Collect the list of sub-directories as we go through.

if ($recursive && $type === static::TYPE_DIR) {
if ($recursive && $item->isDir()) {
$subDirs[] = $pathname;
}
}
Expand Down

0 comments on commit 4e639c6

Please sign in to comment.