-
Notifications
You must be signed in to change notification settings - Fork 529
/
Copy pathInstallsApiStack.php
109 lines (86 loc) · 3.63 KB
/
InstallsApiStack.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
<?php
namespace Laravel\Breeze\Console;
use Illuminate\Filesystem\Filesystem;
trait InstallsApiStack
{
/**
* Install the API Breeze stack.
*
* @return int|null
*/
protected function installApiStack()
{
$this->runCommands([
$this->option('oauth') ? 'php artisan install:api --passport' : 'php artisan install:api',
]);
$files = new Filesystem;
// Controllers...
$files->ensureDirectoryExists(app_path('Http/Controllers/Auth'));
$files->copyDirectory(__DIR__.'/../../stubs/api/app/Http/Controllers/Auth', app_path('Http/Controllers/Auth'));
// Middleware...
$files->copyDirectory(__DIR__.'/../../stubs/api/app/Http/Middleware', app_path('Http/Middleware'));
$this->installMiddlewareAliases([
'verified' => '\App\Http\Middleware\EnsureEmailIsVerified::class',
]);
if ($this->option('oauth')) {
$this->installMiddleware([
'\Laravel\Passport\Http\Middleware\CreateFreshApiToken::class',
], 'web', 'append');
} else {
$this->installMiddleware([
'\Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful::class',
], 'api', 'prepend');
}
// Requests...
$files->ensureDirectoryExists(app_path('Http/Requests/Auth'));
$files->copyDirectory(__DIR__.'/../../stubs/api/app/Http/Requests/Auth', app_path('Http/Requests/Auth'));
// Providers...
$files->copyDirectory(__DIR__.'/../../stubs/api/app/Providers', app_path('Providers'));
// Routes...
copy(__DIR__.'/../../stubs/api/routes/api.php', base_path('routes/api.php'));
copy(__DIR__.'/../../stubs/api/routes/web.php', base_path('routes/web.php'));
copy(__DIR__.'/../../stubs/api/routes/auth.php', base_path('routes/auth.php'));
// OAuth...
if ($this->option('oauth')) {
$this->replaceInFile('auth:sanctum', 'auth:api', base_path('routes/api.php'));
}
// Configuration...
$files->copyDirectory(__DIR__.'/../../stubs/api/config', config_path());
// Environment...
if (! $files->exists(base_path('.env'))) {
copy(base_path('.env.example'), base_path('.env'));
}
file_put_contents(
base_path('.env'),
preg_replace('/APP_URL=(.*)/', 'APP_URL=http://localhost:8000'.PHP_EOL.'FRONTEND_URL=http://localhost:3000', file_get_contents(base_path('.env')))
);
// Tests...
if (! $this->installTests()) {
return 1;
}
$files->delete(base_path('tests/Feature/Auth/PasswordConfirmationTest.php'));
// Cleaning...
$this->removeScaffoldingUnnecessaryForApis();
$this->components->info('Breeze scaffolding installed successfully.');
}
/**
* Remove any application scaffolding that isn't needed for APIs.
*
* @return void
*/
protected function removeScaffoldingUnnecessaryForApis()
{
$files = new Filesystem;
// Remove frontend related files...
$files->delete(base_path('package.json'));
$files->delete(base_path('vite.config.js'));
$files->delete(base_path('tailwind.config.js'));
$files->delete(base_path('postcss.config.js'));
// Remove Laravel "welcome" view...
$files->delete(resource_path('views/welcome.blade.php'));
$files->put(resource_path('views/.gitkeep'), PHP_EOL);
// Remove CSS and JavaScript directories...
$files->deleteDirectory(resource_path('css'));
$files->deleteDirectory(resource_path('js'));
}
}