2024-04-17 11:41:10 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Classes;
|
|
|
|
|
2024-04-17 15:29:12 +02:00
|
|
|
use App\Classes\Traits\ManagesAttachments;
|
2024-04-17 11:41:10 +02:00
|
|
|
use App\Classes\Traits\ManagesMarkdown;
|
|
|
|
use App\Classes\Traits\ManagesMetadata;
|
|
|
|
use Illuminate\Filesystem\FilesystemAdapter;
|
|
|
|
use Illuminate\Support\Str;
|
2024-04-18 00:43:44 +02:00
|
|
|
use League\Flysystem\StorageAttributes;
|
2024-04-17 11:41:10 +02:00
|
|
|
|
|
|
|
class Bundle
|
|
|
|
{
|
2024-04-17 15:29:12 +02:00
|
|
|
use ManagesAttachments, ManagesMarkdown, ManagesMetadata;
|
2024-04-17 11:41:10 +02:00
|
|
|
|
2024-04-17 16:54:37 +02:00
|
|
|
protected string $dataDir;
|
2024-04-17 16:18:39 +02:00
|
|
|
|
2024-04-18 00:43:44 +02:00
|
|
|
public function getPath()
|
|
|
|
{
|
|
|
|
return $this->path;
|
|
|
|
}
|
|
|
|
|
2024-04-17 11:41:10 +02:00
|
|
|
public function __construct(protected string $path, protected FilesystemAdapter $disk)
|
|
|
|
{
|
2024-04-17 16:54:37 +02:00
|
|
|
$this->path = Str::start(Str::finish($this->path, '/'), '/');
|
|
|
|
$this->dataDir = $this->path . 'data/';
|
2024-04-18 00:24:45 +02:00
|
|
|
|
|
|
|
$this->registerDefaultManagers();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Load everything
|
|
|
|
*/
|
|
|
|
public function load()
|
|
|
|
{
|
|
|
|
$this->loadAttachments();
|
|
|
|
$this->loadMetadata();
|
|
|
|
$this->loadMarkdown();
|
2024-04-17 11:41:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Store all files of the bundle
|
|
|
|
*/
|
|
|
|
public function save()
|
|
|
|
{
|
2024-04-17 15:29:12 +02:00
|
|
|
$this->saveAttachments();
|
2024-04-17 11:41:10 +02:00
|
|
|
$this->saveMetadata();
|
|
|
|
$this->saveMarkdown();
|
|
|
|
}
|
|
|
|
|
2024-04-18 00:24:45 +02:00
|
|
|
/**
|
|
|
|
* Register default managers
|
|
|
|
*/
|
|
|
|
private function registerDefaultManagers()
|
|
|
|
{
|
|
|
|
$this->markdown();
|
|
|
|
$this->metadata();
|
|
|
|
$this->metadata('metadata');
|
|
|
|
$this->attachments(AttachmentsManager::Images);
|
|
|
|
$this->attachments(AttachmentsManager::Sounds);
|
|
|
|
$this->attachments(AttachmentsManager::Videos);
|
|
|
|
}
|
|
|
|
|
2024-04-17 11:41:10 +02:00
|
|
|
/**
|
2024-04-17 16:54:37 +02:00
|
|
|
* Get a complete filename prefixed with bundle's path
|
2024-04-17 11:41:10 +02:00
|
|
|
*/
|
|
|
|
private function getFilenameInBundle(string $filename, ?string $extension = null)
|
|
|
|
{
|
2024-04-17 16:54:37 +02:00
|
|
|
return $this->getFullpath($this->path, $filename, $extension);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a complete filename prefixed with bundle's data dir
|
|
|
|
*/
|
|
|
|
private function getFilenameInDataBundle(string $filename, ?string $extension = null)
|
|
|
|
{
|
|
|
|
return $this->getFullpath($this->dataDir, $filename, $extension);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return full path of specified filename in specified root directory, and
|
|
|
|
* optionally add specified extension
|
|
|
|
*/
|
|
|
|
private function getFullpath(string $root, string $filename, ?string $extension = null)
|
|
|
|
{
|
|
|
|
$filename = Str::remove($root, $filename);
|
2024-04-17 11:41:10 +02:00
|
|
|
|
|
|
|
if (!empty($extension) && !Str::endsWith($filename, $extension)) {
|
|
|
|
$filename .= $extension;
|
|
|
|
}
|
|
|
|
|
2024-04-17 16:54:37 +02:00
|
|
|
return sprintf('%s%s', $root, $filename);
|
2024-04-17 11:41:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a boolean value indicating if there already is a bundle in
|
|
|
|
* specified path
|
|
|
|
*/
|
|
|
|
public function exists()
|
|
|
|
{
|
|
|
|
return $this->markdown()->exists();
|
|
|
|
}
|
2024-04-18 00:43:44 +02:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2024-04-17 11:41:10 +02:00
|
|
|
}
|