Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 62 additions & 13 deletions src/Commands/CrudGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public function handle()

// Build the class name from table name
$this->name = $this->_buildClassName();
$getColumns = $this->getColumns();

// Generate the crud
$this->buildOptions()
Expand All @@ -76,6 +77,7 @@ protected function promptForMissingArgumentsUsing(): array
'tailwind' => 'Blade with Tailwind css',
'livewire' => 'Livewire with Tailwind css',
'api' => 'API only',
'vue' => 'Vue with Tailwind css',
],
scroll: 4,
),
Expand Down Expand Up @@ -109,15 +111,15 @@ protected function writeRoute(): static
"Route::get('/{$this->_getRoute()}/update/{{$replacements['{{modelNameLowerCase}}']}}', \\$this->livewireNamespace\\{$replacements['{{modelNamePluralUpperCase}}']}\Edit::class)->name('{$this->_getRoute()}.edit');",
],
'api' => [
"Route::apiResource('".$this->_getRoute()."', {$this->name}Controller::class);",
"Route::apiResource('" . $this->_getRoute() . "', {$this->name}Controller::class);",
],
default => [
"Route::resource('".$this->_getRoute()."', {$this->name}Controller::class);",
"Route::resource('" . $this->_getRoute() . "', {$this->name}Controller::class);",
]
};

foreach ($lines as $line) {
$this->info('<bg=blue;fg=white>'.$line.'</>');
$this->info('<bg=blue;fg=white>' . $line . '</>');
}

$this->info('');
Expand Down Expand Up @@ -153,11 +155,14 @@ protected function buildController(): static

$stubFolder = match ($this->options['stack']) {
'api' => 'api/',
'vue' => 'vue/',
default => ''
};

$controllerTemplate = str_replace(
array_keys($replace), array_values($replace), $this->getStub($stubFolder.'Controller')
array_keys($replace),
array_values($replace),
$this->getStub($stubFolder . 'Controller')
);

$this->write($controllerPath, $controllerTemplate);
Expand All @@ -166,7 +171,9 @@ protected function buildController(): static
$resourcePath = $this->_getResourcePath($this->name);

$resourceTemplate = str_replace(
array_keys($replace), array_values($replace), $this->getStub($stubFolder.'Resource')
array_keys($replace),
array_values($replace),
$this->getStub($stubFolder . 'Resource')
);

$this->write($resourcePath, $resourceTemplate);
Expand All @@ -183,20 +190,24 @@ protected function buildLivewire(): void
$replace = array_merge($this->buildReplacements(), $this->modelReplacements());

foreach (['Index', 'Show', 'Edit', 'Create'] as $component) {
$componentPath = $this->_getLivewirePath($folder.'/'.$component);
$componentPath = $this->_getLivewirePath($folder . '/' . $component);

$componentTemplate = str_replace(
array_keys($replace), array_values($replace), $this->getStub('livewire/'.$component)
array_keys($replace),
array_values($replace),
$this->getStub('livewire/' . $component)
);

$this->write($componentPath, $componentTemplate);
}

// Form
$formPath = $this->_getLivewirePath('Forms/'.$this->name.'Form');
$formPath = $this->_getLivewirePath('Forms/' . $this->name . 'Form');

$componentTemplate = str_replace(
array_keys($replace), array_values($replace), $this->getStub('livewire/Form')
array_keys($replace),
array_values($replace),
$this->getStub('livewire/Form')
);

$this->write($formPath, $componentTemplate);
Expand All @@ -221,7 +232,9 @@ protected function buildModel(): static
$replace = array_merge($this->buildReplacements(), $this->modelReplacements());

$modelTemplate = str_replace(
array_keys($replace), array_values($replace), $this->getStub('Model')
array_keys($replace),
array_values($replace),
$this->getStub('Model')
);

$this->write($modelPath, $modelTemplate);
Expand All @@ -232,7 +245,9 @@ protected function buildModel(): static
$this->info('Creating Request Class ...');

$requestTemplate = str_replace(
array_keys($replace), array_values($replace), $this->getStub('Request')
array_keys($replace),
array_values($replace),
$this->getStub('Request')
);

$this->write($requestPath, $requestTemplate);
Expand All @@ -258,33 +273,67 @@ protected function buildViews(): static
$tableBody = "\n";
$viewRows = "\n";
$form = "\n";

$formCreate = "\n";
foreach ($this->getFilteredColumns() as $column) {
$title = Str::title(str_replace('_', ' ', $column));

$tableHead .= $this->getHead($title);
$tableBody .= $this->getBody($column);
$viewRows .= $this->getField($title, $column, 'view-field');
$form .= $this->getField($title, $column);
$formCreate .= $column . ': props.' . Str::camel($this->name) . '?.' . $column . ',' . "\n";
}



$replace = array_merge($this->buildReplacements(), [
'{{tableHeader}}' => $tableHead,
'{{tableBody}}' => $tableBody,
'{{viewRows}}' => $viewRows,
'{{form}}' => $form,
'{{formCreate}}' => $formCreate,
]);

$this->buildLayout();

if ($this->options['stack'] == 'vue') {

if (!$this->files->exists(resource_path("/js/components/Pagination.vue"))) {
$this->write(
resource_path('/js/components/Pagination.vue'),
$this->getStub('views/vue/Pagination')
);
}
foreach (['Index', 'Create', 'Edit', 'Show'] as $view) {
$path = match ($this->options['stack']) {
'livewire' => $this->isLaravel12() ? "views/{$this->options['stack']}/12/$view" : "views/{$this->options['stack']}/default/$view",
'vue' => "views/{$this->options['stack']}/$view",
default => "views/{$this->options['stack']}/$view"
};

$viewTemplate = str_replace(
array_keys($replace),
array_values($replace),
$this->getStub($path)
);

$this->write($this->_getViewPath($view), $viewTemplate);
}

return $this;
}


foreach (['index', 'create', 'edit', 'form', 'show'] as $view) {
$path = match ($this->options['stack']) {
'livewire' => $this->isLaravel12() ? "views/{$this->options['stack']}/12/$view" : "views/{$this->options['stack']}/default/$view",
default => "views/{$this->options['stack']}/$view"
};

$viewTemplate = str_replace(
array_keys($replace), array_values($replace), $this->getStub($path)
array_keys($replace),
array_values($replace),
$this->getStub($path)
);

$this->write($this->_getViewPath($view), $viewTemplate);
Expand Down
54 changes: 33 additions & 21 deletions src/Commands/GeneratorCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,10 @@ protected function getStub(string $type, bool $content = true): string
$stub_path = config('crud.stub_path', 'default');

if (blank($stub_path) || $stub_path == 'default') {
$stub_path = __DIR__.'/../stubs/';
$stub_path = __DIR__ . '/../stubs/';
}

$path = Str::finish($stub_path, '/')."$type.stub";
$path = Str::finish($stub_path, '/') . "$type.stub";

if (! $content) {
return $path;
Expand All @@ -189,7 +189,7 @@ private function _getSpace(int $no = 1): string
*/
protected function _getControllerPath(string $name): string
{
return app_path($this->_getNamespacePath($this->controllerNamespace)."{$name}Controller.php");
return app_path($this->_getNamespacePath($this->controllerNamespace) . "{$name}Controller.php");
}

/**
Expand All @@ -199,7 +199,7 @@ protected function _getControllerPath(string $name): string
*/
protected function _getApiControllerPath(string $name): string
{
return app_path($this->_getNamespacePath($this->apiControllerNamespace)."{$name}Controller.php");
return app_path($this->_getNamespacePath($this->apiControllerNamespace) . "{$name}Controller.php");
}

/**
Expand All @@ -209,7 +209,7 @@ protected function _getApiControllerPath(string $name): string
*/
protected function _getResourcePath(string $name): string
{
return app_path($this->_getNamespacePath($this->resourceNamespace)."{$name}Resource.php");
return app_path($this->_getNamespacePath($this->resourceNamespace) . "{$name}Resource.php");
}

/**
Expand All @@ -219,7 +219,7 @@ protected function _getResourcePath(string $name): string
*/
protected function _getLivewirePath(string $name): string
{
return app_path($this->_getNamespacePath($this->livewireNamespace)."{$name}.php");
return app_path($this->_getNamespacePath($this->livewireNamespace) . "{$name}.php");
}

/**
Expand All @@ -229,7 +229,7 @@ protected function _getLivewirePath(string $name): string
*/
protected function _getRequestPath(string $name): string
{
return app_path($this->_getNamespacePath($this->requestNamespace)."{$name}Request.php");
return app_path($this->_getNamespacePath($this->requestNamespace) . "{$name}Request.php");
}

/**
Expand All @@ -239,7 +239,7 @@ protected function _getRequestPath(string $name): string
*/
protected function _getModelPath(string $name): string
{
return $this->makeDirectory(app_path($this->_getNamespacePath($this->modelNamespace)."$name.php"));
return $this->makeDirectory(app_path($this->_getNamespacePath($this->modelNamespace) . "$name.php"));
}

/**
Expand Down Expand Up @@ -273,9 +273,11 @@ private function _getLayoutPath(): string
*/
protected function _getViewPath(string $view): string
{

$name = Str::kebab($this->name);
$path = match ($this->options['stack']) {
'livewire' => "/views/livewire/$name/$view.blade.php",
'vue' => "/js/pages/$name/$view.vue",
default => "/views/$name/$view.blade.php"
};

Expand Down Expand Up @@ -337,7 +339,9 @@ protected function getField(string $title, string $column, string $type = 'form-
};

return str_replace(
array_keys($replace), array_values($replace), $this->getStub($path)
array_keys($replace),
array_values($replace),
$this->getStub($path)
);
}

Expand All @@ -354,13 +358,14 @@ protected function getHead(string $title): string

$attr = match ($this->options['stack']) {
'tailwind', 'livewire' => 'scope="col" class="py-3 pl-4 pr-3 text-left text-xs font-semibold uppercase tracking-wide text-gray-500"',
'vue' => 'data-slot="table-head" class="text-muted-foreground h-10 px-2 text-left align-middle font-medium whitespace-nowrap [&:has([role=checkbox])]:pr-0 [&>[role=checkbox]]:translate-y-[2px]"',
default => ''
};

return str_replace(
array_keys($replace),
array_values($replace),
$this->_getSpace(9).'<th '.$attr.'>{{title}}</th>'."\n"
$this->_getSpace(9) . '<th ' . $attr . '>{{title}}</th>' . "\n"
);
}

Expand All @@ -377,13 +382,20 @@ protected function getBody($column): string

$attr = match ($this->options['stack']) {
'tailwind', 'livewire' => 'class="whitespace-nowrap px-3 py-4 text-sm text-gray-500"',
'vue' => 'data-slot="table-cell" class="p-2 align-middle whitespace-nowrap [&:has([role=checkbox])]:pr-0 [&>[role=checkbox]]:translate-y-[2px]"',
default => ''
};

if ($this->options['stack'] === 'vue') {
return str_replace(
array_keys($replace),
array_values($replace),
$this->_getSpace(10) . '<td ' . $attr . '>{{ {{modelNameLowerCase}}.{{column}} }}</td>' . "\n"
);
}
return str_replace(
array_keys($replace),
array_values($replace),
$this->_getSpace(10).'<td '.$attr.'>{{ ${{modelNameLowerCase}}->{{column}} }}</td>'."\n"
$this->_getSpace(10) . '<td ' . $attr . '>{{ ${{modelNameLowerCase}}->{{column}} }}</td>' . "\n"
);
}

Expand All @@ -398,7 +410,7 @@ protected function buildLayout(): void
return;
}

if (view()->exists($this->layout) || view()->exists('components.'.$this->layout)) {
if (view()->exists($this->layout) || view()->exists('components.' . $this->layout)) {
return;
}

Expand Down Expand Up @@ -511,7 +523,7 @@ protected function modelReplacements(): array
$rulesArray = Arr::except($rulesArray, $this->unwantedColumns);
// Make rulesArray
foreach ($rulesArray as $col => $rule) {
$rules .= "\n\t\t\t'$col' => '".implode('|', $rule)."',";
$rules .= "\n\t\t\t'$col' => '" . implode('|', $rule) . "',";
}

return $rules;
Expand All @@ -523,7 +535,7 @@ protected function modelReplacements(): array

// Add quotes to the unwanted columns for fillable
array_walk($filterColumns, function (&$value) {
$value = "'".$value."'";
$value = "'" . $value . "'";
});

// CSV format
Expand Down Expand Up @@ -602,10 +614,10 @@ protected function requireComposerPackages(array $packages, bool $asDev = false)
);

return (new Process($command, base_path(), ['COMPOSER_MEMORY_LIMIT' => '-1']))
->setTimeout(null)
->run(function ($type, $output) {
$this->output->write($output);
}) === 0;
->setTimeout(null)
->run(function ($type, $output) {
$this->output->write($output);
}) === 0;
}

/**
Expand All @@ -622,12 +634,12 @@ protected function runCommands(array $commands): void
try {
$process->setTty(true);
} catch (RuntimeException $e) {
$this->output->writeln(' <bg=yellow;fg=black> WARN </> '.$e->getMessage().PHP_EOL);
$this->output->writeln(' <bg=yellow;fg=black> WARN </> ' . $e->getMessage() . PHP_EOL);
}
}

$process->run(function ($type, $line) {
$this->output->write(' '.$line);
$this->output->write(' ' . $line);
});
}
}
Loading