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;
|
2024-04-20 23:27:47 +02:00
|
|
|
use App\Contracts\Bundles;
|
|
|
|
use App\Services\BundleRenderers\Facades\BundleRenderer;
|
2024-04-25 00:57:06 +02:00
|
|
|
use Carbon\Carbon;
|
2024-04-20 23:27:47 +02:00
|
|
|
use Exception;
|
2024-04-17 11:41:10 +02:00
|
|
|
use Illuminate\Filesystem\FilesystemAdapter;
|
2024-04-25 17:11:12 +02:00
|
|
|
use Illuminate\Support\Collection;
|
2024-04-17 11:41:10 +02:00
|
|
|
use Illuminate\Support\Str;
|
2024-04-18 00:43:44 +02:00
|
|
|
use League\Flysystem\StorageAttributes;
|
2024-04-17 11:41:10 +02:00
|
|
|
|
2024-04-20 23:27:47 +02:00
|
|
|
class Bundle implements Bundles
|
2024-04-17 11:41:10 +02:00
|
|
|
{
|
2024-04-20 23:27:47 +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-20 23:27:47 +02:00
|
|
|
protected int $currentPage = 1;
|
2024-04-18 00:43:44 +02:00
|
|
|
|
2024-04-24 00:47:46 +02:00
|
|
|
protected ?Bundle $section;
|
|
|
|
|
|
|
|
protected ?Bundle $parent;
|
|
|
|
|
2024-04-25 00:57:06 +02:00
|
|
|
protected ?array $directChildren;
|
|
|
|
|
2024-04-17 11:41:10 +02:00
|
|
|
public function __construct(protected string $path, protected FilesystemAdapter $disk)
|
|
|
|
{
|
2024-04-20 23:27:47 +02:00
|
|
|
$this->path = $this->normalizeBundlePath($path);
|
2024-04-17 16:54:37 +02:00
|
|
|
$this->dataDir = $this->path . 'data/';
|
2024-04-18 00:24:45 +02:00
|
|
|
|
|
|
|
$this->registerDefaultManagers();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-04-20 23:27:47 +02:00
|
|
|
* Return current page number
|
2024-04-18 00:24:45 +02:00
|
|
|
*/
|
2024-04-20 23:27:47 +02:00
|
|
|
public function getCurrentPage(): int
|
2024-04-18 00:24:45 +02:00
|
|
|
{
|
2024-04-20 23:27:47 +02:00
|
|
|
return $this->currentPage;
|
2024-04-17 11:41:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-04-20 23:27:47 +02:00
|
|
|
* Return bundle's path
|
2024-04-17 11:41:10 +02:00
|
|
|
*/
|
2024-04-20 23:27:47 +02:00
|
|
|
public function getPath(): string
|
2024-04-17 11:41:10 +02:00
|
|
|
{
|
2024-04-20 23:27:47 +02:00
|
|
|
return $this->path;
|
2024-04-17 11:41:10 +02:00
|
|
|
}
|
|
|
|
|
2024-04-18 00:24:45 +02:00
|
|
|
/**
|
2024-04-20 23:27:47 +02:00
|
|
|
* Return bundle's data dir
|
2024-04-18 00:24:45 +02:00
|
|
|
*/
|
2024-04-20 23:27:47 +02:00
|
|
|
public function getDataDir(): string
|
2024-04-18 00:24:45 +02:00
|
|
|
{
|
2024-04-20 23:27:47 +02:00
|
|
|
return $this->dataDir;
|
2024-04-18 00:24:45 +02:00
|
|
|
}
|
|
|
|
|
2024-04-17 11:41:10 +02:00
|
|
|
/**
|
2024-04-20 23:27:47 +02:00
|
|
|
* Return bundle's filesystem instance
|
2024-04-17 11:41:10 +02:00
|
|
|
*/
|
2024-04-20 23:27:47 +02:00
|
|
|
public function getDisk(): FilesystemAdapter
|
2024-04-17 11:41:10 +02:00
|
|
|
{
|
2024-04-20 23:27:47 +02:00
|
|
|
return $this->disk;
|
2024-04-17 16:54:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-04-20 23:27:47 +02:00
|
|
|
* Return the section where this bundle is located
|
2024-04-17 16:54:37 +02:00
|
|
|
*/
|
2024-04-21 16:46:27 +02:00
|
|
|
public function getSection(): ?Bundle
|
2024-04-17 16:54:37 +02:00
|
|
|
{
|
2024-04-24 00:47:46 +02:00
|
|
|
if (!isset($this->section)) {
|
|
|
|
$parts = preg_split('#/#', $this->path, -1, PREG_SPLIT_NO_EMPTY);
|
|
|
|
|
|
|
|
if (count($parts) > 0) {
|
|
|
|
$this->section = new Bundle($parts[0], $this->disk);
|
|
|
|
} else {
|
|
|
|
$this->section = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->section;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the parent bundle where this bundle is located
|
|
|
|
*/
|
|
|
|
public function getParent(): ?Bundle
|
|
|
|
{
|
|
|
|
if (!isset($this->parent)) {
|
2024-04-28 00:27:49 +02:00
|
|
|
$parentPath = dirname($this->getPath());
|
2024-04-20 23:27:47 +02:00
|
|
|
|
2024-04-28 00:27:49 +02:00
|
|
|
if ($parentPath === $this->getPath()) {
|
2024-04-24 00:47:46 +02:00
|
|
|
$this->parent = null;
|
2024-04-28 00:27:49 +02:00
|
|
|
} else {
|
|
|
|
$this->parent = new Bundle($parentPath, $this->getDisk());
|
2024-04-24 00:47:46 +02:00
|
|
|
}
|
2024-04-20 23:27:47 +02:00
|
|
|
}
|
|
|
|
|
2024-04-24 00:47:46 +02:00
|
|
|
return $this->parent;
|
2024-04-17 16:54:37 +02:00
|
|
|
}
|
|
|
|
|
2024-04-25 00:57:06 +02:00
|
|
|
/**
|
|
|
|
* Return an array containing direct children of this bundle
|
|
|
|
*/
|
|
|
|
public function getDirectChildren(): array
|
|
|
|
{
|
|
|
|
if (!isset($this->directChildren)) {
|
|
|
|
$this->directChildren = static::findBundles($this->getDisk(), $this->getPath());
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->directChildren;
|
|
|
|
}
|
|
|
|
|
2024-04-24 14:20:20 +02:00
|
|
|
public function getArticleTitle(): string
|
|
|
|
{
|
|
|
|
return $this->metadata()->get('title') ?? Str::title(basename($this->path));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getSiteTitle(): string
|
|
|
|
{
|
|
|
|
$title = $this->getArticleTitle();
|
|
|
|
$section = $this->getSection();
|
|
|
|
|
|
|
|
if (!empty($section) && $section->getPath() !== $this->getPath()) {
|
|
|
|
$title .= ' - ' . $section->getArticleTitle();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $title;
|
|
|
|
}
|
|
|
|
|
2024-04-25 17:11:12 +02:00
|
|
|
/**
|
|
|
|
* Return "virtual" metadata, which is a Collection of extended metadata,
|
|
|
|
* with additions and corrections from the "metadata.json" file
|
|
|
|
*/
|
|
|
|
public function virtualMetadata(): Collection
|
|
|
|
{
|
|
|
|
$mergedData = [];
|
|
|
|
|
|
|
|
foreach ($this->metadata()->get('virtualMetadata', []) as $additionalFile) {
|
|
|
|
$mergedData = array_merge_recursive($mergedData, $this->metadata($additionalFile)->all() ?? []);
|
|
|
|
}
|
|
|
|
|
2024-04-27 07:40:10 +02:00
|
|
|
$replacedData = array_merge_recursive($mergedData, $this->metadata('metadata')->all() ?? []);
|
2024-04-25 17:11:12 +02:00
|
|
|
|
|
|
|
return collect($replacedData);
|
|
|
|
}
|
|
|
|
|
2024-04-17 16:54:37 +02:00
|
|
|
/**
|
2024-04-20 23:27:47 +02:00
|
|
|
* Return a boolean value indicating if there already is a bundle in
|
|
|
|
* specified path
|
2024-04-17 16:54:37 +02:00
|
|
|
*/
|
2024-04-20 23:27:47 +02:00
|
|
|
public function exists(): bool
|
2024-04-17 16:54:37 +02:00
|
|
|
{
|
2024-04-20 23:27:47 +02:00
|
|
|
return $this->markdown()->exists();
|
|
|
|
}
|
2024-04-17 11:41:10 +02:00
|
|
|
|
2024-04-20 23:27:47 +02:00
|
|
|
/**
|
|
|
|
* Load everything
|
|
|
|
*/
|
|
|
|
public function load(): void
|
|
|
|
{
|
|
|
|
$this->loadAttachments();
|
|
|
|
$this->loadMetadata();
|
|
|
|
$this->loadMarkdown();
|
|
|
|
}
|
2024-04-17 11:41:10 +02:00
|
|
|
|
2024-04-20 23:27:47 +02:00
|
|
|
/**
|
|
|
|
* Store all files of the bundle
|
|
|
|
*/
|
2024-05-05 17:31:32 +02:00
|
|
|
public function save(): bool
|
2024-04-20 23:27:47 +02:00
|
|
|
{
|
2024-05-05 17:16:12 +02:00
|
|
|
$attachmentsSaved = $this->saveAttachments();
|
|
|
|
$metadataSaved = $this->saveMetadata();
|
|
|
|
$markdownSaved = $this->saveMarkdown();
|
|
|
|
|
2024-04-26 23:05:49 +02:00
|
|
|
if (
|
2024-05-05 17:16:12 +02:00
|
|
|
$attachmentsSaved
|
|
|
|
|| $metadataSaved
|
|
|
|
|| $markdownSaved
|
2024-04-26 23:05:49 +02:00
|
|
|
) {
|
2024-04-26 23:26:55 +02:00
|
|
|
$this->touch();
|
2024-05-05 17:31:32 +02:00
|
|
|
|
|
|
|
return true;
|
2024-04-26 23:05:49 +02:00
|
|
|
}
|
2024-05-05 17:31:32 +02:00
|
|
|
|
|
|
|
return false;
|
2024-04-17 11:41:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-04-20 23:27:47 +02:00
|
|
|
* Repair bundle
|
2024-04-17 11:41:10 +02:00
|
|
|
*/
|
2024-05-05 17:31:32 +02:00
|
|
|
public function repair(): bool
|
2024-04-17 11:41:10 +02:00
|
|
|
{
|
2024-04-20 23:27:47 +02:00
|
|
|
$this->load();
|
2024-04-23 11:58:07 +02:00
|
|
|
$this->repairCover();
|
2024-04-20 23:27:47 +02:00
|
|
|
$this->lintMarkdown();
|
|
|
|
$this->repairAttachments();
|
2024-05-05 17:31:32 +02:00
|
|
|
|
|
|
|
return $this->save();
|
2024-04-17 11:41:10 +02:00
|
|
|
}
|
2024-04-18 00:43:44 +02:00
|
|
|
|
2024-04-26 23:26:55 +02:00
|
|
|
public function touch()
|
|
|
|
{
|
|
|
|
$this->metadata()->set('lastModified', now()->toIso8601String());
|
|
|
|
$this->saveMetadata();
|
2024-04-28 00:27:49 +02:00
|
|
|
|
|
|
|
$parent = $this->getParent();
|
|
|
|
|
|
|
|
if (!empty($parent)) {
|
|
|
|
$parent->touch();
|
|
|
|
}
|
2024-04-26 23:26:55 +02:00
|
|
|
}
|
|
|
|
|
2024-05-09 01:10:24 +02:00
|
|
|
public function render()
|
2024-04-20 23:27:47 +02:00
|
|
|
{
|
|
|
|
$renderer = BundleRenderer::getBundleRendererFor($this);
|
2024-04-26 23:05:49 +02:00
|
|
|
$result = $renderer->render();
|
|
|
|
|
|
|
|
$this->metadata()->set('lastRendered', now()->toIso8601String());
|
|
|
|
$this->metadata()->save();
|
|
|
|
|
|
|
|
return $result;
|
2024-04-20 23:27:47 +02:00
|
|
|
}
|
|
|
|
|
2024-04-23 11:58:07 +02:00
|
|
|
public function renderCard()
|
|
|
|
{
|
|
|
|
$renderer = BundleRenderer::getBundleRendererFor($this);
|
|
|
|
|
|
|
|
return $renderer->renderCard();
|
|
|
|
}
|
|
|
|
|
2024-04-20 23:27:47 +02:00
|
|
|
/**
|
|
|
|
* Return a list of bundles in the specified path
|
|
|
|
*/
|
2024-05-09 01:10:24 +02:00
|
|
|
public static function findBundles(FilesystemAdapter $disk, ?string $path = '/', bool $recursive = false, bool $sortByDate = true): array
|
2024-04-18 00:43:44 +02:00
|
|
|
{
|
2024-04-24 00:47:46 +02:00
|
|
|
$bundles = [];
|
|
|
|
|
2024-04-18 00:43:44 +02:00
|
|
|
if ($recursive) {
|
2024-04-24 00:47:46 +02:00
|
|
|
$bundles = $disk
|
2024-04-18 00:43:44 +02:00
|
|
|
->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 {
|
|
|
|
$directories = $disk->directories($path);
|
|
|
|
|
|
|
|
foreach ($directories as $directory) {
|
|
|
|
$bundle = new Bundle($directory, $disk);
|
|
|
|
|
|
|
|
if ($bundle->exists()) {
|
|
|
|
$bundles[] = $bundle;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-04-24 00:47:46 +02:00
|
|
|
|
2024-05-09 01:10:24 +02:00
|
|
|
if ($sortByDate) {
|
|
|
|
$bundles = collect($bundles)
|
|
|
|
->sortBy([
|
|
|
|
fn (Bundle $a, Bundle $b) => Carbon::parse($a->metadata()->get('date'))
|
|
|
|
->gt(Carbon::parse($b->metadata()->get('date'))),
|
|
|
|
fn (Bundle $a, Bundle $b) => Carbon::parse($b->metadata()->get('date'))
|
|
|
|
->gt(Carbon::parse($a->metadata()->get('date'))),
|
|
|
|
])
|
|
|
|
->toArray();
|
|
|
|
}
|
2024-04-25 00:57:06 +02:00
|
|
|
|
2024-04-24 00:47:46 +02:00
|
|
|
return $bundles;
|
2024-04-18 00:43:44 +02:00
|
|
|
}
|
2024-04-20 23:27:47 +02:00
|
|
|
|
2024-04-23 11:58:07 +02:00
|
|
|
private function repairCover()
|
|
|
|
{
|
|
|
|
$cover = $this->metadata()->get('cover');
|
|
|
|
|
|
|
|
if (empty($cover)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_array($cover)) {
|
|
|
|
$path = $cover['url'];
|
|
|
|
|
|
|
|
foreach ($this->attachments(AttachmentsManager::Images)->manager()->get('files') as $ref => $data) {
|
|
|
|
if (Str::endsWith($data['filename'], $path)) {
|
|
|
|
$this->metadata()->set('cover', $ref);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-20 23:27:47 +02:00
|
|
|
/**
|
|
|
|
* Return a normalized representation of bundle's path
|
|
|
|
*/
|
|
|
|
private function normalizeBundlePath(string $path): string
|
|
|
|
{
|
2024-04-26 21:55:05 +02:00
|
|
|
if ($path === './' || $path === '' || $path === '.') {
|
|
|
|
return '/';
|
|
|
|
}
|
|
|
|
|
2024-04-20 23:27:47 +02:00
|
|
|
$parts = preg_split('#/#', $path, -1, PREG_SPLIT_NO_EMPTY);
|
|
|
|
$count = count($parts);
|
|
|
|
|
2024-04-22 21:46:36 +02:00
|
|
|
if ($count >= 2 && $parts[$count - 2] === 'page') {
|
2024-04-20 23:27:47 +02:00
|
|
|
// Requested URL is pagination
|
|
|
|
$page = array_pop($parts);
|
|
|
|
|
|
|
|
if (!is_numeric($page) || $page < 1) {
|
|
|
|
throw new Exception(sprintf('Invalid page number: %s', $page));
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->currentPage = $page;
|
|
|
|
|
|
|
|
// Pop "page" out of the parts
|
|
|
|
array_pop($parts);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Str::start(Str::finish(implode('/', $parts), '/'), '/');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Register default managers
|
|
|
|
*/
|
|
|
|
private function registerDefaultManagers(): void
|
|
|
|
{
|
|
|
|
$this->markdown();
|
|
|
|
$this->metadata();
|
|
|
|
$this->metadata('metadata');
|
|
|
|
$this->attachments(AttachmentsManager::Images);
|
|
|
|
$this->attachments(AttachmentsManager::Sounds);
|
|
|
|
$this->attachments(AttachmentsManager::Videos);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a complete filename prefixed with bundle's path
|
|
|
|
*/
|
|
|
|
private function getFilenameInBundle(string $filename, ?string $extension = null): string
|
|
|
|
{
|
|
|
|
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): string
|
|
|
|
{
|
|
|
|
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): string
|
|
|
|
{
|
|
|
|
$filename = Str::remove($root, $filename);
|
|
|
|
|
|
|
|
if (!empty($extension) && !Str::endsWith($filename, $extension)) {
|
|
|
|
$filename .= $extension;
|
|
|
|
}
|
|
|
|
|
|
|
|
return sprintf('%s%s', $root, $filename);
|
|
|
|
}
|
2024-04-17 11:41:10 +02:00
|
|
|
}
|