From 5b9f4eeeadcee872a4dc4e6d53334a007b5e7ed5 Mon Sep 17 00:00:00 2001 From: Richard Dern Date: Thu, 18 Apr 2024 00:43:44 +0200 Subject: [PATCH] Provides a way to list bundles --- app/Classes/Bundle.php | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/app/Classes/Bundle.php b/app/Classes/Bundle.php index 7c2fd10..5919268 100644 --- a/app/Classes/Bundle.php +++ b/app/Classes/Bundle.php @@ -7,6 +7,7 @@ use App\Classes\Traits\ManagesMetadata; use Illuminate\Filesystem\FilesystemAdapter; use Illuminate\Support\Str; +use League\Flysystem\StorageAttributes; class Bundle { @@ -14,6 +15,11 @@ class Bundle protected string $dataDir; + public function getPath() + { + return $this->path; + } + public function __construct(protected string $path, protected FilesystemAdapter $disk) { $this->path = Str::start(Str::finish($this->path, '/'), '/'); @@ -94,4 +100,28 @@ public function exists() { return $this->markdown()->exists(); } + + public static function findBundles(FilesystemAdapter $disk, ?string $path = '/', bool $recursive = false) + { + if ($recursive) { + return $disk + ->listContents($path, $recursive) + ->filter(fn (StorageAttributes $attributes) => ($attributes->isFile() && Str::endsWith($attributes->path(), '.md'))) + ->map(fn (StorageAttributes $attributes) => new Bundle(dirname($attributes->path()), $disk)) + ->toArray(); + } else { + $bundles = []; + $directories = $disk->directories($path); + + foreach ($directories as $directory) { + $bundle = new Bundle($directory, $disk); + + if ($bundle->exists()) { + $bundles[] = $bundle; + } + } + + return $bundles; + } + } }