sidebar
This commit is contained in:
@@ -0,0 +1,9 @@
|
||||
<?php
|
||||
|
||||
namespace Lucent\Schema\Sidebar\Item;
|
||||
|
||||
|
||||
abstract class Item
|
||||
{
|
||||
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
namespace Lucent\Schema\Sidebar\Item;
|
||||
|
||||
class LinkItem extends Item
|
||||
{
|
||||
public function __construct(
|
||||
public string $label,
|
||||
public string $href,
|
||||
)
|
||||
{
|
||||
}
|
||||
|
||||
public static function fromArray(array $data): self
|
||||
{
|
||||
return new self(
|
||||
label: $data["label"],
|
||||
href: $data["href"],
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
namespace Lucent\Schema\Sidebar\Item;
|
||||
|
||||
class SchemaItem extends Item
|
||||
{
|
||||
public function __construct(
|
||||
public string $label,
|
||||
public string $schema,
|
||||
)
|
||||
{
|
||||
}
|
||||
|
||||
public static function fromArray(array $data): self
|
||||
{
|
||||
return new self(
|
||||
label: $data["label"],
|
||||
schema: $data["schema"],
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace Lucent\Schema\Sidebar;
|
||||
|
||||
use Lucent\Primitive\Collection;
|
||||
use Lucent\Schema\Sidebar\Item\Item;
|
||||
use Lucent\Schema\Sidebar\Item\LinkItem;
|
||||
use Lucent\Schema\Sidebar\Item\SchemaItem;
|
||||
|
||||
class Section
|
||||
{
|
||||
/**
|
||||
* @param Collection<Item> $items
|
||||
*/
|
||||
public function __construct(
|
||||
public string $label,
|
||||
public Collection $items
|
||||
)
|
||||
{
|
||||
}
|
||||
|
||||
public static function fromArray(array $data): self
|
||||
{
|
||||
return new self(
|
||||
label: $data["label"],
|
||||
items: (new Collection($data["items"]))->map(fn($itemData) => isset($itemData["schema"]) ? SchemaItem::fromArray($itemData) : LinkItem::fromArray($itemData))
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace Lucent\Schema\Sidebar;
|
||||
|
||||
use Lucent\Primitive\Collection;
|
||||
|
||||
class Sidebar
|
||||
{
|
||||
/**
|
||||
* @param Collection<Section> $sections
|
||||
*/
|
||||
public function __construct(
|
||||
public Collection $sections
|
||||
)
|
||||
{
|
||||
}
|
||||
|
||||
public static function fromArray(array $data): self
|
||||
{
|
||||
return new self((new Collection($data))->map(fn($sectionData) => Section::fromArray($sectionData)));
|
||||
}
|
||||
|
||||
public function getUsedSchemas(): array
|
||||
{
|
||||
return $this->sections->reduce(function (array $carry, Section $section) {
|
||||
return array_merge($carry, $section->items->pluck("schema")->toArray());
|
||||
}, []);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
namespace Lucent\Schema\Sidebar;
|
||||
|
||||
|
||||
use Lucent\Channel\Channel;
|
||||
use PhpOption\Option;
|
||||
use Symfony\Component\Yaml\Yaml;
|
||||
|
||||
class SidebarService
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Channel $channel
|
||||
* @param array<string> $readableSchemas
|
||||
* @param string $currentSchema
|
||||
* @return string
|
||||
*/
|
||||
public function render(Channel $channel, array $readableSchemas, string $currentSchema): string
|
||||
{
|
||||
$schemas = $channel->schemas->whereIn("name", $readableSchemas);
|
||||
|
||||
|
||||
return view('lucent::sidebar.sidebar', [
|
||||
'sidebar' => $this->fromConfig(),
|
||||
'schemas' => $schemas,
|
||||
'channel' => $channel,
|
||||
'currentSchema' => $currentSchema,
|
||||
'lucentUrl' => $channel->lucentUrl,
|
||||
])->render();
|
||||
}
|
||||
|
||||
/**
|
||||
* @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);
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user