1
0

Provides a way to list bundles

This commit is contained in:
Richard Dern 2024-04-18 00:43:44 +02:00
parent b14be6c70b
commit 5b9f4eeead

View File

@ -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;
}
}
}