|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Lomkit\Rest\Console\Commands; |
| 4 | + |
| 5 | +use Illuminate\Console\Command; |
| 6 | +use Illuminate\Support\Str; |
| 7 | +use Lomkit\Rest\Console\ResolvesStubPath; |
| 8 | + |
| 9 | +class QuickStartCommand extends Command |
| 10 | +{ |
| 11 | + use ResolvesStubPath; |
| 12 | + |
| 13 | + /** |
| 14 | + * The name and signature of the console command. |
| 15 | + * |
| 16 | + * @var string |
| 17 | + */ |
| 18 | + protected $signature = 'rest:quick-start'; |
| 19 | + |
| 20 | + /** |
| 21 | + * The console command description. |
| 22 | + * |
| 23 | + * @var string |
| 24 | + */ |
| 25 | + protected $description = 'Demonstrate the app using user related resource registering.'; |
| 26 | + |
| 27 | + /** |
| 28 | + * Execute the console command. |
| 29 | + * |
| 30 | + * @return void |
| 31 | + */ |
| 32 | + public function handle() |
| 33 | + { |
| 34 | + $this->comment('Generating User Resource...'); |
| 35 | + $this->callSilent('rest:resource', ['name' => 'UserResource']); |
| 36 | + copy($this->resolveStubPath('../stubs/user-resource.stub'), app_path('Rest/Resources/UserResource.php')); |
| 37 | + |
| 38 | + if (file_exists(app_path('Models/User.php'))) { |
| 39 | + file_put_contents( |
| 40 | + app_path('Rest/Resources/UserResource.php'), |
| 41 | + str_replace('App\User::class', 'App\Models\User::class', file_get_contents(app_path('Rest/Resources/UserResource.php'))) |
| 42 | + ); |
| 43 | + } |
| 44 | + |
| 45 | + $this->comment('Generating User Controller...'); |
| 46 | + $this->callSilent('rest:controller', ['name' => 'UsersController']); |
| 47 | + copy($this->resolveStubPath('../stubs/user-controller.stub'), app_path('Http/Controllers/UsersController.php')); |
| 48 | + |
| 49 | + if (file_exists(app_path('Models/User.php'))) { |
| 50 | + file_put_contents( |
| 51 | + app_path('Http/Controllers/UsersController.php'), |
| 52 | + str_replace('App\User::class', 'App\Models\User::class', file_get_contents(app_path('Http/Controllers/UsersController.php'))) |
| 53 | + ); |
| 54 | + } |
| 55 | + |
| 56 | + $this->setAppNamespace(); |
| 57 | + |
| 58 | + if (file_exists(base_path('routes/api.php'))) { |
| 59 | + file_put_contents( |
| 60 | + base_path('routes/api.php'), |
| 61 | + file_get_contents(base_path('routes/api.php')). |
| 62 | + '\Lomkit\Rest\Facades\Rest::resource(\'users\', \App\Http\Controllers\UsersController::class);' |
| 63 | + ); |
| 64 | + } |
| 65 | + |
| 66 | + $this->info('Laravel Rest Api is ready. Type \'php artisan route:list\' to see your new routes !'); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Set the proper application namespace on the installed files. |
| 71 | + * |
| 72 | + * @return void |
| 73 | + */ |
| 74 | + protected function setAppNamespace() |
| 75 | + { |
| 76 | + $namespace = $this->laravel->getNamespace(); |
| 77 | + |
| 78 | + $this->setAppNamespaceOn(app_path('Rest/Resource/UserResource.php'), $namespace); |
| 79 | + $this->setAppNamespaceOn(app_path('Http/Controllers/UsersController.php'), $namespace); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Set the namespace on the given file. |
| 84 | + * |
| 85 | + * @param string $file |
| 86 | + * @param string $namespace |
| 87 | + * @return void |
| 88 | + */ |
| 89 | + protected function setAppNamespaceOn($file, $namespace) |
| 90 | + { |
| 91 | + file_put_contents($file, str_replace( |
| 92 | + 'App\\', |
| 93 | + $namespace, |
| 94 | + file_get_contents($file) |
| 95 | + )); |
| 96 | + } |
| 97 | +} |
0 commit comments