This commit is contained in:
2024-03-19 23:05:57 +02:00
parent 137c338719
commit 1f03eebd08
34 changed files with 557 additions and 169 deletions
+16 -8
View File
@@ -2,10 +2,16 @@
namespace Lucent\Schema\BlockUi;
use Lucent\JsonSchema\Property\Property;
use Lucent\JsonSchema\Property\RefProperty;
use Lucent\JsonSchema\Property\TypeProperty;
use Lucent\Primitive\Collection;
use Lucent\Schema\FieldInfo;
use Lucent\Schema\FieldInterface;
use Lucent\Schema\FieldType;
use Lucent\Schema\Type;
use Lucent\Schema\Validator\MinMaxInterface;
use PhpOption\Option;
class File implements FieldInterface, MinMaxInterface
{
@@ -19,9 +25,11 @@ class File implements FieldInterface, MinMaxInterface
public string $name,
public string $label,
public string $mime = "",
public string $help = "",
public ?int $min = null,
public ?int $max = null,
public array $collections = [],
public string $group = "",
)
{
$this->info = new FieldInfo("file", "File", FieldType::FILE);
@@ -50,20 +58,20 @@ class File implements FieldInterface, MinMaxInterface
return count($value) < $this->min;
}
public function isRequired(): bool
{
return false;
}
public function toJsonSchema(): Property
{
return new TypeProperty(
type: PropertyType::STRING,
return new RefProperty(
id: $this->name,
title: Option::fromValue($this->label),
description: Option::fromValue($this->help, ""),
default: Option::fromValue($this->default, ""),
minLength: Option::fromValue($this->min),
maxLength: Option::fromValue($this->max),
readOnly: Option::fromValue($this->readonly, false),
enum: Option::fromValue($this->selectOptions),
_ref: new Collection($this->collections),
minItems: Option::fromValue($this->min),
maxItems: Option::fromValue($this->max),
comment: Option::fromValue($this->group, ""),
format: none(),
);
}
+4
View File
@@ -29,5 +29,9 @@ class Heading implements FieldInterface
return $output;
}
public function isRequired(): bool
{
return false;
}
}
+4 -1
View File
@@ -28,6 +28,9 @@ class Markdown implements FieldInterface
$output[$this->name] = $input[$this->name] ?? "";
return $output;
}
public function isRequired(): bool
{
return false;
}
}
+4 -1
View File
@@ -49,6 +49,9 @@ class Reference implements FieldInterface, MinMaxInterface
return count($value) < $this->min;
}
public function isRequired(): bool
{
return false;
}
}
+4 -1
View File
@@ -28,6 +28,9 @@ class Rich implements FieldInterface
$output[$this->name] = $input[$this->name] ?? "";
return $output;
}
public function isRequired(): bool
{
return false;
}
}
+4 -1
View File
@@ -28,6 +28,9 @@ class Textarea implements FieldInterface
$output[$this->name] = $input[$this->name] ?? "";
return $output;
}
public function isRequired(): bool
{
return false;
}
}
-1
View File
@@ -18,7 +18,6 @@ class CollectionSchema implements Schema
public array $visible,
public array $groups,
public Collection $fields,
public bool $isEntry = false,
public string $color = "",
public string $sortBy = "-_sys.updatedAt",
public string $titleTemplate = "",
+74
View File
@@ -0,0 +1,74 @@
<?php
namespace Lucent\Schema\Commands;
use DirectoryIterator;
use Illuminate\Console\Command;
use Lucent\Schema\Schema;
use Lucent\Schema\SchemaService;
use Lucent\Schema\Type;
use function Lucent\Commands\base_path;
class CompileSchemas extends Command
{
protected $signature = 'lucent:schemas';
protected $description = 'Compiles schemas';
public function __construct(
public SchemaService $schemaService
)
{
parent::__construct();
}
public function handle()
{
$configDir = base_path(config('lucent.schemas_path'));
$schemasDirIterator = new DirectoryIterator($configDir);
$schemas = [];
foreach ($schemasDirIterator as $file) {
if ($file->getExtension() !== "json") {
continue;
}
$schemaJson = file_get_contents($configDir . "/" . $file->getFilename());
$schema = json_decode($schemaJson, true);
if (empty($schema)) {
$this->error("Invalid JSON " . $file->getFilename());
return 0;
}
$schemas[] = $schema;
}
$schemas = collect($schemas)->sortBy("label")->values();
$roles = $schemas
->map([$this->schemaService, 'fromArray'])
->whereIn("type", [Type::COLLECTION, Type::FILES])
->reduce(fn($carry, Schema $schema) => array_merge(
$carry,
$schema->read,
$schema->write,
config("lucent.canInvite") ?? [],
config("lucent.canBuild") ?? [],
), []);
$json = [
"schemas" => $schemas->toArray(),
"roles" => collect($roles)->push("admin")->push("removed")->unique()->values()->toArray()
];
if (!file_exists(storage_path("lucent"))) {
mkdir(storage_path("lucent"));
}
file_put_contents(schemas_path(), json_encode($json));
$this->info("Lucent Schemas were updated");
}
}
-1
View File
@@ -11,5 +11,4 @@ enum FieldType: string
case FILE = 'file';
case JSON = 'json';
case REFERENCE = 'reference';
case TAB = 'tab';
}
-1
View File
@@ -19,7 +19,6 @@ class FilesSchema implements Schema
public Collection $fields,
public string $path,
public array $groups,
public bool $isEntry = false,
public string $sortBy = "-_sys.updatedAt",
public string $color = "",
public string $titleTemplate = "",
-2
View File
@@ -22,7 +22,6 @@ class SchemaService
visible: $schemaArr["visible"] ?? [],
groups: $schemaArr["groups"] ?? [],
fields: (new Collection($schemaArr["fields"]))->map([$this, 'mapFields']),
isEntry: $schemaArr["isEntry"] ?? false,
color: $schemaArr["color"] ?? "",
sortBy: $schemaArr["sortBy"] ?? "-_sys.updatedAt",
titleTemplate: $schemaArr["titleTemplate"] ?? "",
@@ -36,7 +35,6 @@ class SchemaService
fields: (new Collection($schemaArr["fields"]))->map([$this, 'mapFields']),
path: $schemaArr["path"] ?? $schemaArr["name"],
groups: $schemaArr["groups"] ?? [],
isEntry: $schemaArr["isEntry"] ?? false,
sortBy: $schemaArr["sortBy"] ?? "-_sys.updatedAt",
color: $schemaArr["color"] ?? "",
titleTemplate: $schemaArr["titleTemplate"] ?? "",
+9
View File
@@ -0,0 +1,9 @@
<?php
namespace Lucent\Schema\Sidebar\Item;
abstract class Item
{
}
+21
View File
@@ -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"],
);
}
}
+21
View File
@@ -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"],
);
}
}
+29
View File
@@ -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))
);
}
}
+29
View File
@@ -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());
}, []);
}
}
+58
View File
@@ -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);
}
}
-1
View File
@@ -22,7 +22,6 @@ class Reference implements FieldInterface, MinMaxInterface
public function __construct(
public string $name,
public string $label,
public string $mime = "",
public string $help = "",
public ?int $min = null,
public ?int $max = null,