|
3 | 3 | namespace Lomkit\Rest\Console\Commands;
|
4 | 4 |
|
5 | 5 | use Illuminate\Console\Command;
|
| 6 | +use Illuminate\Support\Str; |
6 | 7 | use Lomkit\Rest\Console\ResolvesStubPath;
|
7 | 8 |
|
8 | 9 | class QuickStartCommand extends Command
|
@@ -32,37 +33,41 @@ public function handle()
|
32 | 33 | {
|
33 | 34 | $this->comment('Generating User Resource...');
|
34 | 35 | $this->callSilent('rest:resource', ['name' => 'UserResource']);
|
35 |
| - copy($this->resolveStubPath('/stubs/user-resource.stub'), app_path('Rest/Resources/UserResource.php')); |
36 |
| - |
37 |
| - if (file_exists(app_path('Models/User.php'))) { |
38 |
| - file_put_contents( |
39 |
| - app_path('Rest/Resources/UserResource.php'), |
40 |
| - str_replace('App\User::class', 'App\Models\User::class', file_get_contents(app_path('Rest/Resources/UserResource.php'))) |
41 |
| - ); |
42 |
| - } |
43 | 36 |
|
44 | 37 | $this->comment('Generating User Controller...');
|
45 | 38 | $this->callSilent('rest:controller', ['name' => 'UsersController']);
|
46 |
| - copy($this->resolveStubPath('/stubs/user-controller.stub'), app_path('Rest/Controllers/UsersController.php')); |
| 39 | + |
| 40 | + $this->updateUserModelNamespace(); |
| 41 | + $this->setAppNamespace(); |
| 42 | + $this->updateApiRoutes(); |
| 43 | + |
| 44 | + $this->info('Laravel Rest Api is ready. Type \'php artisan route:list\' to see your new routes !'); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Update the User model namespace in the generated files. |
| 49 | + * |
| 50 | + * @return void |
| 51 | + */ |
| 52 | + protected function updateUserModelNamespace() |
| 53 | + { |
| 54 | + $resource = app_path('Rest/Resources/UserResource.php'); |
47 | 55 |
|
48 | 56 | if (file_exists(app_path('Models/User.php'))) {
|
49 | 57 | file_put_contents(
|
50 |
| - app_path('Rest/Controllers/UsersController.php'), |
51 |
| - str_replace('App\User::class', 'App\Models\User::class', file_get_contents(app_path('Rest/Controllers/UsersController.php'))) |
| 58 | + $resource, |
| 59 | + str_replace('App\Models\Model::class', 'App\Models\User::class', file_get_contents($resource)) |
52 | 60 | );
|
53 | 61 | }
|
54 | 62 |
|
55 |
| - $this->setAppNamespace(); |
| 63 | + $controller = app_path('Rest/Controllers/UsersController.php'); |
56 | 64 |
|
57 |
| - if (file_exists(base_path('routes/api.php'))) { |
| 65 | + if (file_exists(app_path('Models/User.php'))) { |
58 | 66 | file_put_contents(
|
59 |
| - base_path('routes/api.php'), |
60 |
| - file_get_contents(base_path('routes/api.php')). |
61 |
| - '\Lomkit\Rest\Facades\Rest::resource(\'users\', \App\Rest\Controllers\UsersController::class);' |
| 67 | + $controller, |
| 68 | + str_replace('App\Rest\Resources\ModelResource::class', 'App\Rest\Resources\UserResource::class', file_get_contents($controller)) |
62 | 69 | );
|
63 | 70 | }
|
64 |
| - |
65 |
| - $this->info('Laravel Rest Api is ready. Type \'php artisan route:list\' to see your new routes !'); |
66 | 71 | }
|
67 | 72 |
|
68 | 73 | /**
|
@@ -94,4 +99,24 @@ protected function setAppNamespaceOn($file, $namespace)
|
94 | 99 | file_get_contents($file)
|
95 | 100 | ));
|
96 | 101 | }
|
| 102 | + |
| 103 | + /** |
| 104 | + * Update the api routes file to include the new resource. |
| 105 | + * |
| 106 | + * @return void |
| 107 | + */ |
| 108 | + protected function updateApiRoutes() |
| 109 | + { |
| 110 | + $routesPath = base_path('routes/api.php'); |
| 111 | + if (!file_exists($routesPath)) { |
| 112 | + file_put_contents($routesPath, '<?php'); |
| 113 | + } |
| 114 | + |
| 115 | + $routeContent = file_get_contents($routesPath); |
| 116 | + $newRoute = "\Lomkit\Rest\Facades\Rest::resource('users', \App\Rest\Controllers\UsersController::class);"; |
| 117 | + |
| 118 | + if (!Str::contains($routeContent, $newRoute)) { |
| 119 | + file_put_contents($routesPath, $routeContent.PHP_EOL.$newRoute); |
| 120 | + } |
| 121 | + } |
97 | 122 | }
|
0 commit comments