1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Thinkstudeo\Rakshak\Console; |
4
|
|
|
|
5
|
|
|
use Illuminate\Console\Command; |
6
|
|
|
use Illuminate\Console\DetectsApplicationNamespace; |
7
|
|
|
use Illuminate\Filesystem\Filesystem; |
8
|
|
|
use Illuminate\Support\Str; |
9
|
|
|
|
10
|
|
|
class InstallCommand extends Command |
11
|
|
|
{ |
12
|
|
|
use DetectsApplicationNamespace; |
13
|
|
|
/** |
14
|
|
|
* The signature required to run the command from cli. |
15
|
|
|
* |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
protected $signature = 'rakshak:install |
19
|
|
|
{--views : Only scaffold the authentication views} |
20
|
|
|
{--force : Overwrite existing views by default}'; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* What the command does. |
24
|
|
|
* |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
protected $description = 'Install Rakshak package for Laravel'; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Handle the execution of the command. |
31
|
|
|
* |
32
|
|
|
* @return void |
33
|
|
|
*/ |
34
|
|
|
public function handle() |
35
|
|
|
{ |
36
|
|
|
$this->comment('Publishing Rakshak Resources and Assets...'); |
37
|
|
|
$this->callSilent('rakshak:publish', ['--force' => $this->option('force') ? true : false]); |
38
|
|
|
$this->createDirectories(); |
39
|
|
|
|
40
|
|
|
$this->comment('Making Auth Routes, Controllers and Views...'); |
41
|
|
|
$this->exportRoutes(); |
42
|
|
|
$this->makeControllers(); |
43
|
|
|
$this->exportViews(); |
44
|
|
|
|
45
|
|
|
$this->comment('Making Rakshak Notifications...'); |
46
|
|
|
$this->exportNotifications(); |
47
|
|
|
|
48
|
|
|
$this->comment('Updating the User model...'); |
49
|
|
|
$this->updateUserModel(); |
50
|
|
|
|
51
|
|
|
$this->info('Rakshak scaffolding installed successfully.'); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Copy the Authentication routes to the Laravel App. |
56
|
|
|
* |
57
|
|
|
* @return void |
58
|
|
|
*/ |
59
|
|
|
protected function exportRoutes() |
60
|
|
|
{ |
61
|
|
|
if ( |
62
|
|
|
! Str::contains(file_get_contents(base_path('routes/web.php')), 'Auth::routes') || |
63
|
|
|
$this->option('force') |
64
|
|
|
) { |
65
|
|
|
file_put_contents( |
66
|
|
|
base_path('routes/web.php'), |
67
|
|
|
file_get_contents(__DIR__.'/stubs/routes.stub'), |
68
|
|
|
FILE_APPEND |
69
|
|
|
); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Controllers to be copied over to the main app. |
75
|
|
|
* |
76
|
|
|
* @var array |
77
|
|
|
*/ |
78
|
|
|
protected $controllers = [ |
79
|
|
|
'RegisterController.stub' => 'Http/Controllers/Auth/RegisterController.php', |
80
|
|
|
'LoginController.stub' => 'Http/Controllers/Auth/LoginController.php', |
81
|
|
|
'HomeController.stub' => 'Http/Controllers/HomeController.php', |
82
|
|
|
]; |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Copy the controllers to the main app. |
86
|
|
|
* |
87
|
|
|
* @return void |
88
|
|
|
*/ |
89
|
|
|
protected function makeControllers() |
90
|
|
|
{ |
91
|
|
|
foreach ($this->controllers as $key => $value) { |
92
|
|
|
if (! file_exists(app_path($value)) || $this->option('force')) { |
93
|
|
|
file_put_contents( |
94
|
|
|
app_path($value), |
95
|
|
|
$this->compileStub(__DIR__."/stubs/controllers/{$key}") |
96
|
|
|
); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Compiles the stub. |
103
|
|
|
* |
104
|
|
|
* @return string |
105
|
|
|
*/ |
106
|
|
|
protected function compileStub($stubPath) |
107
|
|
|
{ |
108
|
|
|
return str_replace( |
109
|
|
|
'{{namespace}}', |
110
|
|
|
$this->getAppNamespace(), |
111
|
|
|
file_get_contents($stubPath) |
112
|
|
|
); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Create the directories for the files. |
117
|
|
|
* |
118
|
|
|
* @return void |
119
|
|
|
*/ |
120
|
|
|
protected function createDirectories() |
121
|
|
|
{ |
122
|
|
|
if (! is_dir($directory = resource_path('views/layouts'))) { |
123
|
|
|
mkdir($directory, 0755, true); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
if (! is_dir($directory = resource_path('views/auth/passwords'))) { |
127
|
|
|
mkdir($directory, 0755, true); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
if (! is_dir($directory = app_path('Notifications/Rakshak'))) { |
131
|
|
|
mkdir($directory, 0755, true); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* The views that need to be exported. |
137
|
|
|
* |
138
|
|
|
* @var array |
139
|
|
|
*/ |
140
|
|
|
protected $views = [ |
141
|
|
|
'auth/login.stub' => 'auth/login.blade.php', |
142
|
|
|
'auth/register.stub' => 'auth/register.blade.php', |
143
|
|
|
'auth/verify.stub' => 'auth/verify.blade.php', |
144
|
|
|
'auth/passwords/email.stub' => 'auth/passwords/email.blade.php', |
145
|
|
|
'auth/passwords/reset.stub' => 'auth/passwords/reset.blade.php', |
146
|
|
|
'layouts/app.stub' => 'layouts/app.blade.php', |
147
|
|
|
'home.stub' => 'home.blade.php', |
148
|
|
|
]; |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Export the authentication views. |
152
|
|
|
* |
153
|
|
|
* @return void |
154
|
|
|
*/ |
155
|
|
|
protected function exportViews() |
156
|
|
|
{ |
157
|
|
|
foreach ($this->views as $key => $value) { |
158
|
|
|
if (file_exists($view = resource_path('views/'.$value)) && ! $this->option('force')) { |
159
|
|
|
if (! $this->confirm("The [{$value}] view already exists. Do you want to replace it?")) { |
160
|
|
|
continue; |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
copy( |
165
|
|
|
__DIR__.'/stubs/views/'.$key, |
166
|
|
|
$view |
167
|
|
|
); |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Copy the notifications to the main app, to allow users to customize the notifications. |
173
|
|
|
* |
174
|
|
|
* @return void |
175
|
|
|
*/ |
176
|
|
|
public function exportNotifications() |
177
|
|
|
{ |
178
|
|
|
$stubs = (new Filesystem)->files(__DIR__.'/stubs/notifications/'); |
179
|
|
|
|
180
|
|
|
foreach ($stubs as $stub) { |
181
|
|
|
$filename = str_replace('stub', 'php', basename($stub)); |
182
|
|
|
$path = app_path("Notifications/Rakshak/{$filename}"); |
183
|
|
|
|
184
|
|
|
if (! file_exists($path) || $this->option('force')) { |
185
|
|
|
file_put_contents( |
186
|
|
|
$path, |
187
|
|
|
$this->compileStub($stub->getPathname()) |
188
|
|
|
); |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Replace the default User model. |
195
|
|
|
* |
196
|
|
|
* @return void |
197
|
|
|
*/ |
198
|
|
|
public function updateUserModel() |
199
|
|
|
{ |
200
|
|
|
$stubPath = __DIR__.'/stubs/User.stub'; |
201
|
|
|
$namespace = trim(str_replace( |
202
|
|
|
class_basename(config('auth.providers.users.model')), |
203
|
|
|
'', |
204
|
|
|
config('auth.providers.users.model') |
205
|
|
|
), '\\'); |
206
|
|
|
if ( |
207
|
|
|
! str_contains(file_get_contents(app_path('User.php')), 'HasRakshak') || |
208
|
|
|
$this->option('force') |
209
|
|
|
) { |
210
|
|
|
file_put_contents( |
211
|
|
|
app_path('User.php'), |
212
|
|
|
str_replace( |
213
|
|
|
'{{namespace}}', |
214
|
|
|
$namespace, |
215
|
|
|
file_get_contents($stubPath) |
216
|
|
|
) |
217
|
|
|
); |
218
|
|
|
} |
219
|
|
|
} |
220
|
|
|
} |
221
|
|
|
|