Skip to content

Commit

Permalink
Builder wip
Browse files Browse the repository at this point in the history
  • Loading branch information
adrolli committed Dec 2, 2024
1 parent 81369c0 commit 0225e51
Show file tree
Hide file tree
Showing 5 changed files with 205 additions and 28 deletions.
33 changes: 33 additions & 0 deletions packages/builder/src/Blocks/AbstractBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ abstract class AbstractBlock
{
protected bool $fillable = true;

protected array $sectionConfig = [
'name' => null,
'order' => 0,
'is_meta' => false,
];

protected array $casts = [
'model' => [],
];
Expand Down Expand Up @@ -726,4 +732,31 @@ public function getConfig(): array
{
return $this->config;
}

public function setSection(string $name, int $order = 0, bool $isMeta = false): void
{
$this->sectionConfig['name'] = $name;
$this->sectionConfig['order'] = $order;
$this->sectionConfig['is_meta'] = $isMeta;
}

public function getSectionName(): ?string
{
return $this->sectionConfig['name'];
}

public function getSectionOrder(): int
{
return $this->sectionConfig['order'];
}

public function isMetaSection(): bool
{
return $this->sectionConfig['is_meta'];
}

public function hasSection(): bool
{
return $this->sectionConfig['name'] !== null;
}
}
71 changes: 71 additions & 0 deletions packages/builder/src/Blocks/Sections/AddressSection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

declare(strict_types=1);

namespace Moox\Builder\Blocks\Sections;

use Moox\Builder\Blocks\AbstractBlock;

final class AddressSection extends AbstractBlock
{
protected array $formFields = [];

protected array $tableColumns = [];

protected array $filters = [];

protected array $actions = [];

protected array $factories = [];

public function __construct(
string $name = 'address_section',
string $label = 'Address',
string $description = 'Address section with street, city, postal code and country',
bool $nullable = false,
) {
parent::__construct($name, $label, $description, $nullable);

$this->setSection('address', 10);

$this->useStatements = [
'resource' => [
'forms' => [
'use Filament\Forms\Components\Section;',
'use Filament\Forms\Components\TextInput;',
],
'columns' => ['use Filament\Tables\Columns\TextColumn;'],
],
];

$this->formFields['resource'] = [
"Section::make('Address')->schema([
TextInput::make('street')->required(),
TextInput::make('city')->required(),
TextInput::make('postal_code')->required(),
TextInput::make('country')->required()
])",
];

$this->tableColumns['resource'] = [
"TextColumn::make('city')->sortable()->searchable()",
"TextColumn::make('country')->sortable()->searchable()",
];

$this->migrations = [
'fields' => [
'street' => '$table->string("street")',
'city' => '$table->string("city")',
'postal_code' => '$table->string("postal_code")',
'country' => '$table->string("country")',
],
];

$this->factories['model']['definitions'] = [
'street' => 'fake()->streetAddress()',
'city' => 'fake()->city()',
'postal_code' => 'fake()->postcode()',
'country' => 'fake()->country()',
];
}
}
49 changes: 21 additions & 28 deletions packages/builder/src/Generators/Entity/ResourceGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,28 @@
use Moox\Builder\Generators\Entity\Pages\EditPageGenerator;
use Moox\Builder\Generators\Entity\Pages\ListPageGenerator;
use Moox\Builder\Generators\Entity\Pages\ViewPageGenerator;
use Moox\Builder\Services\Entity\SectionManager;
use Moox\Builder\Services\File\FileManager;
use RuntimeException;

class ResourceGenerator extends AbstractGenerator
{
private SectionManager $sectionManager;

public function __construct(
BuildContext $context,
FileManager $fileManager,
array $blocks = []
) {
parent::__construct($context, $fileManager, $blocks);
$this->sectionManager = new SectionManager;
}

public function generate(): void
{
$this->processBlocks();
$sections = $this->getSections();

$this->generateResourcePages();

$template = $this->loadStub($this->getTemplate());
Expand All @@ -47,9 +54,9 @@ public function generate(): void
'use_statements' => $this->formatUseStatements(),
'traits' => $this->formatTraits(),
'form_schema' => $this->getFormSchema(),
'form_sections' => $this->getFormSections(),
'form_sections' => $sections['form_sections'],
'meta_schema' => $this->getMetaSchema(),
'meta_sections' => $this->getMetaSections(),
'meta_sections' => $sections['meta_sections'],
'table_columns' => $this->getTableColumns(),
'table_filters' => $this->getTableFilters(),
'table_actions' => $this->getTableActions(),
Expand All @@ -65,6 +72,18 @@ public function generate(): void
$this->writeFile($path, $content);
}

protected function processBlocks(): void
{
foreach ($this->getBlocks() as $block) {
$this->sectionManager->addBlock($block);
}
}

protected function getSections(): array
{
return $this->sectionManager->getFormattedSections();
}

protected function generateResourcePages(): void
{
$pageGenerators = [
Expand Down Expand Up @@ -244,19 +263,6 @@ protected function getModelReference(): string
return $this->context->formatNamespace('model', true).'\\'.$this->context->getEntityName();
}

protected function getFormSections(): string
{
$sections = [];
foreach ($this->getBlocks() as $block) {
$formSections = $block->getFormSections();
if (! empty($formSections)) {
$sections = array_merge($sections, $formSections);
}
}

return implode(",\n ", $sections);
}

protected function getMetaSchema(): string
{
$fields = [];
Expand All @@ -270,19 +276,6 @@ protected function getMetaSchema(): string
return implode(",\n ", $fields);
}

protected function getMetaSections(): string
{
$sections = [];
foreach ($this->getBlocks() as $block) {
$metaSections = $block->getMetaSections();
if (! empty($metaSections)) {
$sections = array_merge($sections, $metaSections);
}
}

return implode(",\n ", $sections);
}

protected function getUseStatements(string $context, ?string $subContext = null): array
{
$statements = [];
Expand Down
4 changes: 4 additions & 0 deletions packages/builder/src/Presets/SimpleItemPreset.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Moox\Builder\Presets;

use Moox\Builder\Blocks\Sections\AddressSection;
use Moox\Builder\Blocks\Simple;
use Moox\Builder\Blocks\Tabs;
use Moox\Builder\Blocks\Taxonomy;
Expand Down Expand Up @@ -34,6 +35,7 @@ protected function initializePreset(): void
description: 'The content of the item'
),
new Tabs,
/*
new Taxonomy(
name: 'tags',
label: 'Tags',
Expand All @@ -45,6 +47,8 @@ protected function initializePreset(): void
description: 'The categories of the item',
nested: true,
),
*/
new AddressSection,
];
}
}
76 changes: 76 additions & 0 deletions packages/builder/src/Services/Entity/SectionManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

declare(strict_types=1);

namespace Moox\Builder\Services\Entity;

use Moox\Builder\Blocks\AbstractBlock;

final class SectionManager
{
private array $sections = [];

private array $metaSections = [];

public function addBlock(AbstractBlock $block): void
{
if (! $block->hasSection()) {
$sectionName = 'default';
} else {
$sectionName = $block->getSectionName();
}

$order = $block->getSectionOrder();

if ($block->isMetaSection()) {
if (! isset($this->metaSections[$sectionName])) {
$this->metaSections[$sectionName] = [];
}
$this->metaSections[$sectionName][$order][] = $block;
} else {
if (! isset($this->sections[$sectionName])) {
$this->sections[$sectionName] = [];
}
$this->sections[$sectionName][$order][] = $block;
}
}

private function formatSections(array $sections): string
{
if (empty($sections)) {
return '';
}

$output = [];
foreach ($sections as $sectionName => $orderBlocks) {
ksort($orderBlocks);
$formattedBlocks = [];

foreach ($orderBlocks as $blocks) {
foreach ($blocks as $block) {
if (isset($block->getFormFields()['resource'])) {
$formattedBlocks = array_merge($formattedBlocks, $block->getFormFields()['resource']);
}
}
}

if (! empty($formattedBlocks)) {
if ($sectionName === 'default') {
$output[] = implode(',', $formattedBlocks);
} else {
$output[] = "Section::make('".$sectionName."')->schema([".implode(',', $formattedBlocks).'])';
}
}
}

return implode(',', $output);
}

public function getFormattedSections(): array
{
return [
'form_sections' => $this->formatSections($this->sections),
'meta_sections' => $this->formatSections($this->metaSections),
];
}
}

0 comments on commit 0225e51

Please sign in to comment.