Skip to content

Commit

Permalink
Merge pull request #154 from dissto/fix-groups
Browse files Browse the repository at this point in the history
Fix group resolving
  • Loading branch information
CodeWithDennis authored Apr 13, 2024
2 parents d43d96e + d8b46ef commit c284f68
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 44 deletions.
2 changes: 1 addition & 1 deletion src/Commands/FilamentTestsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ protected function getSourceFile(Resource $resource): array|bool|string

$start = microtime(true);

$contents .= $this->getStubContents($stub['path'], $this->getStubVariables($resource));
$contents .= $this->getStubContents($stub['path'], $stub['variables']);

$end = microtime(true);
}
Expand Down
13 changes: 0 additions & 13 deletions src/Handlers/StubHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,21 +68,8 @@ public function getStubs(): Collection
\CodeWithDennis\FilamentTests\Stubs\Page\Edit\Render::make($resource)->get(),

\CodeWithDennis\FilamentTests\Stubs\Page\View\Render::make($resource)->get(),

...$this->getTodoStubs(),
];

return collect($stubs);
}

protected function getTodoStubs()
{
return collect(scandir(__DIR__.'/../Stubs'))
->filter(fn ($file) => str_ends_with($file, '.php'))
->map(fn ($file) => str_replace('.php', '', $file))
->map(fn ($file) => '\\CodeWithDennis\\FilamentTests\\Stubs\\'.$file)
->map(fn ($file) => new $file($this->resource))
->filter(fn ($file) => $file->isTodo())
->map(fn ($file) => $file->get());
}
}
32 changes: 25 additions & 7 deletions src/Stubs/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,29 @@ public function variables(array|Closure|null $variables): static

public function getVariables(): array
{
$defaults = [
return $this->evaluate($this->variables ?? []);
}

public function getDefaultVariables(): array
{
$resource = $this->resource;

$resourceModel = $resource->getModel();
$userModel = \App\Models\User::class;
$modelImport = $resourceModel === $userModel ? "use {$resourceModel};" : "use {$resourceModel};\nuse {$userModel};";

$toBeConverted = [
'MODEL_IMPORT' => $modelImport,
'MODEL_PLURAL_NAME' => str($resourceModel)->afterLast('\\')->plural(),
'MODEL_SINGULAR_NAME' => str($resourceModel)->afterLast('\\'),
'RESOURCE' => str($resource::class)->afterLast('\\'),
'LOAD_TABLE_METHOD_IF_DEFERRED' => $this->tableHasDeferredLoading($resource) ? $this->getDeferredLoadingMethod() : '',
'RESOLVED_GROUP_METHOD' => $this->getGroupMethod(),
];

$variables = $this->variables ?? [];

return $this->evaluate(array_merge($defaults, $variables));
return array_map(function ($value) {
return $this->convertDoubleQuotedArrayString($value);
}, $toBeConverted);
}

public function shouldGenerate(bool|Closure|null $condition): static
Expand Down Expand Up @@ -173,7 +189,7 @@ public function get(): ?array
'group' => $this->getGroup(),
'fileName' => $this->getName().'.stub',
'path' => $this->getPath(),
'variables' => $this->getVariables(),
'variables' => array_merge($this->getDefaultVariables(), $this->getVariables()),
'isTodo' => $this->isTodo(),
];
}
Expand Down Expand Up @@ -584,8 +600,10 @@ public function toGroupMethod(): ?string
{
$group = $this->getGroup();

$parts = array_filter(array_map(fn ($part) => "'".strtolower($part)."'", explode('/', $group)));
$parts = collect(explode('/', $group))
->filter(fn ($part) => ! empty($part))
->map(fn ($part) => "'".trim(str($part)->kebab())."'");

return $this->convertDoubleQuotedArrayString(implode(', ', $parts));
return $this->convertDoubleQuotedArrayString($parts->implode(','));
}
}
23 changes: 0 additions & 23 deletions src/Stubs/SetupStub.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,4 @@
class SetupStub extends Base
{
public Closure|string|null $name = 'Setup';

public function getVariables(): array
{
$resource = $this->resource;

$resourceModel = $resource->getModel();
$userModel = \App\Models\User::class;
$modelImport = $resourceModel === $userModel ? "use {$resourceModel};" : "use {$resourceModel};\nuse {$userModel};";

$toBeConverted = [
'MODEL_IMPORT' => $modelImport,
'MODEL_PLURAL_NAME' => str($resourceModel)->afterLast('\\')->plural(),
'MODEL_SINGULAR_NAME' => str($resourceModel)->afterLast('\\'),
'RESOURCE' => str($resource::class)->afterLast('\\'),
'LOAD_TABLE_METHOD_IF_DEFERRED' => $this->tableHasDeferredLoading($resource) ? $this->getDeferredLoadingMethod() : '',
];

$converted = array_map(function ($value) {
return $this->convertDoubleQuotedArrayString($value);
}, $toBeConverted);

return array_merge($converted, $toBeConverted);
}
}

0 comments on commit c284f68

Please sign in to comment.