forked from Lomkit/laravel-rest-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuickStartCommand.php
122 lines (103 loc) · 3.31 KB
/
QuickStartCommand.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<?php
namespace Lomkit\Rest\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Str;
use Lomkit\Rest\Console\ResolvesStubPath;
class QuickStartCommand extends Command
{
use ResolvesStubPath;
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'rest:quick-start';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Demonstrate the app using user related resource registering.';
/**
* Execute the console command.
*
* @return void
*/
public function handle()
{
$this->comment('Generating User Resource...');
$this->callSilent('rest:resource', ['name' => 'UserResource']);
$this->comment('Generating User Controller...');
$this->callSilent('rest:controller', ['name' => 'UsersController']);
$this->updateUserModelNamespace();
$this->setAppNamespace();
$this->updateApiRoutes();
$this->info('Laravel Rest Api is ready. Type \'php artisan route:list\' to see your new routes !');
}
/**
* Update the User model namespace in the generated files.
*
* @return void
*/
protected function updateUserModelNamespace()
{
$resource = app_path('Rest/Resources/UserResource.php');
if (file_exists(app_path('Models/User.php'))) {
file_put_contents(
$resource,
str_replace('App\Models\Model::class', 'App\Models\User::class', file_get_contents($resource))
);
}
$controller = app_path('Rest/Controllers/UsersController.php');
if (file_exists(app_path('Models/User.php'))) {
file_put_contents(
$controller,
str_replace('App\Rest\Resources\ModelResource::class', 'App\Rest\Resources\UserResource::class', file_get_contents($controller))
);
}
}
/**
* Set the proper application namespace on the installed files.
*
* @return void
*/
protected function setAppNamespace()
{
$namespace = $this->laravel->getNamespace();
$this->setAppNamespaceOn(app_path('Rest/Resources/UserResource.php'), $namespace);
$this->setAppNamespaceOn(app_path('Rest/Controllers/UsersController.php'), $namespace);
}
/**
* Set the namespace on the given file.
*
* @param string $file
* @param string $namespace
*
* @return void
*/
protected function setAppNamespaceOn($file, $namespace)
{
file_put_contents($file, str_replace(
'App\\',
$namespace,
file_get_contents($file)
));
}
/**
* Update the api routes file to include the new resource.
*
* @return void
*/
protected function updateApiRoutes()
{
$routesPath = base_path('routes/api.php');
if (!file_exists($routesPath)) {
file_put_contents($routesPath, '<?php');
}
$routeContent = file_get_contents($routesPath);
$newRoute = "\Lomkit\Rest\Facades\Rest::resource('users', \App\Rest\Controllers\UsersController::class);";
if (!Str::contains($routeContent, $newRoute)) {
file_put_contents($routesPath, $routeContent.PHP_EOL.$newRoute);
}
}
}