Skip to content

Commit

Permalink
Builder wip
Browse files Browse the repository at this point in the history
not using data and resource bug
  • Loading branch information
adrolli committed Nov 26, 2024
1 parent e242a81 commit 6579a92
Show file tree
Hide file tree
Showing 13 changed files with 228 additions and 66 deletions.
11 changes: 5 additions & 6 deletions packages/builder/DEVLOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@ We work on these tasks in order from top to bottom:
### Entity

- [WIP] Iterate over all blocks, presets and contexts to find out if they are working as expected

- [ ] The Resource needs a more complex layout introducing regions
- [ ] Presets need to be able to define where blocks should be placed

- [ ] ResourceGenerator: Currently all block and pages use statements as well as traits are missing, need to fix this
- [ ] AbstractGenerator (and AbstractPageGenerator) use direct access to get use statements and traits. But we need to use the data array
- [ ] All generators need to use the implementation from AbstractGenerator then
- [ ] We can flatten the nested array that is special for the resource use statements, as we do not need the information
- [ ] Finally all use statements, traits as well as all other data should come from the data array, to be able to rebuild from an older build for example. Please verify this.
- [ ] Add default actions or try to add the Publish Traits first, then use actions to test the entities

- [ ] Config and translations have wrong filenames and must be wired correctly
- [ ] Need to generate Tabs, Taxonomy and Relations partials, may already work partially

- [ ] Refactor DeleteCommand to use new services
- [ ] Add --migration option to create command
- [ ] Would Builder now be able to generate itself?
Expand Down
4 changes: 4 additions & 0 deletions packages/builder/config/builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -825,6 +825,10 @@
*/

'contexts' => [
'moox' => [
'base_path' => app_path('Moox'),
'base_namespace' => 'App\\Moox',
],
'app' => [
'base_path' => app_path(),
'base_namespace' => 'App',
Expand Down
36 changes: 21 additions & 15 deletions packages/builder/src/Blocks/AbstractBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ abstract class AbstractBlock
'columns' => [],
'filters' => [],
'actions' => [],
'traits' => [],
'pages' => [],
],
'pages' => [
Expand Down Expand Up @@ -229,9 +228,24 @@ public function getMethods(string $context, ?string $type = null): array
return $this->methods[$context] ?? [];
}

public function getFormFields(): array
public function getFormFields(string $context = 'resource'): array
{
return $this->formFields['resource'] ?? [];
return $this->formFields[$context] ?? [];
}

public function getFormSections(string $context = 'resource'): array
{
return $this->formSections[$context] ?? [];
}

public function getMetaFields(string $context = 'resource'): array
{
return $this->metaFields[$context] ?? [];
}

public function getMetaSections(string $context = 'resource'): array
{
return $this->metaSections[$context] ?? [];
}

public function getTableColumns(): array
Expand Down Expand Up @@ -422,20 +436,12 @@ public function getResourceUseStatements(): array
'use Illuminate\Database\Eloquent\Builder;',
];

$statements = array_merge(
return array_unique(array_merge(
$baseStatements,
$this->getFlattenedUseStatements('model'),
$this->getFlattenedUseStatements('resource'),
$this->getFlattenedUseStatements('pages')
);

if ($this->traits['resource']) {
foreach ($this->traits['resource'] as $trait) {
$statements[] = 'use Moox\Core\Traits\\'.$trait.';';
}
}

return array_unique($statements);
));
}

public function getPageUseStatements(string $page): array
Expand All @@ -448,8 +454,8 @@ public function resolveBlockDependencies(array $blocks): array
$resolvedBlocks = $blocks;

foreach ($blocks as $block) {
if (isset($block->containsBlocks)) {
foreach ($block->containsBlocks as $includedBlock) {
if (isset($block->includedBlocks)) {
foreach ($block->includedBlocks as $includedBlock) {
$resolvedBlocks = array_filter(
$resolvedBlocks,
fn ($b) => ! ($b instanceof $includedBlock)
Expand Down
20 changes: 12 additions & 8 deletions packages/builder/src/Blocks/Publish.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@

class Publish extends AbstractBlock
{
protected array $includedBlocks = [
protected array $requiredBlocks = [];

protected array $containsBlocks = [
SoftDelete::class,
];

protected array $incompatibleBlocks = [];

public function __construct(
string $name = 'publish',
string $label = 'Publish',
Expand All @@ -27,10 +31,6 @@ public function __construct(
'columns' => ['use Filament\Tables\Columns\TextColumn;'],
'filters' => ['use Filament\Tables\Filters\Filter;'],
'actions' => ['use Filament\Actions\Action;'],
'traits' => [
'use Moox\Core\Traits\SinglePublishInResource;',
'use Illuminate\Database\Eloquent\SoftDeletes;',
],
'pages' => [
'use App\Filament\Resources\PublishableItemResource\Pages;',
],
Expand All @@ -43,9 +43,9 @@ public function __construct(
],
];

$this->traits['model'] = ['SinglePublishInModel'];
$this->traits['resource'] = ['SinglePublishInResource'];
$this->traits['pages']['list'] = ['SinglePublishInListPage'];
$this->traits['model'] = ['Moox\Core\Traits\SinglePublishInModel'];
$this->traits['resource'] = ['Moox\Core\Traits\SinglePublishInResource'];
$this->traits['pages']['list'] = ['Moox\Core\Traits\SinglePublishInListPage'];

$this->methods['model'] = [
'scopes' => [
Expand Down Expand Up @@ -81,6 +81,9 @@ public function __construct(
];

$this->metaFields['resource'] = [
'static::getFormActions()',
'static::getPublishAtFormField()',
'static::getAuthorFormField()',
"DateTimePicker::make('publish_at')
->label(__('core::core.publish_at'))
->nullable()",
Expand Down Expand Up @@ -133,6 +136,7 @@ public function __construct(
$this->migrations['fields'] = [
'$table->timestamp("published_at")->nullable()',
'$table->timestamp("publish_at")->nullable()',
'$table->softDeletes()',
];

$this->factories['model']['states'] = [
Expand Down
6 changes: 5 additions & 1 deletion packages/builder/src/Blocks/SoftDelete.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

class SoftDelete extends AbstractBlock
{
protected array $requiredBlocks = [];

protected array $containsBlocks = [];

protected array $incompatibleBlocks = [
Publish::class,
];
Expand Down Expand Up @@ -39,7 +43,7 @@ public function __construct(
],
];

$this->traits['model'] = ['SoftDeletes'];
$this->traits['model'] = ['Illuminate\Database\Eloquent\SoftDeletes'];

$this->methods['resource'] = [
'public static function getTableQuery(?string $currentTab = null): Builder {
Expand Down
8 changes: 7 additions & 1 deletion packages/builder/src/Generators/Entity/AbstractGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,13 @@ protected function formatTraits(): string
return '';
}

return 'use '.implode(', ', array_unique($traits)).';';
$shortTraits = array_map(function ($trait) {
$parts = explode('\\', $trait);

return end($parts);
}, $traits);

return 'use '.implode(', ', array_unique($shortTraits)).';';
}

protected function formatMethods(): string
Expand Down
20 changes: 20 additions & 0 deletions packages/builder/src/Generators/Entity/ModelGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,24 @@ protected function getGeneratorType(): string
{
return 'model';
}
protected function formatUseStatements(): string
{
$statements = $this->getUseStatements('model');
foreach ($this->getBlocks() as $block) {
$blockTraits = $block->getTraits('model');
if (! empty($blockTraits)) {
foreach ($blockTraits as $trait) {
if (! in_array("use $trait;", $statements)) {
$statements[] = "use $trait;";
}
}
}
}

return implode("\n", array_map(function ($statement) {
return rtrim($statement, ';').';';
}, array_unique($statements)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ protected function formatUseStatements(): string
$blockTraits = $block->getTraits('pages');
if (isset($blockTraits[$pageType])) {
foreach ($blockTraits[$pageType] as $trait) {
$statements[] = 'use Moox\Core\Traits\\'.$trait.';';
$statements[] = "use $trait;";
}
}
}
Expand All @@ -122,7 +122,12 @@ protected function formatTraits(): string
$pageType = strtolower($this->getPageType());
$blockTraits = $block->getTraits('pages');
if (isset($blockTraits[$pageType])) {
$traits = array_merge($traits, $blockTraits[$pageType]);
$shortTraits = array_map(function ($trait) {
$parts = explode('\\', $trait);

return end($parts);
}, $blockTraits[$pageType]);
$traits = array_merge($traits, $shortTraits);
}
}

Expand Down
Loading

0 comments on commit 6579a92

Please sign in to comment.