Passed
Push — master ( 6c3371...08ef46 )
by Mokhlas
07:15
created

MultiAuthPrepare::installMigration()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 39
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 26
nc 1
nop 0
dl 0
loc 39
rs 9.504
c 0
b 0
f 0
1
<?php
2
3
namespace iMokhles\MultiAuthBackPack\Command;
4
5
use Illuminate\Console\Command;
6
use Illuminate\Database\Console\Migrations\BaseCommand;
7
use Illuminate\Database\Migrations\MigrationCreator;
8
use Illuminate\Support\Composer;
9
use Symfony\Component\Process\Exception\ProcessFailedException;
10
use Symfony\Component\Process\Process;
11
12
class MultiAuthPrepare extends BaseCommand
13
{
14
    /**
15
     * @var
16
     */
17
    protected $progressBar;
18
    /**
19
     * The name and signature of the console command.
20
     *
21
     * @var string
22
     */
23
    protected $signature = 'make:multi-backpack {name} {--admin_theme= : chose the theme you want';
24
25
    /**
26
     * The console command description.
27
     *
28
     * @var string
29
     */
30
    protected $description = 'create multi authentication guards with backpack panel';
31
32
    /**
33
     * The migration creator instance.
34
     *
35
     * @var \Illuminate\Database\Migrations\MigrationCreator
36
     */
37
    protected $creator;
38
39
    /**
40
     * The Composer instance.
41
     *
42
     * @var \Illuminate\Support\Composer
43
     */
44
    protected $composer;
45
46
    /**
47
     * Create a new migration install command instance.
48
     *
49
     * @param  \Illuminate\Database\Migrations\MigrationCreator  $creator
50
     * @param  \Illuminate\Support\Composer  $composer
51
     */
52
    public function __construct(MigrationCreator $creator, Composer $composer)
53
    {
54
        parent::__construct();
55
        $this->creator = $creator;
56
        $this->composer = $composer;
57
    }
58
59
    /**
60
     * Execute the console command.
61
     *
62
     * @return boolean
63
     */
64
    public function handle()
65
    {
66
67
        $this->progressBar = $this->output->createProgressBar(14);
68
        $this->progressBar->start();
69
70
        $this->line(" Preparing For MultiAuth. Please wait...");
71
        $this->progressBar->advance();
72
73
        $name = ucfirst($this->getParsedNameInput());
74
75
        $admin_theme = $this->option('admin_theme');
76
        if (is_null($admin_theme)) {
77
            $admin_theme = 'adminlte';
78
        }
79
80
        if ($this->isAlreadySetup() == false) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
81
82
            $this->line(" installing migrations...");
83
            $this->installMigration();
84
            $this->progressBar->advance();
85
86
            $this->line(" installing models...");
87
            $this->installModel();
88
            $this->progressBar->advance();
89
90
            $this->line(" installing route maps...");
91
            $this->installRouteMaps();
92
            $this->progressBar->advance();
93
94
            $this->line(" installing route files...");
95
            $this->installRouteFiles();
96
            $this->progressBar->advance();
97
98
            $this->line(" installing controllers...");
99
            $this->installControllers();
100
            $this->progressBar->advance();
101
102
            $this->line(" installing requests...");
103
            $this->installRequests();
104
            $this->progressBar->advance();
105
106
            $this->line(" installing configs...");
107
            $this->installConfigs();
108
            $this->progressBar->advance();
109
110
            $this->line(" installing middleware...");
111
            $this->installMiddleware();
112
            $this->progressBar->advance();
113
114
            $this->line(" installing unauthenticated function...");
115
            $this->installUnauthenticated();
116
            $this->progressBar->advance();
117
118
            $this->line(" installing views...");
119
            $this->installView($admin_theme);
0 ignored issues
show
Bug introduced by
It seems like $admin_theme can also be of type string[]; however, parameter $theme_name of iMokhles\MultiAuthBackPa...hPrepare::installView() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

119
            $this->installView(/** @scrutinizer ignore-type */ $admin_theme);
Loading history...
120
            $this->progressBar->advance();
121
122
            $this->line(" installing prologue alert...");
123
            $this->installPrologueAlert();
124
            $this->progressBar->advance();
125
126
            $this->line(" dump autoload...");
127
            $this->composer->dumpAutoloads();
128
            $this->progressBar->advance();
129
130
            $this->progressBar->finish();
131
            $this->info(" finished ".$name." setup with Backpack panel.");
132
        } else {
133
            $this->progressBar->finish();
134
            $this->line(" failed. already setup for: ".$name);
135
            $this->progressBar->advance();
136
        }
137
138
139
140
141
        return true;
142
    }
143
144
    /**
145
     * Publish Prologue Alert
146
     *
147
     * @return boolean
148
     */
149
    public function installPrologueAlert() {
150
        $alertsConfigFile = $this->getConfigsFolderPath().DIRECTORY_SEPARATOR."prologue/alerts.php";
151
        if (!file_exists($alertsConfigFile)) {
152
            $this->executeProcess('php artisan vendor:publish --provider="Prologue\Alerts\AlertsServiceProvider"',
153
                'publishing config for notifications - prologue/alerts');
0 ignored issues
show
Bug introduced by
'publishing config for n...ions - prologue/alerts' of type string is incompatible with the type boolean expected by parameter $beforeNotice of iMokhles\MultiAuthBackPa...epare::executeProcess(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

153
                /** @scrutinizer ignore-type */ 'publishing config for notifications - prologue/alerts');
Loading history...
154
        }
155
156
        return true;
157
    }
158
159
    /**
160
     * Install Migration.
161
     *
162
     * @return boolean
163
     */
164
    public function installMigration()
165
    {
166
        $nameSmallPlural = str_plural(snake_case($this->getParsedNameInput()));
167
        $name = ucfirst($this->getParsedNameInput());
168
        $namePlural = str_plural($name);
169
170
171
172
        $modelTableContent = file_get_contents(__DIR__ . '/../Migration/modelTable.stub');
173
        $modelTableContentNew = str_replace([
174
            '{{$namePlural}}',
175
            '{{$nameSmallPlural}}',
176
        ], [
177
            $namePlural,
178
            $nameSmallPlural
179
        ], $modelTableContent);
180
181
182
        $modelResetPasswordTableContent = file_get_contents(__DIR__ . '/../Migration/passwordResetsTable.stub');
183
        $modelResetPasswordTableContentNew = str_replace([
184
            '{{$namePlural}}',
185
            '{{$nameSmallPlural}}',
186
        ], [
187
            $namePlural,
188
            $nameSmallPlural
189
        ], $modelResetPasswordTableContent);
190
191
192
        $migrationName = date('Y_m_d_His') . '_'.'create_' . str_plural(snake_case($name)) .'_table.php';
193
        $migrationModelPath = $this->getMigrationPath().DIRECTORY_SEPARATOR.$migrationName;
194
        file_put_contents($migrationModelPath, $modelTableContentNew);
195
196
        $migrationResetName = date('Y_m_d_His') . '_'
197
            .'create_' . str_plural(snake_case($name))
198
            .'_password_resets_table.php';
199
        $migrationResetModelPath = $this->getMigrationPath().DIRECTORY_SEPARATOR.$migrationResetName;
200
        file_put_contents($migrationResetModelPath, $modelResetPasswordTableContentNew);
201
202
        return true;
203
204
    }
205
206
207
    /**
208
     * Install Model.
209
     *
210
     * @return boolean
211
     */
212
    public function installModel()
213
    {
214
        $nameSmall = snake_case($this->getParsedNameInput());
215
        $name = ucfirst($this->getParsedNameInput());
216
217
218
        $arrayToChange = [
219
            '{{$name}}',
220
        ];
221
222
        $newChanges = [
223
            $name,
224
        ];
225
        $nameSmallPlural = str_plural(snake_case($this->getParsedNameInput()));
226
        array_push($arrayToChange, '{{$nameSmallPlural}}');
227
        array_push($arrayToChange, '{{$nameSmall}}');
228
        array_push($newChanges, $nameSmallPlural);
229
        array_push($newChanges, $nameSmall);
230
231
        $modelContent = file_get_contents(__DIR__ . '/../Backpack/Model/model.stub');
232
        $modelContentNew = str_replace($arrayToChange, $newChanges, $modelContent);
233
234
        $createFolder = $this->getAppFolderPath().DIRECTORY_SEPARATOR."Models";
235
        if (!file_exists($createFolder)) {
236
            mkdir($createFolder);
237
        }
238
239
        $modelPath = $createFolder.DIRECTORY_SEPARATOR.$name.".php";
240
        file_put_contents($modelPath, $modelContentNew);
241
242
243
244
        $resetNotificationContent = file_get_contents(__DIR__ . '/../Notification/resetPasswordNotification.stub');
245
        $resetNotificationContentNew = str_replace([
246
            '{{$name}}',
247
            '{{$nameSmall}}',
248
        ], [
249
            $name,
250
            $nameSmall
251
        ], $resetNotificationContent);
252
253
        $verifyEmailContent = file_get_contents(__DIR__ . '/../Notification/verifyEmailNotification.stub');
254
        $verifyEmailContentNew = str_replace([
255
            '{{$name}}',
256
            '{{$nameSmall}}',
257
        ], [
258
            $name,
259
            $nameSmall
260
        ], $verifyEmailContent);
261
262
        $createFolder = $this->getAppFolderPath().DIRECTORY_SEPARATOR."Notifications";
263
        if (!file_exists($createFolder)) {
264
            mkdir($createFolder);
265
        }
266
267
        $resetNotificationPath = $createFolder.DIRECTORY_SEPARATOR.$name."ResetPasswordNotification.php";
268
        file_put_contents($resetNotificationPath, $resetNotificationContentNew);
269
270
        $verifyEmailPath = $createFolder.DIRECTORY_SEPARATOR.$name."VerifyEmailNotification.php";
271
        file_put_contents($verifyEmailPath, $verifyEmailContentNew);
272
273
        return true;
274
275
    }
276
277
    /**
278
     * Install View.
279
     *
280
     * @param string $theme_name
281
     * @return bool
282
     */
283
    public function installView($theme_name = 'adminlte')
284
    {
285
        $nameSmall = snake_case($this->getParsedNameInput());
286
        $name = ucfirst($this->getParsedNameInput());
287
288
        // layouts
289
        $appBlade = file_get_contents(__DIR__ . '/../Backpack/Views/'.$theme_name.'/layouts/layout.blade.stub');
290
        $appGuestBlade = file_get_contents(__DIR__ . '/../Backpack/Views/'.$theme_name.'/layouts/layout_guest.blade.stub');
291
292
        // home
293
        $homeBlade = file_get_contents(__DIR__ . '/../Backpack/Views/'.$theme_name.'/home.blade.stub');
294
295
        // auth
296
        $loginBlade = file_get_contents(__DIR__ . '/../Backpack/Views/'.$theme_name.'/auth/login.blade.stub');
297
        $registerBlade = file_get_contents(__DIR__ . '/../Backpack/Views/'.$theme_name.'/auth/register.blade.stub');
298
        $verifyEmailBlade = file_get_contents(__DIR__ . '/../Backpack/Views/'.$theme_name.'/auth/verify.blade.stub');
299
300
        // auth/passwords
301
        $resetBlade = file_get_contents(__DIR__ . '/../Backpack/Views/'.$theme_name.'/auth/passwords/reset.blade.stub');
302
        $emailBlade = file_get_contents(__DIR__ . '/../Backpack/Views/'.$theme_name.'/auth/passwords/email.blade.stub');
303
304
        // auth/account
305
        $update_infoBlade = file_get_contents(__DIR__
306
            . '/../Backpack/Views/'.$theme_name.'/auth/account/update_info.blade.stub');
307
        $change_passwordBlade = file_get_contents(__DIR__
308
            . '/../Backpack/Views/'.$theme_name.'/auth/account/change_password.blade.stub');
309
        $sidemenuBlade = file_get_contents(__DIR__
310
            . '/../Backpack/Views/'.$theme_name.'/auth/account/sidemenu.blade.stub');
311
312
        // inc
313
        $main_headerBlade = file_get_contents(__DIR__
314
            . '/../Backpack/Views/'.$theme_name.'/inc/main_header.blade.stub');
315
        $sidebarBlade = file_get_contents(__DIR__
316
            . '/../Backpack/Views/'.$theme_name.'/inc/sidebar.blade.stub');
317
        $alertsBlade = file_get_contents(__DIR__
318
            . '/../Backpack/Views/'.$theme_name.'/inc/alerts.blade.stub');
319
        $footerBlade = file_get_contents(__DIR__
320
            . '/../Backpack/Views/'.$theme_name.'/inc/footer.blade.stub');
321
        $footerGuestBlade = file_get_contents(__DIR__
322
            . '/../Backpack/Views/'.$theme_name.'/inc/footer_guest.blade.stub');
323
        $headBlade = file_get_contents(__DIR__
324
            . '/../Backpack/Views/'.$theme_name.'/inc/head.blade.stub');
325
        $scriptsBlade = file_get_contents(__DIR__
326
            . '/../Backpack/Views/'.$theme_name.'/inc/scripts.blade.stub');
327
328
        if ($theme_name == 'adminlte') {
329
            $menuBlade = file_get_contents(__DIR__
330
                . '/../Backpack/Views/'.$theme_name.'/inc/menu.blade.stub');
331
            $sidebar_user_panelBlade = file_get_contents(__DIR__
332
                . '/../Backpack/Views/'.$theme_name.'/inc/sidebar_user_panel.blade.stub');
333
            // style
334
            $authCSS = file_get_contents(__DIR__
335
                . '/../Backpack/Views/'.$theme_name.'/style/backpack_auth_css.blade.stub');
336
        } else {
337
            $account_infoBlade = file_get_contents(__DIR__
338
                . '/../Backpack/Views/'.$theme_name.'/auth/account/account_info.blade.stub');
339
            $sidebar_user_panelBlade = file_get_contents(__DIR__
340
                . '/../Backpack/Views/'.$theme_name.'/inc/user_menu.blade.stub');
341
            $breadcrumbBlade = file_get_contents(__DIR__
342
                . '/../Backpack/Views/'.$theme_name.'/auth/account/breadcrumb.blade.stub');
343
            $notifications_menuBlade = file_get_contents(__DIR__
344
                . '/../Backpack/Views/'.$theme_name.'/auth/account/notifications_menu.blade.stub');
345
        }
346
347
348
349
350
        $createFolder = $this->getViewsFolderPath().DIRECTORY_SEPARATOR."$nameSmall";
351
        if (!file_exists($createFolder)) {
352
            mkdir($createFolder);
353
        }
354
355
        $createFolderLayouts = $this->getViewsFolderPath().DIRECTORY_SEPARATOR
356
            ."$nameSmall"
357
            .DIRECTORY_SEPARATOR."layouts";
358
        if (!file_exists($createFolderLayouts)) {
359
            mkdir($createFolderLayouts);
360
        }
361
362
        $createFolderStyle = $this->getViewsFolderPath().DIRECTORY_SEPARATOR
363
            ."$nameSmall"
364
            .DIRECTORY_SEPARATOR."style";
365
        if (!file_exists($createFolderStyle)) {
366
            mkdir($createFolderStyle);
367
        }
368
369
        $createFolderInc = $this->getViewsFolderPath().DIRECTORY_SEPARATOR
370
            ."$nameSmall"
371
            .DIRECTORY_SEPARATOR."inc";
372
        if (!file_exists($createFolderInc)) {
373
            mkdir($createFolderInc);
374
        }
375
376
        $createFolderAuth = $this->getViewsFolderPath().DIRECTORY_SEPARATOR."$nameSmall"
377
            .DIRECTORY_SEPARATOR."auth";
378
        if (!file_exists($createFolderAuth)) {
379
            mkdir($createFolderAuth);
380
        }
381
382
        $createFolderAuthPasswords = $this->getViewsFolderPath().DIRECTORY_SEPARATOR.
383
            "$nameSmall".DIRECTORY_SEPARATOR
384
            ."auth".DIRECTORY_SEPARATOR."passwords";
385
        if (!file_exists($createFolderAuthPasswords)) {
386
            mkdir($createFolderAuthPasswords);
387
        }
388
389
        $createFolderAuthAccount = $this->getViewsFolderPath().DIRECTORY_SEPARATOR.
390
            "$nameSmall".DIRECTORY_SEPARATOR
391
            ."auth".DIRECTORY_SEPARATOR."account";
392
        if (!file_exists($createFolderAuthAccount)) {
393
            mkdir($createFolderAuthAccount);
394
        }
395
396
        $headBladeNew = str_replace([
397
            '{{$nameSmall}}',
398
        ], [
399
            $nameSmall
400
        ], $headBlade);
401
402
        $appBladeNew = str_replace([
403
            '{{$nameSmall}}',
404
        ], [
405
            $nameSmall
406
        ], $appBlade);
407
408
        $appGuestBladeNew = str_replace([
409
            '{{$nameSmall}}',
410
        ], [
411
            $nameSmall
412
        ], $appGuestBlade);
413
414
        $verifyEmailBladeNew = str_replace([
415
            '{{$nameSmall}}',
416
        ], [
417
            $nameSmall
418
        ], $verifyEmailBlade);
419
420
        $homeBladeNew = str_replace([
421
            '{{$nameSmall}}',
422
            '{{$name}}',
423
424
        ], [
425
            $nameSmall
426
        ], $homeBlade);
427
428
        $loginBladeNew = str_replace([
429
            '{{$nameSmall}}',
430
        ], [
431
            $nameSmall
432
        ], $loginBlade);
433
434
        $registerBladeNew = str_replace([
435
            '{{$nameSmall}}',
436
        ], [
437
            $nameSmall
438
        ], $registerBlade);
439
440
        $emailBladeNew = str_replace([
441
            '{{$nameSmall}}',
442
        ], [
443
            $nameSmall
444
        ], $emailBlade);
445
446
        $resetBladeNew = str_replace([
447
            '{{$nameSmall}}',
448
        ], [
449
            $nameSmall
450
        ], $resetBlade);
451
452
        $update_infoBladeNew = str_replace([
453
            '{{$nameSmall}}',
454
            '{{$name}}',
455
        ], [
456
            $nameSmall,
457
            $name
458
        ], $update_infoBlade);
459
460
        $change_passwordBladeNew = str_replace([
461
            '{{$nameSmall}}',
462
        ], [
463
            $nameSmall,
464
        ], $change_passwordBlade);
465
466
        if ($theme_name != 'adminlte') {
467
            $account_infoBladeNew = str_replace([
468
                '{{$nameSmall}}',
469
            ], [
470
                $nameSmall,
471
            ], $account_infoBlade);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $account_infoBlade does not seem to be defined for all execution paths leading up to this point.
Loading history...
472
        }
473
474
475
476
        $sidemenuBladeNew = str_replace([
477
            '{{$nameSmall}}',
478
        ], [
479
            $nameSmall
480
        ], $sidemenuBlade);
481
482
        $main_headerBladeNew = str_replace([
483
            '{{$nameSmall}}',
484
        ], [
485
            $nameSmall
486
        ], $main_headerBlade);
487
488
        $menuBladeNew = str_replace([
489
            '{{$nameSmall}}',
490
        ], [
491
            $nameSmall
492
        ], $menuBlade);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $menuBlade does not seem to be defined for all execution paths leading up to this point.
Loading history...
493
494
        $sidebarBladeNew = str_replace([
495
            '{{$nameSmall}}',
496
        ], [
497
            $nameSmall
498
        ], $sidebarBlade);
499
500
        $sidebar_user_panelBladeNew = str_replace([
501
            '{{$nameSmall}}',
502
        ], [
503
            $nameSmall
504
        ], $sidebar_user_panelBlade);
505
506
507
        file_put_contents($createFolderLayouts.'/layout.blade.php', $appBladeNew);
508
        file_put_contents($createFolderLayouts.'/layout_guest.blade.php', $appGuestBladeNew);
509
        file_put_contents($createFolder.'/home.blade.php', $homeBladeNew);
510
511
512
        file_put_contents($createFolderInc.'/main_header.blade.php', $main_headerBladeNew);
513
        file_put_contents($createFolderInc.'/sidebar.blade.php', $sidebarBladeNew);
514
515
        file_put_contents($createFolderInc.'/alerts.blade.php', $alertsBlade);
516
        file_put_contents($createFolderInc.'/footer.blade.php', $footerBlade);
517
        file_put_contents($createFolderInc.'/footer_guest.blade.php', $footerGuestBlade);
518
        file_put_contents($createFolderInc.'/head.blade.php', $headBladeNew);
519
        file_put_contents($createFolderInc.'/scripts.blade.php', $scriptsBlade);
520
521
        file_put_contents($createFolderAuth.'/login.blade.php', $loginBladeNew);
522
        file_put_contents($createFolderAuth.'/verify.blade.php', $verifyEmailBladeNew);
523
        file_put_contents($createFolderAuth.'/register.blade.php', $registerBladeNew);
524
        file_put_contents($createFolderAuthPasswords.'/email.blade.php', $emailBladeNew);
525
        file_put_contents($createFolderAuthPasswords.'/reset.blade.php', $resetBladeNew);
526
527
        file_put_contents($createFolderAuthAccount.'/sidemenu.blade.php', $sidemenuBladeNew);
528
        file_put_contents($createFolderAuthAccount.'/update_info.blade.php', $update_infoBladeNew);
529
        file_put_contents($createFolderAuthAccount.'/change_password.blade.php', $change_passwordBladeNew);
530
531
        if ($theme_name == 'adminlte') {
532
            file_put_contents($createFolderStyle.'/backpack_auth_css.blade.php', $authCSS);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $authCSS does not seem to be defined for all execution paths leading up to this point.
Loading history...
533
            file_put_contents($createFolderInc.'/menu.blade.php', $menuBladeNew);
534
            file_put_contents($createFolderInc.'/sidebar_user_panel.blade.php', $sidebar_user_panelBladeNew);
535
        } else {
536
            file_put_contents($createFolderAuthAccount.'/account_info.blade.php', $account_infoBladeNew);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $account_infoBladeNew does not seem to be defined for all execution paths leading up to this point.
Loading history...
537
            file_put_contents($createFolderInc.'/user_menu.blade.php', $sidebar_user_panelBladeNew);
538
            file_put_contents($createFolderInc.'/breadcrumb.blade.php', $breadcrumbBlade);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $breadcrumbBlade does not seem to be defined for all execution paths leading up to this point.
Loading history...
539
            file_put_contents($createFolderInc.'/notifications_menu.blade.php', $notifications_menuBlade);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $notifications_menuBlade does not seem to be defined for all execution paths leading up to this point.
Loading history...
540
        }
541
542
543
        return true;
544
545
    }
546
547
    /**
548
     * Install RouteMaps.
549
     *
550
     * @return boolean
551
     */
552
553
    public function installRouteMaps()
554
    {
555
        $nameSmall = snake_case($this->getParsedNameInput());
556
        $name = ucfirst($this->getParsedNameInput());
557
        $mapCallFunction = file_get_contents(__DIR__ . '/../Route/mapRoute.stub');
558
        $mapCallFunctionNew = str_replace('{{$name}}', "$name", $mapCallFunction);
559
        $this->insert($this->getRouteServicesPath(), '$this->mapWebRoutes();', $mapCallFunctionNew, true);
560
        $mapFunction = file_get_contents(__DIR__ . '/../Route/mapRouteFunction.stub');
561
        $mapFunctionNew = str_replace([
562
            '{{$name}}',
563
            '{{$nameSmall}}'
564
        ], [
565
            "$name",
566
            "$nameSmall"
567
        ], $mapFunction);
568
        $this->insert($this->getRouteServicesPath(), '        //
569
    }', $mapFunctionNew, true);
570
        return true;
571
    }
572
573
    public function isAlreadySetup() {
574
        $name = ucfirst($this->getParsedNameInput());
575
576
        $routeServicesContent = file_get_contents($this->getRouteServicesPath());
577
578
        if (str_contains($routeServicesContent,'$this->map'.$name.'Routes();')) {
579
            return true;
580
        }
581
        return false;
582
    }
583
584
    /**
585
     * Install RouteFile.
586
     *
587
     * @return boolean
588
     */
589
590
    public function installRouteFiles()
591
    {
592
        $nameSmall = snake_case($this->getParsedNameInput());
593
        $name = ucfirst($this->getParsedNameInput());
594
        $createFolder = $this->getRoutesFolderPath().DIRECTORY_SEPARATOR.$nameSmall;
595
        if (!file_exists($createFolder)) {
596
            mkdir($createFolder);
597
        }
598
        $routeFileContent = file_get_contents(__DIR__ . '/../Route/routeFile.stub');
599
        $routeFileContentNew = str_replace([
600
            '{{$name}}',
601
            '{{$nameSmall}}'
602
        ], [
603
            "$name",
604
            "$nameSmall"
605
        ], $routeFileContent);
606
        $routeFile = $createFolder.DIRECTORY_SEPARATOR.$nameSmall.".php";
607
        file_put_contents($routeFile, $routeFileContentNew);
608
        return true;
609
    }
610
611
    /**
612
     * Install Requests.
613
     *
614
     * @return boolean
615
     */
616
617
    public function installRequests()
618
    {
619
        $nameSmall = snake_case($this->getParsedNameInput());
620
        $name = ucfirst($this->getParsedNameInput());
621
622
        $nameFolder = $this->getControllersPath().DIRECTORY_SEPARATOR.$name;
623
        if (!file_exists($nameFolder)) {
624
            mkdir($nameFolder);
625
        }
626
627
        $requestsFolder = $nameFolder.DIRECTORY_SEPARATOR."Requests";
628
        if (!file_exists($requestsFolder)) {
629
            mkdir($requestsFolder);
630
        }
631
        $accountInfoContent = file_get_contents(__DIR__ . '/../Request/AccountInfoRequest.stub');
632
        $changePasswordContent = file_get_contents(__DIR__ . '/../Request/ChangePasswordRequest.stub');
633
634
        $accountInfoContentNew = str_replace([
635
            '{{$name}}',
636
            '{{$nameSmall}}'
637
        ], [
638
            "$name",
639
            "$nameSmall"
640
        ], $accountInfoContent);
641
642
        $changePasswordContentNew = str_replace([
643
            '{{$name}}',
644
            '{{$nameSmall}}'
645
        ], [
646
            "$name",
647
            "$nameSmall"
648
        ], $changePasswordContent);
649
650
        $accountInfoFile = $requestsFolder.DIRECTORY_SEPARATOR."{$name}AccountInfoRequest.php";
651
        $changePasswordFile = $requestsFolder.DIRECTORY_SEPARATOR."{$name}ChangePasswordRequest.php";
652
653
        file_put_contents($accountInfoFile, $accountInfoContentNew);
654
        file_put_contents($changePasswordFile, $changePasswordContentNew);
655
656
        return true;
657
658
    }
659
    /**
660
     * Install Controller.
661
     *
662
     * @return boolean
663
     */
664
665
    public function installControllers()
666
    {
667
        $nameSmall = snake_case($this->getParsedNameInput());
668
        $nameSmallPlural = str_plural(snake_case($this->getParsedNameInput()));
669
        $name = ucfirst($this->getParsedNameInput());
670
671
        $nameFolder = $this->getControllersPath().DIRECTORY_SEPARATOR.$name;
672
        if (!file_exists($nameFolder)) {
673
            mkdir($nameFolder);
674
        }
675
676
        $authFolder = $nameFolder.DIRECTORY_SEPARATOR."Auth";
677
        if (!file_exists($authFolder)) {
678
            mkdir($authFolder);
679
        }
680
681
        $controllerContent = file_get_contents(__DIR__ . '/../Controllers/Controller.stub');
682
        $homeControllerContent = file_get_contents(__DIR__ . '/../Controllers/HomeController.stub');
683
        $loginControllerContent = file_get_contents(__DIR__ . '/../Controllers/Auth/LoginController.stub');
684
        $forgotControllerContent = file_get_contents(__DIR__ . '/../Controllers/Auth/ForgotPasswordController.stub');
685
        $registerControllerContent = file_get_contents(__DIR__ . '/../Controllers/Auth/RegisterController.stub');
686
        $resetControllerContent = file_get_contents(__DIR__ . '/../Controllers/Auth/ResetPasswordController.stub');
687
        $myAccountControllerContent = file_get_contents(__DIR__ . '/../Controllers/Auth/MyAccountController.stub');
688
        $verificationControllerContent = file_get_contents(__DIR__ . '/../Controllers/Auth/VerificationController.stub');
689
690
        $controllerFileContentNew = str_replace('{{$name}}', "$name", $controllerContent);
691
692
        $homeFileContentNew = str_replace([
693
            '{{$name}}',
694
            '{{$nameSmall}}'
695
        ], [
696
            "$name",
697
            "$nameSmall"
698
        ], $homeControllerContent);
699
700
        $loginFileContentNew = str_replace([
701
            '{{$name}}',
702
            '{{$nameSmall}}'
703
        ], [
704
            "$name",
705
            "$nameSmall"
706
        ], $loginControllerContent);
707
708
        $forgotFileContentNew = str_replace([
709
            '{{$name}}',
710
            '{{$nameSmall}}',
711
            '{{$nameSmallPlural}}'
712
        ], [
713
            "$name",
714
            "$nameSmall",
715
            "$nameSmallPlural"
716
        ], $forgotControllerContent);
717
718
        $registerFileContentNew = str_replace([
719
            '{{$name}}',
720
            '{{$nameSmall}}',
721
            '{{$nameSmallPlural}}'
722
        ], [
723
            "$name",
724
            "$nameSmall",
725
            "$nameSmallPlural"
726
        ], $registerControllerContent);
727
728
        $resetFileContentNew = str_replace([
729
            '{{$name}}',
730
            '{{$nameSmall}}',
731
            '{{$nameSmallPlural}}'
732
        ], [
733
            "$name",
734
            "$nameSmall",
735
            "$nameSmallPlural"
736
        ], $resetControllerContent);
737
738
        $myAccountFileContentNew = str_replace([
739
            '{{$name}}',
740
            '{{$nameSmall}}'
741
        ], [
742
            "$name",
743
            "$nameSmall"
744
        ], $myAccountControllerContent);
745
746
        $verificationControllerContentNew = str_replace([
747
            '{{$name}}',
748
            '{{$nameSmall}}'
749
        ], [
750
            "$name",
751
            "$nameSmall"
752
        ], $verificationControllerContent);
753
754
        $controllerFile = $nameFolder.DIRECTORY_SEPARATOR."Controller.php";
755
        $homeFile = $nameFolder.DIRECTORY_SEPARATOR."HomeController.php";
756
        $loginFile = $authFolder.DIRECTORY_SEPARATOR."LoginController.php";
757
        $forgotFile = $authFolder.DIRECTORY_SEPARATOR."ForgotPasswordController.php";
758
        $registerFile = $authFolder.DIRECTORY_SEPARATOR."RegisterController.php";
759
        $resetFile = $authFolder.DIRECTORY_SEPARATOR."ResetPasswordController.php";
760
        $verificationFile = $authFolder.DIRECTORY_SEPARATOR."VerificationController.php";
761
762
        $myAccountFile = $authFolder.DIRECTORY_SEPARATOR."{$name}AccountController.php";
763
764
765
        file_put_contents($controllerFile, $controllerFileContentNew);
766
        file_put_contents($homeFile, $homeFileContentNew);
767
        file_put_contents($loginFile, $loginFileContentNew);
768
        file_put_contents($forgotFile, $forgotFileContentNew);
769
        file_put_contents($registerFile, $registerFileContentNew);
770
        file_put_contents($resetFile, $resetFileContentNew);
771
        file_put_contents($myAccountFile, $myAccountFileContentNew);
772
        file_put_contents($verificationFile, $verificationControllerContentNew);
773
774
        return true;
775
776
    }
777
778
    /**
779
     * Install Configs.
780
     *
781
     * @return boolean
782
     */
783
784
    public function installConfigs()
785
    {
786
        $nameSmall = snake_case($this->getParsedNameInput());
787
        $nameSmallPlural = str_plural(snake_case($this->getParsedNameInput()));
788
        $name = ucfirst($this->getParsedNameInput());
789
790
        $authConfigFile = $this->getConfigsFolderPath().DIRECTORY_SEPARATOR."auth.php";
791
792
        $guardContent = file_get_contents(__DIR__ . '/../Config/guard.stub');
793
        $passwordContent = file_get_contents(__DIR__ . '/../Config/password.stub');
794
        $providerContent = file_get_contents(__DIR__ . '/../Config/provider.stub');
795
796
        $guardFileContentNew = str_replace([
797
            '{{$nameSmall}}',
798
            '{{$nameSmallPlural}}'
799
        ], [
800
            "$nameSmall",
801
            "$nameSmallPlural"
802
        ], $guardContent);
803
804
        $passwordFileContentNew = str_replace('{{$nameSmallPlural}}', "$nameSmallPlural", $passwordContent);
805
806
        $providerFileContentNew = str_replace([
807
            '{{$name}}',
808
            '{{$nameSmallPlural}}'
809
        ], [
810
            "$name",
811
            "$nameSmallPlural"
812
        ], $providerContent);
813
814
        $this->insert($authConfigFile, '    \'guards\' => [', $guardFileContentNew, true);
815
816
        $this->insert($authConfigFile, '    \'passwords\' => [', $passwordFileContentNew, true);
817
818
        $this->insert($authConfigFile, '    \'providers\' => [', $providerFileContentNew, true);
819
820
        return true;
821
822
    }
823
824
    /**
825
     * Install Unauthenticated Handler.
826
     *
827
     * @return boolean
828
     */
829
    public function installUnauthenticated()
830
    {
831
        $nameSmall = snake_case($this->getParsedNameInput());
832
        $exceptionHandlerFile = $this->getAppFolderPath().DIRECTORY_SEPARATOR."Exceptions".DIRECTORY_SEPARATOR
833
            ."Handler.php";
834
        $exceptionHandlerFileContent = file_get_contents($exceptionHandlerFile);
835
        $exceptionHandlerFileContentNew = file_get_contents(__DIR__ . '/../Exceptions/handlerUnauthorized.stub');
836
837
838
        if (!str_contains($exceptionHandlerFileContent, 'MultiAuthUnAuthenticated')) {
839
            // replace old file
840
            $deleted = unlink($exceptionHandlerFile);
841
            if ($deleted) {
842
                file_put_contents($exceptionHandlerFile, $exceptionHandlerFileContentNew);
843
            }
844
        }
845
846
        $exceptionHandlerGuardContentNew = file_get_contents(__DIR__ . '/../Exceptions/handlerGuard.stub');
847
        $exceptionHandlerGuardContentNew2 = str_replace('{{$nameSmall}}', "$nameSmall",
848
            $exceptionHandlerGuardContentNew);
849
850
        $this->insert($exceptionHandlerFile, '        switch(array_get($exception->guards(), 0)) {',
851
            $exceptionHandlerGuardContentNew2, true);
852
853
        return true;
854
855
    }
856
857
    /**
858
     * Install Middleware.
859
     *
860
     * @return boolean
861
     */
862
863
    public function installMiddleware()
864
    {
865
        $nameSmall = snake_case($this->getParsedNameInput());
866
867
        $redirectIfMiddlewareFile = $this->getMiddlewarePath().DIRECTORY_SEPARATOR."RedirectIfAuthenticated.php";
868
        $authenticateMiddlewareFile = $this->getMiddlewarePath().DIRECTORY_SEPARATOR."Authenticate.php";
869
        $ensureEmailIsVerifiedMiddlewareFile = $this->getMiddlewarePath().DIRECTORY_SEPARATOR."EnsureEmailIsVerified.php";
870
        $middlewareKernelFile = $this->getHttpPath().DIRECTORY_SEPARATOR."Kernel.php";
871
872
        $redirectIfMiddlewareFileContent = file_get_contents($redirectIfMiddlewareFile);
873
        $authenticateMiddlewareFileContent = file_get_contents($redirectIfMiddlewareFile);
874
875
        $ensureEmailIsVerifiedMiddlewareFileeContent = file_get_contents(__DIR__ . '/../Middleware/ensureEmailIsVerified.stub');
876
        $redirectIfMiddlewareContentNew = file_get_contents(__DIR__ . '/../Middleware/redirectIf.stub');
877
        $authenticateMiddlewareContentNew = file_get_contents(__DIR__ . '/../Middleware/authenticate.stub');
878
879
        if (!file_exists($ensureEmailIsVerifiedMiddlewareFile)) {
880
            file_put_contents($ensureEmailIsVerifiedMiddlewareFile, $ensureEmailIsVerifiedMiddlewareFileeContent);
881
        }
882
883
        if (!str_contains($redirectIfMiddlewareFileContent, 'MultiAuthGuards')) {
884
            // replace old file
885
            $deleted = unlink($redirectIfMiddlewareFile);
886
            if ($deleted) {
887
                file_put_contents($redirectIfMiddlewareFile, $redirectIfMiddlewareContentNew);
888
            }
889
        }
890
891
        if (!str_contains($authenticateMiddlewareFileContent, 'MultiAuthGuards')) {
892
            // replace old file
893
            $deleted = unlink($authenticateMiddlewareFile);
894
            if ($deleted) {
895
                file_put_contents($authenticateMiddlewareFile, $authenticateMiddlewareContentNew);
896
            }
897
        }
898
899
        $redirectIfMiddlewareGroupContentNew = file_get_contents(__DIR__ . '/../Middleware/redirectMiddleware.stub');
900
        $redirectIfMiddlewareGuardContentNew = file_get_contents(__DIR__ . '/../Middleware/redirectMiddlewareGuard.stub');
901
        $authenticateIfMiddlewareGuardContentNew = file_get_contents(__DIR__ . '/../Middleware/authenticateIf.stub');
902
903
        $redirectIfMiddlewareGroupContentNew2 = str_replace('{{$nameSmall}}', "$nameSmall",
904
            $redirectIfMiddlewareGroupContentNew);
905
        $redirectIfMiddlewareGuardContentNew2 = str_replace('{{$nameSmall}}', "$nameSmall",
906
            $redirectIfMiddlewareGuardContentNew);
907
        $authenticateIfMiddlewareGuardContentNew2 = str_replace('{{$nameSmall}}', "$nameSmall",
908
            $authenticateIfMiddlewareGuardContentNew);
909
910
        $this->insert($middlewareKernelFile, '    protected $middlewareGroups = [',
911
            $redirectIfMiddlewareGroupContentNew2, true);
912
913
        $this->insert($redirectIfMiddlewareFile, '        switch ($guard) {',
914
            $redirectIfMiddlewareGuardContentNew2, true);
915
916
        $this->insert($authenticateMiddlewareFile, '        // MultiAuthGuards',
917
            $authenticateIfMiddlewareGuardContentNew2, true);
918
919
        return true;
920
921
    }
922
923
    /**
924
     * Run a SSH command.
925
     *
926
     * @param string $command      The SSH command that needs to be run
927
     * @param bool   $beforeNotice Information for the user before the command is run
928
     * @param bool   $afterNotice  Information for the user after the command is run
929
     *
930
     * @return mixed Command-line output
931
     */
932
    public function executeProcess($command, $beforeNotice = false, $afterNotice = false)
933
    {
934
        if ($beforeNotice) {
935
            $this->info('### '.$beforeNotice);
0 ignored issues
show
Bug introduced by
Are you sure $beforeNotice of type true can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

935
            $this->info('### './** @scrutinizer ignore-type */ $beforeNotice);
Loading history...
936
        } else {
937
            $this->info('### Running: '.$command);
938
        }
939
        $process = new Process($command);
940
        $process->run(function ($type, $buffer) {
941
            if (Process::ERR === $type) {
942
                echo '... > '.$buffer;
943
            } else {
944
                echo 'OUT > '.$buffer;
945
            }
946
        });
947
        // executes after the command finishes
948
        if (!$process->isSuccessful()) {
949
            throw new ProcessFailedException($process);
950
        }
951
        if ($afterNotice) {
952
            $this->info('### '.$afterNotice);
0 ignored issues
show
Bug introduced by
Are you sure $afterNotice of type true can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

952
            $this->info('### './** @scrutinizer ignore-type */ $afterNotice);
Loading history...
953
        }
954
    }
955
956
    /**
957
     * Get the desired class name from the input.
958
     *
959
     * @return string
960
     */
961
    protected function getParsedNameInput()
962
    {
963
        return mb_strtolower(str_singular($this->getNameInput()));
964
    }
965
    /**
966
     * Get the desired class name from the input.
967
     *
968
     * @return string
969
     */
970
    protected function getNameInput()
971
    {
972
        return trim($this->argument('name'));
0 ignored issues
show
Bug introduced by
It seems like $this->argument('name') can also be of type string[]; however, parameter $str of trim() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

972
        return trim(/** @scrutinizer ignore-type */ $this->argument('name'));
Loading history...
973
    }
974
975
    /**
976
     * Write the migration file to disk.
977
     *
978
     * @param  string  $name
979
     * @param  string  $table
980
     * @param  bool    $create
981
     * @return mixed
982
     */
983
    protected function writeMigration($name, $table, $create)
984
    {
985
        $file = pathinfo($this->creator->create(
986
            $name, $this->getMigrationPath(), $table, $create
987
        ), PATHINFO_FILENAME);
988
        $this->line("<info>Created Migration:</info> {$file}");
989
    }
990
991
    /**
992
     * Get migration path.
993
     *
994
     * @return string
995
     */
996
    protected function getMigrationPath()
997
    {
998
        return parent::getMigrationPath();
999
    }
1000
1001
    /**
1002
     * Get Routes Provider Path.
1003
     *
1004
     * @return string
1005
     */
1006
    protected function getRouteServicesPath()
1007
    {
1008
        return $this->getAppFolderPath().DIRECTORY_SEPARATOR.'Providers'.DIRECTORY_SEPARATOR.'RouteServiceProvider.php';
1009
    }
1010
1011
    /**
1012
     * Get Routes Folder Path.
1013
     *
1014
     * @return string
1015
     */
1016
    protected function getAppFolderPath()
1017
    {
1018
        return $this->laravel->basePath().DIRECTORY_SEPARATOR.'app';
1019
    }
1020
1021
    /**
1022
     * Get Routes Folder Path.
1023
     *
1024
     * @return string
1025
     */
1026
    protected function getRoutesFolderPath()
1027
    {
1028
        return $this->laravel->basePath().DIRECTORY_SEPARATOR.'routes';
1029
    }
1030
1031
    /**
1032
     * Get Config Folder Path.
1033
     *
1034
     * @return string
1035
     */
1036
    protected function getConfigsFolderPath()
1037
    {
1038
        return $this->laravel->basePath().DIRECTORY_SEPARATOR.'config';
1039
    }
1040
1041
    /**
1042
     * Get Config Folder Path.
1043
     *
1044
     * @return string
1045
     */
1046
    protected function getViewsFolderPath()
1047
    {
1048
        return $this->laravel->basePath().DIRECTORY_SEPARATOR.'resources'.DIRECTORY_SEPARATOR.'views';
1049
    }
1050
1051
    /**
1052
     * Get Controllers Path.
1053
     *
1054
     * @return string
1055
     */
1056
    protected function getControllersPath()
1057
    {
1058
        return $this->getAppFolderPath().DIRECTORY_SEPARATOR.'Http'.DIRECTORY_SEPARATOR.'Controllers';
1059
    }
1060
1061
    /**
1062
     * Get Http Path.
1063
     *
1064
     * @return string
1065
     */
1066
    protected function getHttpPath()
1067
    {
1068
        return $this->getAppFolderPath().DIRECTORY_SEPARATOR.'Http';
1069
    }
1070
1071
    /**
1072
     * Get Middleware Path.
1073
     *
1074
     * @return string
1075
     */
1076
    protected function getMiddlewarePath()
1077
    {
1078
        return $this->getAppFolderPath().DIRECTORY_SEPARATOR.'Http'.DIRECTORY_SEPARATOR.'Middleware';
1079
    }
1080
1081
    /**
1082
     * insert text into file
1083
     *
1084
     * @param string $filePath
1085
     * @param string $insertMarker
1086
     * @param string $text
1087
     * @param boolean $after
1088
     *
1089
     * @return integer
1090
     */
1091
    public function insertIntoFile($filePath, $insertMarker, $text, $after = true) {
1092
        $contents = file_get_contents($filePath);
1093
        $new_contents = preg_replace($insertMarker,($after) ? '$0' . $text : $text . '$0', $contents);
1094
        return file_put_contents($filePath, $new_contents);
1095
    }
1096
1097
    /**
1098
     * insert text into file
1099
     *
1100
     * @param string $filePath
1101
     * @param string $keyword
1102
     * @param string $body
1103
     * @param boolean $after
1104
     *
1105
     * @return integer
1106
     */
1107
    public function insert($filePath, $keyword, $body, $after = true) {
1108
1109
        $contents = file_get_contents($filePath);
1110
        $new_contents = substr_replace($contents, PHP_EOL . $body,
1111
            ($after) ? strpos($contents, $keyword) + strlen($keyword) : strpos($contents, $keyword)
1112
            , 0);
1113
        return file_put_contents($filePath, $new_contents);
1114
    }
1115
}
1116