Skip to content

Commit 675c5f5

Browse files
committed
Added Crud starter generator, fix log permission issue
1 parent 05a3c64 commit 675c5f5

File tree

7 files changed

+175
-7
lines changed

7 files changed

+175
-7
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,18 @@ php artisan make:dto UserDTO
127127

128128
The newly created DTO will be located at `app/Http/DTOs/UserDTO.php`, ready to Transfer your data across the application.
129129

130+
131+
### Create CRUD Starter
132+
133+
Generate all the necessary boilerplate files for a specific entity (such as model, controller, routes, resource, request, service, etc.) using the following command:
134+
135+
136+
```bash
137+
php artisan make:crud Test
138+
```
139+
140+
This command will create all the required boilerplate files for the `Test` entity.
141+
130142
Leverage these Artisan commands to streamline your development process and maintain a well-structured codebase.
131143

132144
## Authors
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
namespace App\Console\Commands;
4+
5+
use Illuminate\Console\Command;
6+
use Illuminate\Support\Facades\Artisan;
7+
8+
class GenerateCrudStarter extends Command
9+
{
10+
protected $signature = 'make:crud {name}';
11+
protected $description = 'Create a new CRUD starter setup for a given entity';
12+
13+
public function __construct()
14+
{
15+
parent::__construct();
16+
}
17+
18+
public function handle()
19+
{
20+
$name = $this->argument('name');
21+
22+
$this->info("Creating CRUD for {$name}");
23+
24+
$tasks = [
25+
'migration' => ['make:migration', ['name' => "create_{$name}_table"]],
26+
'model' => ['make:model', ['name' => $name]],
27+
'controller' => [
28+
'make:controller',
29+
[
30+
'name' => "Api/V1/{$name}/{$name}Controller",
31+
'--api' => true,
32+
'--model' => $name
33+
]
34+
],
35+
'request' => ['make:request', ['name' => "{$name}/{$name}InsertUpdateRequest"]],
36+
'resource' => ['make:resource', ['name' => "{$name}/{$name}Resource"]],
37+
'service' => ['make:service', ['service' => "{$name}/{$name}Service"]],
38+
'dto' => ['make:dto', ['dto' => "{$name}DTO"]],
39+
'route' => ['make:route', ['name' => $name]]
40+
];
41+
42+
foreach ($tasks as $taskName => $task) {
43+
[$command, $arguments] = $task;
44+
if ($this->callArtisanCommand($taskName, $command, $arguments)) {
45+
return;
46+
}
47+
}
48+
49+
$this->info('CRUD starter created successfully!');
50+
}
51+
52+
private function callArtisanCommand($taskName, $command, $arguments)
53+
{
54+
try {
55+
$exitCode = Artisan::call($command, $arguments);
56+
$this->line(Artisan::output());
57+
return $exitCode !== 0;
58+
} catch (\Exception $e) {
59+
$this->error("Error creating {$taskName}: {$e->getMessage()}");
60+
return true;
61+
}
62+
}
63+
}

app/Console/Commands/MakeDTOCommand.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -189,14 +189,12 @@ public function handle()
189189

190190
if (File::exists($path)) {
191191
$this->error("File {$path} already exists!");
192-
exit(1);
192+
return 1;
193193
}
194194

195195
File::put($path, $fileContents);
196196
$this->info("DTO generated successfully! path : {$path}");
197-
exit(0);
198-
199-
//return 0;
197+
return 0;
200198

201199
}
202200
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
3+
namespace App\Console\Commands;
4+
5+
use Illuminate\Console\Command;
6+
use Illuminate\Support\Facades\File;
7+
use Illuminate\Support\Str;
8+
9+
class MakeRouteFileCommand extends Command
10+
{
11+
protected $signature = 'make:route {name}';
12+
protected $description = 'Create a new route file for a given entity';
13+
14+
public function __construct()
15+
{
16+
parent::__construct();
17+
}
18+
19+
/**
20+
* Create view directory if not exists.
21+
*
22+
* @param $path
23+
*/
24+
public function createDir($path)
25+
{
26+
$dir = dirname($path);
27+
28+
if (!file_exists($dir)) {
29+
mkdir($dir, 0777, true);
30+
}
31+
}
32+
33+
34+
/**
35+
* getStubFilePath
36+
*
37+
* @return string
38+
*/
39+
protected function getStubFilePath()
40+
{
41+
$stub = '/stubs/routes.stub';
42+
return $stub;
43+
}
44+
45+
protected function getStub()
46+
{
47+
return file_get_contents(__DIR__ . $this->getStubFilePath());
48+
49+
}
50+
51+
public function handle()
52+
{
53+
$name = Str::studly($this->argument('name'));
54+
$snakeName = Str::snake($name);
55+
// $pluralName = Str::plural(Str::snake($name));
56+
$controller = "{$name}Controller";
57+
$controllerPath = "Api\V1\\{$name}\\{$name}Controller";
58+
$path = base_path("routes/api/v1/{$name}/{$name}Routes.php");
59+
60+
$this->createDir($path);
61+
62+
if (File::exists($path)) {
63+
$this->error("File {$path} already exists!");
64+
return 1;
65+
}
66+
67+
$stub = $this->getStub();
68+
$content = str_replace(
69+
['$CONTROLLER$', '$NAME$', '$CONTROLLER_PATH$'],
70+
[$controller, $snakeName, $controllerPath],
71+
$stub
72+
);
73+
74+
File::put($path, $content);
75+
76+
$this->info("Route file created successfully! Path: {$path}");
77+
78+
return 0;
79+
}
80+
81+
}

app/Console/Commands/MakeServiceCommand.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class MakeServiceCommand extends Command
3838
* Get command agrumants - EX : UserService
3939
* getArguments
4040
*
41-
* @return array<int|string>[]
41+
* @return array
4242
*/
4343
protected function getArguments()
4444
{
@@ -208,11 +208,11 @@ public function handle()
208208

209209
if (File::exists($path)) {
210210
$this->error("File {$path} already exists!");
211-
exit(1);
211+
return 1;
212212
}
213213

214214
File::put($path, $fileContents);
215215
$this->info("Service generated successfully! path : {$path}");
216-
exit(0);
216+
return 0;
217217
}
218218
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
use App\Http\Controllers\$CONTROLLER_PATH$;
4+
5+
Route::group(['prefix' => 'v1', 'middleware' => ['auth:sanctum']], function () {
6+
Route::get('/$NAME$', [$CONTROLLER$::class, 'index']);
7+
Route::get('/$NAME$/{id}', [$CONTROLLER$::class, 'show']);
8+
Route::post('/$NAME$', [$CONTROLLER$::class, 'store']);
9+
Route::post('/$NAME$/update', [$CONTROLLER$::class, 'update']);
10+
Route::delete('/$NAME$/{id}', [$CONTROLLER$::class, 'destroy']);
11+
// Add more routes as needed
12+
});

config/logging.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
'path' => storage_path('logs/laravel.log'),
6464
'level' => env('LOG_LEVEL', 'debug'),
6565
'replace_placeholders' => true,
66+
'permission' => 0666,
6667
],
6768

6869
'daily' => [
@@ -71,6 +72,7 @@
7172
'level' => env('LOG_LEVEL', 'debug'),
7273
'days' => 14,
7374
'replace_placeholders' => true,
75+
'permission' => 0666,
7476
],
7577

7678
'slack' => [

0 commit comments

Comments
 (0)