From 4e639c65173d995dd556b25b4bfa2151be69ae60 Mon Sep 17 00:00:00 2001 From: Jason Judge Date: Mon, 30 May 2022 18:24:49 +0100 Subject: [PATCH] Issue #1 Laravel 8/Flysystem 3 support --- composer.json | 4 ++-- src/Console/Commands/ListStorage.php | 35 +++++++++++++++++----------- 2 files changed, 23 insertions(+), 16 deletions(-) diff --git a/composer.json b/composer.json index f1cefde..694b2a5 100644 --- a/composer.json +++ b/composer.json @@ -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": { }, diff --git a/src/Console/Commands/ListStorage.php b/src/Console/Commands/ListStorage.php index 002c287..83b916e 100644 --- a/src/Console/Commands/ListStorage.php +++ b/src/Console/Commands/ListStorage.php @@ -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; @@ -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); @@ -126,21 +133,21 @@ 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. @@ -148,9 +155,9 @@ protected function listDirectory( } // 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 @@ -166,7 +173,7 @@ protected function listDirectory( if ($longFormat) { $this->line(sprintf( '%1s %10d %s %s', - $type === static::TYPE_DIR ? 'd' : '-', + $item->isDir() ? 'd' : '-', $size, $datetime, $basename @@ -174,7 +181,7 @@ protected function listDirectory( } else { $message = sprintf('%s', $basename); - if ($type === static::TYPE_FILE) { + if ($item->isFile()) { $this->info($message); } else { $this->warn($message); @@ -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; } }