|
|
@@ -4,6 +4,7 @@
|
|
|
|
|
|
use Illuminate\Console\Command;
|
|
|
use Illuminate\Filesystem\Filesystem;
|
|
|
+use Illuminate\Support\Str;
|
|
|
|
|
|
class CreateCrud extends Command
|
|
|
{
|
|
|
@@ -30,14 +31,19 @@ public function handle(): void
|
|
|
$this->createRequest(name: $name);
|
|
|
$this->createResource(name: $name);
|
|
|
$this->updateAppServiceProvider(name: $name);
|
|
|
+ $this->createRoute(name: $name);
|
|
|
+ $this->createMigration(name: $name);
|
|
|
$this->info(string: 'CRUD operations created successfully!');
|
|
|
}
|
|
|
|
|
|
protected function createModel($name): void
|
|
|
{
|
|
|
+
|
|
|
+ $tableNameSnakeCase = Str::snake(Str::plural($name));
|
|
|
+
|
|
|
$modelTemplate = str_replace(
|
|
|
- search: ['{{modelName}}'],
|
|
|
- replace: [$name],
|
|
|
+ search: ['{{modelName}}', '{{tableNameSnakeCase}}'],
|
|
|
+ replace: [$name, $tableNameSnakeCase],
|
|
|
subject: $this->getStub(type: 'Model')
|
|
|
);
|
|
|
|
|
|
@@ -145,6 +151,48 @@ protected function updateAppServiceProvider($name): void
|
|
|
$this->files->put(path: $providerPath, contents: $content);
|
|
|
}
|
|
|
|
|
|
+ protected function createRoute($name): void
|
|
|
+ {
|
|
|
+ $routeTemplate = str_replace(
|
|
|
+ search: [
|
|
|
+ '{{modelName}}',
|
|
|
+ '{{modelNameSnakeCase}}'
|
|
|
+ ],
|
|
|
+ replace: [
|
|
|
+ $name,
|
|
|
+ Str::snake($name)
|
|
|
+ ],
|
|
|
+ subject: $this->getStub(type: 'Route')
|
|
|
+ );
|
|
|
+
|
|
|
+ $routePath = base_path("routes/authRoutes/" . strtolower($name) . ".php");
|
|
|
+
|
|
|
+ if (!$this->files->isDirectory(base_path('routes/authRoutes'))) {
|
|
|
+ $this->files->makeDirectory(base_path('routes/authRoutes'), 0755, true);
|
|
|
+ }
|
|
|
+
|
|
|
+ $this->put(path: $routePath, contents: $routeTemplate);
|
|
|
+ }
|
|
|
+
|
|
|
+ protected function createMigration($name): void
|
|
|
+ {
|
|
|
+ $tableNameSnakeCase = Str::snake(Str::plural($name));
|
|
|
+
|
|
|
+ $migrationTemplate = str_replace(
|
|
|
+ search: ['{{tableNameSnakeCase}}'],
|
|
|
+ replace: [$tableNameSnakeCase],
|
|
|
+ subject: $this->getStub(type: 'Migration')
|
|
|
+ );
|
|
|
+
|
|
|
+ $timestamp = date('Y_m_d_His');
|
|
|
+ $filename = $timestamp . "_create_{$tableNameSnakeCase}_table.php";
|
|
|
+
|
|
|
+ $this->put(
|
|
|
+ path: database_path("migrations/{$filename}"),
|
|
|
+ contents: $migrationTemplate
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
protected function getStub($type): string
|
|
|
{
|
|
|
return $this->files->get(path: storage_path(path: "stubs/{$type}.stub"));
|