57 lines
1.8 KiB
PHP
57 lines
1.8 KiB
PHP
<?php
|
|
|
|
namespace Modules\Builder\Services;
|
|
|
|
use InvalidArgumentException;
|
|
use Modules\Builder\Components\ContentComponent;
|
|
use Modules\Builder\Components\HeadingComponent;
|
|
use Modules\Builder\Components\HeroComponent;
|
|
use Modules\Builder\Contracts\BuilderComponent;
|
|
|
|
final class BuilderRegistry
|
|
{
|
|
private array $components = [];
|
|
|
|
private array $widgets = [];
|
|
|
|
private array $fields = [];
|
|
|
|
public function registerDefaults(): void
|
|
{
|
|
foreach ([HeroComponent::class, HeadingComponent::class, ContentComponent::class] as $component) {
|
|
$this->registerComponent(new $component);
|
|
}
|
|
foreach (['metric', 'chart', 'table', 'activity', 'calendar'] as $widget) {
|
|
$this->widgets[$widget] = ['type' => $widget, 'label' => str($widget)->headline()->toString()];
|
|
}
|
|
foreach (['text', 'textarea', 'email', 'number', 'select', 'checkbox', 'date', 'file'] as $field) {
|
|
$this->fields[$field] = ['type' => $field, 'label' => str($field)->headline()->toString()];
|
|
}
|
|
}
|
|
|
|
public function registerComponent(BuilderComponent $component): void
|
|
{
|
|
$this->components[$component->type()] = $component;
|
|
}
|
|
|
|
public function component(string $type): BuilderComponent
|
|
{
|
|
return $this->components[$type] ?? throw new InvalidArgumentException("Unknown builder component [$type].");
|
|
}
|
|
|
|
public function components(): array
|
|
{
|
|
return array_map(fn (BuilderComponent $c) => ['type' => $c->type(), 'label' => $c->label(), 'category' => $c->category(), 'schema' => $c->schema(), 'defaults' => $c->defaults()], array_values($this->components));
|
|
}
|
|
|
|
public function widgets(): array
|
|
{
|
|
return array_values($this->widgets);
|
|
}
|
|
|
|
public function fields(): array
|
|
{
|
|
return array_values($this->fields);
|
|
}
|
|
}
|