1 | <?php |
||||
2 | |||||
3 | namespace BukanKalengKaleng\LaravelRebuild\Console\Commands; |
||||
4 | |||||
5 | use Illuminate\Console\Command; |
||||
6 | use Illuminate\Support\Composer; |
||||
7 | |||||
8 | class Rebuild extends Command |
||||
9 | { |
||||
10 | /** |
||||
11 | * The name and signature of the console command. |
||||
12 | * |
||||
13 | * @var string |
||||
14 | */ |
||||
15 | protected $signature = 'rebuild'; |
||||
16 | |||||
17 | /** |
||||
18 | * The console command description. |
||||
19 | * |
||||
20 | * @var string |
||||
21 | */ |
||||
22 | protected $description = 'Rebuild the app from scratch'; |
||||
23 | |||||
24 | /** |
||||
25 | * Create a new command instance. |
||||
26 | * |
||||
27 | * @return void |
||||
28 | */ |
||||
29 | public function __construct(Composer $composer) |
||||
30 | { |
||||
31 | parent::__construct(); |
||||
32 | |||||
33 | $this->composer = $composer; |
||||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||||
34 | } |
||||
35 | |||||
36 | /** |
||||
37 | * Execute the console command. |
||||
38 | * |
||||
39 | * @return mixed |
||||
40 | */ |
||||
41 | public function handle() |
||||
42 | { |
||||
43 | if (app()->environment(['prod', 'production'])) { |
||||
0 ignored issues
–
show
The method
environment() does not exist on Illuminate\Container\Container . Are you sure you never get this type here, but always one of the subclasses?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
44 | if ($this->confirm('You are in PRODUCTION environment. Continue?')) { |
||||
45 | $this->rebuildSequence(); |
||||
46 | |||||
47 | return; |
||||
48 | } |
||||
49 | } |
||||
50 | |||||
51 | $this->rebuildSequence(); |
||||
52 | } |
||||
53 | |||||
54 | /** |
||||
55 | * Command operation sequence. |
||||
56 | * |
||||
57 | * @return mixed |
||||
58 | */ |
||||
59 | protected function rebuildSequence() |
||||
60 | { |
||||
61 | if ($this->confirm('You are about to rebuild the app from scratch. Continue?')) { |
||||
62 | $this->enterMaintenanceMode(); |
||||
63 | |||||
64 | $this->rebuildDatabaseSchema(); |
||||
65 | $this->seedInitialData(); |
||||
66 | $this->seedDummyData(); |
||||
67 | $this->seedExampleData(); |
||||
68 | |||||
69 | $this->migrateSessionsTable(); |
||||
70 | $this->migrateNotificationsTable(); |
||||
71 | |||||
72 | $this->clearEvent(); |
||||
73 | $this->clearFrameworkBootstrapFiles(); |
||||
74 | $this->cacheFrameworkBootstrapFiles(); |
||||
75 | |||||
76 | $this->rediscoverPackages(); |
||||
77 | $this->createSymbolicLink(); |
||||
78 | |||||
79 | $this->runSelfDiagnosis(); |
||||
80 | $this->runApplicationTest(); |
||||
81 | |||||
82 | $this->leaveMaintenanceMode(); |
||||
83 | } |
||||
84 | } |
||||
85 | |||||
86 | /** |
||||
87 | * Set application to Maintenance Mode. |
||||
88 | * |
||||
89 | * @return void |
||||
90 | */ |
||||
91 | protected function enterMaintenanceMode() |
||||
92 | { |
||||
93 | if (config('rebuild.should_enter_maintenance_mode')) { |
||||
94 | $this->call('down'); |
||||
95 | $this->line(''); |
||||
96 | } |
||||
97 | } |
||||
98 | |||||
99 | /** |
||||
100 | * Rebuild database schema. |
||||
101 | * |
||||
102 | * @return void |
||||
103 | */ |
||||
104 | protected function rebuildDatabaseSchema() |
||||
105 | { |
||||
106 | if (config('rebuild.should_rebuild_database_schema')) { |
||||
107 | $this->call('migrate:fresh', ['--force' => true]); |
||||
108 | $this->info('Rebuilding database schema is done.'); |
||||
109 | $this->line(''); |
||||
110 | } |
||||
111 | } |
||||
112 | |||||
113 | /** |
||||
114 | * Seeding initial data. |
||||
115 | * |
||||
116 | * @return void |
||||
117 | */ |
||||
118 | protected function seedInitialData() |
||||
119 | { |
||||
120 | if (config('rebuild.should_seed_initial_data')) { |
||||
121 | $this->composer->dumpAutoloads(); |
||||
122 | |||||
123 | $this->call('db:seed', ['--force' => true]); |
||||
124 | $this->info('Seeding initial data is done.'); |
||||
125 | $this->line(''); |
||||
126 | } |
||||
127 | } |
||||
128 | |||||
129 | /** |
||||
130 | * Seeding dummy data. |
||||
131 | * |
||||
132 | * @return void |
||||
133 | */ |
||||
134 | protected function seedDummyData() |
||||
135 | { |
||||
136 | if (config('rebuild.dummy.should_seed')) { |
||||
137 | if ($this->confirm('Install dummy data?')) { |
||||
138 | try { |
||||
139 | $this->call('db:seed', [ |
||||
140 | '--class' => config('rebuild.dummy.seeder_name'), |
||||
141 | '--force' => true, |
||||
142 | ]); |
||||
143 | } catch (\Exception $e) { |
||||
144 | $this->error($e->getMessage()); |
||||
145 | $this->info('Seeding dummy data is aborted.'); |
||||
146 | $this->line(''); |
||||
147 | |||||
148 | return true; |
||||
149 | } |
||||
150 | |||||
151 | $this->info('Seeding dummy data is done.'); |
||||
152 | $this->line(''); |
||||
153 | } |
||||
154 | } |
||||
155 | } |
||||
156 | |||||
157 | /** |
||||
158 | * Seeding example data. |
||||
159 | * |
||||
160 | * @return void |
||||
161 | */ |
||||
162 | protected function seedExampleData() |
||||
163 | { |
||||
164 | if (config('rebuild.example.should_seed')) { |
||||
165 | if ($this->confirm('Install example data?')) { |
||||
166 | try { |
||||
167 | $this->call('db:seed', [ |
||||
168 | '--class' => config('rebuild.example.seeder_name'), |
||||
169 | '--force' => true, |
||||
170 | ]); |
||||
171 | } catch (\Exception $e) { |
||||
172 | $this->error($e->getMessage()); |
||||
173 | $this->info('Seeding example data is aborted.'); |
||||
174 | $this->line(''); |
||||
175 | |||||
176 | return true; |
||||
177 | } |
||||
178 | |||||
179 | $this->info('Seeding example data is done.'); |
||||
180 | $this->line(''); |
||||
181 | } |
||||
182 | } |
||||
183 | } |
||||
184 | |||||
185 | /** |
||||
186 | * Migrate sessions table. |
||||
187 | * |
||||
188 | * @return void |
||||
189 | */ |
||||
190 | protected function migrateSessionsTable() |
||||
191 | { |
||||
192 | if (config('rebuild.should_migrate_sessions_table')) { |
||||
193 | try { |
||||
194 | $this->call('session:table'); |
||||
195 | $this->call('migrate'); |
||||
196 | } catch (\Exception $e) { |
||||
197 | $this->error($e->getMessage()); |
||||
198 | $this->info('Migrating sessions table is aborted.'); |
||||
199 | $this->line(''); |
||||
200 | |||||
201 | return true; |
||||
202 | } |
||||
203 | |||||
204 | $this->info('Migrating sessions table is done.'); |
||||
205 | $this->line(''); |
||||
206 | } |
||||
207 | } |
||||
208 | |||||
209 | /** |
||||
210 | * Migrate notifications table. |
||||
211 | * |
||||
212 | * @return void |
||||
213 | */ |
||||
214 | protected function migrateNotificationsTable() |
||||
215 | { |
||||
216 | if (config('rebuild.should_migrate_notifications_table')) { |
||||
217 | try { |
||||
218 | $this->call('notification:table'); |
||||
219 | $this->call('migrate'); |
||||
220 | } catch (\Exception $e) { |
||||
221 | $this->error($e->getMessage()); |
||||
222 | $this->info('Migrating notifications table is aborted.'); |
||||
223 | $this->line(''); |
||||
224 | |||||
225 | return true; |
||||
226 | } |
||||
227 | |||||
228 | $this->info('Migrating notifications table is done.'); |
||||
229 | $this->line(''); |
||||
230 | } |
||||
231 | } |
||||
232 | |||||
233 | /** |
||||
234 | * Clear event. |
||||
235 | * |
||||
236 | * @return void |
||||
237 | */ |
||||
238 | protected function clearEvent() |
||||
239 | { |
||||
240 | if (config('rebuild.should_clear_event')) { |
||||
241 | $this->line('Run \'artisan event:clear\' command:'); |
||||
242 | $this->call('event:clear'); |
||||
243 | $this->line(''); |
||||
244 | } |
||||
245 | } |
||||
246 | |||||
247 | /** |
||||
248 | * Clear these following cached files: |
||||
249 | * Views, Application cache, Route cache, Configuration cache, and |
||||
250 | * Compiled services and packages files. |
||||
251 | * |
||||
252 | * @return void |
||||
253 | */ |
||||
254 | protected function clearFrameworkBootstrapFiles() |
||||
255 | { |
||||
256 | if (config('rebuild.should_clear_framework_bootstrap_files')) { |
||||
257 | $this->line('Run \'artisan optimize:clear\' command:'); |
||||
258 | $this->call('optimize:clear'); |
||||
259 | $this->line(''); |
||||
260 | } |
||||
261 | } |
||||
262 | |||||
263 | /** |
||||
264 | * Cache framework bootstrap files. |
||||
265 | * |
||||
266 | * @return void |
||||
267 | */ |
||||
268 | protected function cacheFrameworkBootstrapFiles() |
||||
269 | { |
||||
270 | if (config('rebuild.should_cache_framework_bootstrap_files')) { |
||||
271 | $this->line('Run \'artisan optimize\' command:'); |
||||
272 | $this->call('optimize'); |
||||
273 | $this->line(''); |
||||
274 | } |
||||
275 | } |
||||
276 | |||||
277 | /** |
||||
278 | * Rediscover packages. |
||||
279 | * |
||||
280 | * @return void |
||||
281 | */ |
||||
282 | protected function rediscoverPackages() |
||||
283 | { |
||||
284 | if (config('rebuild.should_rediscover_packages')) { |
||||
285 | $this->line('Run \'artisan package:discover\' command:'); |
||||
286 | $this->call('package:discover'); |
||||
287 | $this->line(''); |
||||
288 | } |
||||
289 | } |
||||
290 | |||||
291 | /** |
||||
292 | * Create symbolic link. |
||||
293 | * |
||||
294 | * @return void |
||||
295 | */ |
||||
296 | protected function createSymbolicLink() |
||||
297 | { |
||||
298 | if (config('rebuild.should_create_symbolic_link')) { |
||||
299 | $this->line('Run \'artisan storage:link\' command:'); |
||||
300 | $this->call('storage:link'); |
||||
301 | $this->line(''); |
||||
302 | } |
||||
303 | } |
||||
304 | |||||
305 | /** |
||||
306 | * Run BeyondCode's Laravel Self-Diagnosis command |
||||
307 | * more: https://github.com/beyondcode/laravel-self-diagnosis. |
||||
308 | * |
||||
309 | * @return void |
||||
310 | */ |
||||
311 | protected function runSelfDiagnosis() |
||||
312 | { |
||||
313 | if (config('rebuild.should_self_diagnosis')) { |
||||
314 | $this->line('Run \'artisan self-diagosis\' command:'); |
||||
315 | $this->call('self-diagnosis'); |
||||
316 | $this->line(''); |
||||
317 | } |
||||
318 | } |
||||
319 | |||||
320 | /** |
||||
321 | * Run application tests. |
||||
322 | * |
||||
323 | * @return void |
||||
324 | */ |
||||
325 | protected function runApplicationTest() |
||||
326 | { |
||||
327 | if (config('rebuild.should_run_application_test')) { |
||||
328 | $this->call('test'); |
||||
329 | $this->line(''); |
||||
330 | } |
||||
331 | } |
||||
332 | |||||
333 | /** |
||||
334 | * Wake up application from Maintenance Mode. |
||||
335 | * |
||||
336 | * @return void |
||||
337 | */ |
||||
338 | protected function leaveMaintenanceMode() |
||||
339 | { |
||||
340 | if (config('rebuild.should_leave_maintenance_mode')) { |
||||
341 | $this->call('up'); |
||||
342 | $this->line(''); |
||||
343 | } |
||||
344 | } |
||||
345 | } |
||||
346 |