2024-03-19 23:05:57 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Lucent\Schema\Sidebar;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
use Lucent\Channel\Channel;
|
2024-03-20 01:57:17 +02:00
|
|
|
use Lucent\Channel\ChannelService;
|
|
|
|
|
use Lucent\Primitive\Collection;
|
|
|
|
|
use Lucent\Schema\CollectionSchema;
|
|
|
|
|
use Lucent\Schema\FilesSchema;
|
|
|
|
|
use Lucent\Schema\Schema;
|
|
|
|
|
use Lucent\Schema\Sidebar\Item\Item;
|
|
|
|
|
use Lucent\Schema\Sidebar\Item\LinkItem;
|
|
|
|
|
use Lucent\Schema\Sidebar\Item\SchemaItem;
|
|
|
|
|
use Lucent\Schema\Sidebar\View\ItemData;
|
|
|
|
|
use Lucent\Schema\Sidebar\View\SectionData;
|
2024-03-19 23:05:57 +02:00
|
|
|
use PhpOption\Option;
|
|
|
|
|
use Symfony\Component\Yaml\Yaml;
|
|
|
|
|
|
|
|
|
|
class SidebarService
|
|
|
|
|
{
|
2024-03-20 01:57:17 +02:00
|
|
|
public function __construct(
|
|
|
|
|
private ChannelService $channelService,
|
|
|
|
|
)
|
2024-03-19 23:05:57 +02:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param array<string> $readableSchemas
|
|
|
|
|
* @param string $currentSchema
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
2024-03-20 01:57:17 +02:00
|
|
|
public function render(array $readableSchemas, string $currentSchema): string
|
2024-03-19 23:05:57 +02:00
|
|
|
{
|
2024-03-20 01:57:17 +02:00
|
|
|
$schemas = $this->channelService->channel->schemas->whereIn("name", $readableSchemas);
|
|
|
|
|
$sidebar = $this->fromConfig();
|
|
|
|
|
$sections = [];
|
|
|
|
|
$usedSchemaNames = [];
|
2024-03-19 23:05:57 +02:00
|
|
|
|
2024-03-20 01:57:17 +02:00
|
|
|
if ($sidebar->isDefined()) {
|
|
|
|
|
$sections = $sidebar->get()->sections->map(function (Section $section) use ($currentSchema): SectionData {
|
|
|
|
|
return new SectionData(
|
|
|
|
|
label: $section->label,
|
|
|
|
|
isOpen: $section->items->where("schema", $currentSchema)->isNotEmpty(),
|
|
|
|
|
items: array_map(fn($item) => $this->renderItem($currentSchema, $item), $section->items->toArray())
|
|
|
|
|
);
|
|
|
|
|
})->toArray();
|
|
|
|
|
|
|
|
|
|
$usedSchemaNames =$sidebar->get()->sections->reduce(fn(array $carry, Section $section) => array_merge($carry, $section->items->pluck("schema")->toArray()), []);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$sections[] = new SectionData(
|
|
|
|
|
label: $sidebar->isDefined() ? "Rest" : "Content",
|
|
|
|
|
isOpen: $schemas->whereNotIn("name",$usedSchemaNames)->where("name",$currentSchema)->isNotEmpty(),
|
|
|
|
|
items: array_map(fn($item) => $this->renderItem($currentSchema, $item), $schemas->whereNotIn("name",$usedSchemaNames)->toArray())
|
|
|
|
|
);
|
2024-03-19 23:05:57 +02:00
|
|
|
|
|
|
|
|
return view('lucent::sidebar.sidebar', [
|
2024-03-20 01:57:17 +02:00
|
|
|
'channel' => $this->channelService->channel,
|
|
|
|
|
'sections' => $sections,
|
2024-03-19 23:05:57 +02:00
|
|
|
])->render();
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-20 01:57:17 +02:00
|
|
|
private function renderItem(string $currentSchema, Item|Schema $item): ItemData
|
|
|
|
|
{
|
|
|
|
|
return match (get_class($item)) {
|
|
|
|
|
LinkItem::class => new ItemData($item->label, $item->href, false, true),
|
|
|
|
|
SchemaItem::class => new ItemData($item->label, $this->channelService->channel->lucentUrl . "/content/" . $item->schema, $currentSchema === $item->schema, false),
|
|
|
|
|
CollectionSchema::class => new ItemData($item->label, $this->channelService->channel->lucentUrl . "/content/" . $item->name, $currentSchema === $item->name, false),
|
|
|
|
|
FilesSchema::class => new ItemData($item->label, $this->channelService->channel->lucentUrl . "/content/" . $item->name, $currentSchema === $item->name, false),
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-19 23:05:57 +02:00
|
|
|
/**
|
|
|
|
|
* @return Option<Sidebar>
|
|
|
|
|
*/
|
|
|
|
|
private function fromConfig(): Option
|
|
|
|
|
{
|
|
|
|
|
$configPath = base_path(config("lucent.sidebar_path")) . "/sidebar.yaml";
|
|
|
|
|
|
|
|
|
|
if (!file_exists($configPath)) {
|
|
|
|
|
return none();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$yaml = Yaml::parse(file_get_contents($configPath));
|
|
|
|
|
|
|
|
|
|
if (empty($yaml)) {
|
|
|
|
|
return none();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$sidebar = Sidebar::fromArray($yaml);
|
|
|
|
|
return some($sidebar);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|