MultiAuthPrepare::getRoutesFolderPath()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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

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