Skip to content

Commit

Permalink
Refactor core traits
Browse files Browse the repository at this point in the history
  • Loading branch information
adrolli committed Dec 10, 2024
1 parent 9bd70c1 commit 57122f6
Show file tree
Hide file tree
Showing 26 changed files with 611 additions and 341 deletions.
10 changes: 5 additions & 5 deletions packages/builder/config/builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@
'toggle-buttons' => \Moox\Builder\Blocks\Filament\ToggleButtons::class,
],
'features' => [
'simple' => \Moox\Builder\Blocks\Simple::class,
'soft-delete' => \Moox\Builder\Blocks\SoftDelete::class,
'title-with-slug' => \Moox\Builder\Blocks\TitleWithSlug::class,
'simple-status' => \Moox\Builder\Blocks\SimpleStatus::class,
'simple-type' => \Moox\Builder\Blocks\SimpleType::class,
'simple' => \Moox\Builder\Blocks\Single\Simple::class,

Check failure on line 42 in packages/builder/config/builder.php

View workflow job for this annotation

GitHub Actions / phpstan

Class Moox\Builder\Blocks\Single\Simple not found.
'soft-delete' => \Moox\Builder\Blocks\Single\SoftDelete::class,

Check failure on line 43 in packages/builder/config/builder.php

View workflow job for this annotation

GitHub Actions / phpstan

Class Moox\Builder\Blocks\Single\SoftDelete not found.
'title-with-slug' => \Moox\Builder\Blocks\Moox\TitleWithSlug::class,
'simple-status' => \Moox\Builder\Blocks\Features\SimpleStatus::class,
'simple-type' => \Moox\Builder\Blocks\Features\SimpleType::class,
],
'sections' => [
'address' => \Moox\Builder\Blocks\Sections\AddressSection::class,
Expand Down
14 changes: 14 additions & 0 deletions packages/builder/src/Blocks/AbstractBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,10 @@ abstract class AbstractBlock

protected array $sections = [];

protected array $resourceInit = [];

protected array $tableInit = [];

public function __construct(
protected string $name,
protected string $label,
Expand Down Expand Up @@ -828,4 +832,14 @@ public function getFillableFields(): array
{
return [$this->name];
}

public function getResourceInit(): array
{
return $this->resourceInit;
}

public function getTableInit(): array
{
return $this->tableInit;
}
}
10 changes: 4 additions & 6 deletions packages/builder/src/Blocks/Singles/Publish.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,11 @@ public function __construct(
'published_at' => 'null',
],
];
}

$this->config['tabs'] = [
public function getTabs(): array
{
return [
'all' => [
'label' => 'trans//core::core.all',
'icon' => 'gmdi-filter-list',
Expand Down Expand Up @@ -196,9 +199,4 @@ public function __construct(
],
];
}

public function getTabs(): array
{
return $this->config['tabs'];
}
}
36 changes: 28 additions & 8 deletions packages/builder/src/Blocks/Singles/Simple.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,39 @@ public function __construct(
) {
parent::__construct($name, $label, $description);

$this->traits['resource'] = ['Moox\Core\Traits\Simple\SingleSimpleInResource'];
$this->traits['pages']['list'] = ['Moox\Core\Traits\Simple\SingleSimpleInListPage'];
$this->traits['pages']['view'] = ['Moox\Core\Traits\Simple\SingleSimpleInViewPage'];
$this->traits['pages']['create'] = ['Moox\Core\Traits\Simple\SingleSimpleInCreatePage'];
$this->traits['pages']['edit'] = ['Moox\Core\Traits\Simple\SingleSimpleInEditPage'];
$this->traits['resource'] = [
'Moox\Core\Traits\Simple\SingleSimpleInResource',
'Moox\Core\Traits\Base\BaseInResource',
];
$this->traits['pages']['list'] = [
'Moox\Core\Traits\Simple\SingleSimpleInListPage',
'Moox\Core\Traits\Base\BaseInListPage',
];
$this->traits['pages']['view'] = [
'Moox\Core\Traits\Simple\SingleSimpleInViewPage',
'Moox\Core\Traits\Base\BaseInViewPage',
];
$this->traits['pages']['create'] = [
'Moox\Core\Traits\Simple\SingleSimpleInCreatePage',
'Moox\Core\Traits\Base\BaseInCreatePage',
];
$this->traits['pages']['edit'] = [
'Moox\Core\Traits\Simple\SingleSimpleInEditPage',
'Moox\Core\Traits\Base\BaseInEditPage',
];

$this->addSection('meta')
->asMeta()
->withFields([
'static::getSimpleFormActions()',
'static::getFormActions()',
]);

$this->actions['resource'] = 'static::getSimpleResourceActions()';
$this->actions['bulk'] = 'static::getSimpleBulkActions()';
$this->actions['resource'] = [
'...static::getTableActions()',
];

$this->actions['bulk'] = [
'...static::getBulkActions()',
];
}
}
95 changes: 51 additions & 44 deletions packages/builder/src/Blocks/Singles/SoftDelete.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,48 +23,29 @@ public function __construct(
) {
parent::__construct($name, $label, $description);

$this->useStatements = [
'resource' => [
'actions' => [
'use Filament\Tables\Actions\DeleteBulkAction;',
'use Filament\Tables\Actions\RestoreBulkAction;',
],
],
'pages' => [
'edit' => ['use Filament\Actions\Action;'],
'view' => ['use Filament\Actions\Action;'],
],
$this->traits['model'] = [
'Moox\Core\Traits\SoftDelete\SingleSoftDeleteInModel',
'Moox\Core\Traits\Base\BaseInModel',
];

$this->traits['model'] = ['Moox\Core\Traits\SoftDelete\SingleSoftDeleteInModel'];
$this->traits['resource'] = ['Moox\Core\Traits\SoftDelete\SingleSoftDeleteInResource'];
$this->traits['pages']['edit'] = ['Moox\Core\Traits\SoftDelete\SingleSoftDeleteInEditPage'];
$this->traits['pages']['list'] = ['Moox\Core\Traits\SoftDelete\SingleSoftDeleteInListPage'];
$this->traits['pages']['view'] = ['Moox\Core\Traits\SoftDelete\SingleSoftDeleteInViewPage'];
$this->traits['pages']['create'] = ['Moox\Core\Traits\SoftDelete\SingleSoftDeleteInCreatePage'];

$this->actions['pages']['edit']['header'] = [
"Action::make('restore')
->label(__('core::core.restore'))
->color('success')
->button()
->extraAttributes(['class' => 'w-full'])
->action(fn (\$record) => \$record->restore())
->visible(fn (\$livewire, \$record) => \$record && \$record->trashed())",
"Action::make('delete')
->label(__('core::core.delete'))
->color('danger')
->button()
->extraAttributes(['class' => 'w-full'])
->action(fn (\$record) => \$record->delete())
->visible(fn (\$livewire, \$record) => \$record && ! \$record->trashed())",
$this->traits['resource'] = [
'Moox\Core\Traits\SoftDelete\SingleSoftDeleteInResource',
'Moox\Core\Traits\Base\BaseInResource',
];

$this->actions['bulk'] = [
"DeleteBulkAction::make()
->hidden(fn () => request()->routeIs('*.trash'))",
"RestoreBulkAction::make()
->visible(fn () => request()->routeIs('*.trash'))",
$this->traits['pages']['edit'] = [
'Moox\Core\Traits\SoftDelete\SingleSoftDeleteInEditPage',
'Moox\Core\Traits\Base\BaseInEditPage',
];
$this->traits['pages']['list'] = [
'Moox\Core\Traits\SoftDelete\SingleSoftDeleteInListPage',
'Moox\Core\Traits\Base\BaseInListPage',
];
$this->traits['pages']['view'] = [
'Moox\Core\Traits\SoftDelete\SingleSoftDeleteInViewPage',
'Moox\Core\Traits\Base\BaseInViewPage',
];
$this->traits['pages']['create'] = [
'Moox\Core\Traits\SoftDelete\SingleSoftDeleteInCreatePage',
'Moox\Core\Traits\Base\BaseInCreatePage',
];

$this->migrations['fields'] = [
Expand All @@ -74,16 +55,42 @@ public function __construct(
$this->addSection('meta')
->asMeta()
->withFields([
'static::getSimpleFormActions()',
'static::getFormActions()',
]);

$this->actions['resource'] = [
'\Filament\Tables\Actions\ViewAction::make()',
'\Filament\Tables\Actions\EditAction::make()',
'...static::getTableActions()',
];

$this->actions['bulk'] = [
'\Filament\Tables\Actions\DeleteBulkAction::make()',
'...static::getBulkActions()',
];
}

public function getTabs(): array
{
return [
'all' => [
'label' => 'trans//core::core.all',
'icon' => 'gmdi-filter-list',
'query' => [
['field' => 'deleted_at', 'operator' => '=', 'value' => null],
],
],
'deleted' => [
'label' => 'trans//core::core.deleted',
'icon' => 'gmdi-filter-list',
'query' => [
['field' => 'deleted_at', 'operator' => '!=', 'value' => null],
],
],
];
}

public function getTableInit(): array
{
return [
'$currentTab = static::getCurrentTab();',
];
}
}
9 changes: 9 additions & 0 deletions packages/builder/src/Generators/Entity/ResourceGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ public function generate(): void
}

$formSchema = $this->generateFormSchema();
$resourceInit = [];
$tableInit = [];
foreach ($this->getBlocks() as $block) {
$resourceInit = array_merge($resourceInit, $block->getResourceInit());
$tableInit = array_merge($tableInit, $block->getTableInit());
}

$variables = [
'namespace' => $this->context->formatNamespace('resource', false),
'class_name' => $this->context->getEntityName(),
Expand All @@ -64,6 +71,8 @@ public function generate(): void
'table_bulk_actions' => $this->getTableBulkActions(),
'default_sort_column' => $this->getDefaultSortColumn(),
'default_sort_direction' => $this->getDefaultSortDirection(),
'resource_init' => implode("\n ", $resourceInit),
'table_init' => implode("\n ", $tableInit),
];

$content = $this->replaceTemplateVariables($template, $variables);
Expand Down
6 changes: 3 additions & 3 deletions packages/builder/src/Presets/SoftDeleteItemPreset.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ protected function initializePreset(): void
createForm: '\Moox\Tag\Forms\TaxonomyCreateForm::class',
),
new AddressSection,
new SimpleStatus(
enum: ['Probably', 'Never', 'Done', 'Maybe'],
),
new SimpleType(
enum: ['Post', 'Page'],
),
new SimpleStatus(
enum: ['Probably', 'Never', 'Done', 'Maybe'],
),
new SoftDelete,
];
}
Expand Down
Loading

0 comments on commit 57122f6

Please sign in to comment.