adrema/modules/Dashboard/DashboardFactory.php

42 lines
825 B
PHP
Raw Normal View History

2024-09-24 01:26:08 +02:00
<?php
namespace Modules\Dashboard;
use Livewire\Livewire;
class DashboardFactory
{
/**
* @var array<int, class-string<Block>>
*/
private array $blocks = [];
/**
* @return array<int, Block>
*/
public function load(): array
{
return collect($this->blocks)->map(fn ($block) => app($block))->toArray();
}
/**
* @param class-string<Block> $block
*/
public function register(string $block): self
{
$this->blocks[] = $block;
$componentName = str($block)->replace('\\', '.')->explode('.')->map(fn ($part) => str($part)->lcfirst()->kebab())->implode('.');
Livewire::component($componentName, $block);
return $this;
}
public function purge(): self
{
$this->blocks = [];
return $this;
}
}