-
Notifications
You must be signed in to change notification settings - Fork 476
/
Copy pathAuthCommand.php
164 lines (143 loc) · 4.52 KB
/
AuthCommand.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?php
namespace Laravel\Ui;
use Illuminate\Console\Command;
use InvalidArgumentException;
class AuthCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'ui:auth
{ type=bootstrap : The preset type (bootstrap) }
{--views : Only scaffold the authentication views}
{--force : Overwrite existing views by default}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Scaffold basic login and registration views and routes';
/**
* The views that need to be exported.
*
* @var array
*/
protected $views = [
'auth/login.stub' => 'auth/login.blade.php',
'auth/passwords/confirm.stub' => 'auth/passwords/confirm.blade.php',
'auth/passwords/email.stub' => 'auth/passwords/email.blade.php',
'auth/passwords/reset.stub' => 'auth/passwords/reset.blade.php',
'auth/register.stub' => 'auth/register.blade.php',
'auth/verify.stub' => 'auth/verify.blade.php',
'home.stub' => 'home.blade.php',
'layouts/app.stub' => 'layouts/app.blade.php',
];
/**
* Execute the console command.
*
* @return void
*
* @throws \InvalidArgumentException
*/
public function handle()
{
if (static::hasMacro($this->argument('type'))) {
return call_user_func(static::$macros[$this->argument('type')], $this);
}
if (! in_array($this->argument('type'), ['bootstrap'])) {
throw new InvalidArgumentException('Invalid preset.');
}
$this->ensureDirectoriesExist();
$this->exportViews();
if (! $this->option('views')) {
$this->exportBackend();
}
$this->info('Authentication scaffolding generated successfully.');
}
/**
* Create the directories for the files.
*
* @return void
*/
protected function ensureDirectoriesExist()
{
if (! is_dir($directory = $this->getViewPath('layouts'))) {
mkdir($directory, 0755, true);
}
if (! is_dir($directory = $this->getViewPath('auth/passwords'))) {
mkdir($directory, 0755, true);
}
}
/**
* Export the authentication views.
*
* @return void
*/
protected function exportViews()
{
foreach ($this->views as $key => $value) {
if (file_exists($view = $this->getViewPath($value)) && ! $this->option('force')) {
if (! $this->confirm("The [{$value}] view already exists. Do you want to replace it?")) {
continue;
}
}
copy(
__DIR__.'/Auth/'.$this->argument('type').'-stubs/'.$key,
$view
);
}
}
/**
* Export the authentication backend.
*
* @return void
*/
protected function exportBackend()
{
$this->callSilent('ui:controllers');
$controller = app_path('Http/Controllers/HomeController.php');
if (file_exists($controller) && ! $this->option('force')) {
if ($this->confirm("The [HomeController.php] file already exists. Do you want to replace it?")) {
file_put_contents($controller, $this->compileControllerStub());
}
} else {
file_put_contents($controller, $this->compileControllerStub());
}
file_put_contents(
base_path('routes/web.php'),
file_get_contents(__DIR__.'/Auth/stubs/routes.stub'),
FILE_APPEND
);
copy(
__DIR__.'/../stubs/migrations/2014_10_12_100000_create_password_resets_table.php',
base_path('database/migrations/2014_10_12_100000_create_password_resets_table.php')
);
}
/**
* Compiles the "HomeController" stub.
*
* @return string
*/
protected function compileControllerStub()
{
return str_replace(
'{{namespace}}',
$this->laravel->getNamespace(),
file_get_contents(__DIR__.'/Auth/stubs/controllers/HomeController.stub')
);
}
/**
* Get full view path relative to the application's configured view path.
*
* @param string $path
* @return string
*/
protected function getViewPath($path)
{
return implode(DIRECTORY_SEPARATOR, [
config('view.paths')[0] ?? resource_path('views'), $path,
]);
}
}