Completed
Push — master ( 4bbf81...304e9a )
by Mokhlas
01:34
created

MultiAuthPrepare::getViewsFolderPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace iMokhles\MultiAuthCommand\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
     * The name and signature of the console command.
16
     *
17
     * @var string
18
     */
19
    protected $signature = 'make:multi_auth {name} {--admin_theme= : chose the theme you want}';
20
21
    /**
22
     * The console command description.
23
     *
24
     * @var string
25
     */
26
    protected $description = 'create multi authentication guards with dashboard panel';
27
28
    /**
29
     * @var
30
     */
31
    protected $progressBar;
32
33
    /**
34
     * The migration creator instance.
35
     *
36
     * @var \Illuminate\Database\Migrations\MigrationCreator
37
     */
38
    protected $creator;
39
40
    /**
41
     * The Composer instance.
42
     *
43
     * @var \Illuminate\Support\Composer
44
     */
45
    protected $composer;
46
47
    /**
48
     * Create a new migration install command instance.
49
     *
50
     * @param  \Illuminate\Database\Migrations\MigrationCreator  $creator
51
     * @param  \Illuminate\Support\Composer  $composer
52
     */
53
    public function __construct(MigrationCreator $creator, Composer $composer)
54
    {
55
        parent::__construct();
56
        $this->creator = $creator;
57
        $this->composer = $composer;
58
    }
59
60
    /**
61
     * Execute the console command.
62
     *
63
     * @return boolean
64
     */
65
    public function handle()
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 = 'startui';
78
        }
79
80
        if ($this->checkIfThemeFileExistsOrNot($admin_theme)) {
81
            $this->line(" installing migrations...");
82
            $this->installMigration();
83
            $this->progressBar->advance();
84
85
            $this->line(" installing notifications database table...");
86
            $this->installNotificationsDatabase();
87
            $this->progressBar->advance();
88
89
            $this->line(" installing models...");
90
            $this->installModel();
91
            $this->progressBar->advance();
92
93
            $this->line(" installing route maps...");
94
            $this->installRouteMaps();
95
            $this->progressBar->advance();
96
97
            $this->line(" installing route files...");
98
            $this->installRouteFiles();
99
            $this->progressBar->advance();
100
101
            $this->line(" installing controllers...");
102
            $this->installControllers();
103
            $this->progressBar->advance();
104
105
            $this->line(" installing requests...");
106
            $this->installRequests();
107
            $this->progressBar->advance();
108
109
            $this->line(" installing configs...");
110
            $this->installConfigs();
111
            $this->progressBar->advance();
112
113
            $this->line(" installing middleware...");
114
            $this->installMiddleware();
115
            $this->progressBar->advance();
116
117
            $this->line(" installing unauthenticated function...");
118
            $this->installUnauthenticated();
119
            $this->progressBar->advance();
120
121
            $this->line(" installing views...");
122
            $this->installView($admin_theme);
123
            $this->progressBar->advance();
124
125
            $this->line(" installing languages files...");
126
            $this->installLangs();
127
            $this->progressBar->advance();
128
129
            $this->line(" installing prologue alert...");
130
            $this->installPrologueAlert();
131
            $this->progressBar->advance();
132
133
            $this->line(" dump autoload...");
134
            $this->composer->dumpAutoloads();
135
            $this->progressBar->advance();
136
137
            $this->progressBar->finish();
138
            $this->info(" finished ".$name." setup with Backpack panel.");
139
        } else {
140
            $this->progressBar->advance();
141
            $this->progressBar->finish();
142
            $this->line(" failed: ".$admin_theme." theme not found");
143
        }
144
        return true;
145
    }
146
147
    /**
148
     * @param $admin_theme
149
     * @return bool
150
     */
151
    private function checkIfThemeFileExistsOrNot($admin_theme) {
0 ignored issues
show
Coding Style Naming introduced by
The parameter $admin_theme is not named in camelCase.

This check marks parameter names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
152
        return (file_exists(__DIR__ . '/../Stubs/Views/'.$admin_theme.'/dashboard.blade.stub'));
153
    }
154
155
    /**
156
     * @return bool
157
     */
158
    public function isAlreadySetup() {
159
        $name = ucfirst($this->getParsedNameInput());
160
161
        $routeServicesContent = file_get_contents($this->getRouteServicesPath());
162
163
        if (str_contains($routeServicesContent,'$this->map'.$name.'Routes();')) {
0 ignored issues
show
Unused Code introduced by
This if statement, and the following return statement can be replaced with return str_contains($rou.... $name . 'Routes();');.
Loading history...
164
            return true;
165
        }
166
        return false;
167
    }
168
169
    /**
170
     * Install Migration.
171
     *
172
     * @return boolean
173
     */
174
    public function installMigration()
175
    {
176
        $nameSmallPlural = str_plural(snake_case($this->getParsedNameInput()));
177
        $name = ucfirst($this->getParsedNameInput());
178
        $namePlural = str_plural($name);
179
180
181
182
        $modelTableContent = file_get_contents(__DIR__ . '/../Stubs/Migration/modelTable.stub');
183
        $modelTableContentNew = str_replace([
184
            '{{$namePlural}}',
185
            '{{$nameSmallPlural}}',
186
        ], [
187
            $namePlural,
188
            $nameSmallPlural
189
        ], $modelTableContent);
190
191
192
        $modelResetPasswordTableContent = file_get_contents(__DIR__ . '/../Stubs/Migration/passwordResetsTable.stub');
193
        $modelResetPasswordTableContentNew = str_replace([
194
            '{{$namePlural}}',
195
            '{{$nameSmallPlural}}',
196
        ], [
197
            $namePlural,
198
            $nameSmallPlural
199
        ], $modelResetPasswordTableContent);
200
201
202
        $migrationName = date('Y_m_d_His') . '_'.'create_' . str_plural(snake_case($name)) .'_table.php';
203
        $migrationModelPath = $this->getMigrationPath().DIRECTORY_SEPARATOR.$migrationName;
204
        file_put_contents($migrationModelPath, $modelTableContentNew);
205
206
        $migrationResetName = date('Y_m_d_His') . '_'
207
            .'create_' . str_plural(snake_case($name))
208
            .'_password_resets_table.php';
209
        $migrationResetModelPath = $this->getMigrationPath().DIRECTORY_SEPARATOR.$migrationResetName;
210
        file_put_contents($migrationResetModelPath, $modelResetPasswordTableContentNew);
211
212
        return true;
213
214
    }
215
216
    /**
217
     * Install Model.
218
     *
219
     * @return boolean
220
     */
221
    public function installModel()
222
    {
223
        $nameSmall = snake_case($this->getParsedNameInput());
224
        $name = ucfirst($this->getParsedNameInput());
225
226
227
        $arrayToChange = [
228
            '{{$name}}',
229
        ];
230
231
        $newChanges = [
232
            $name,
233
        ];
234
        $nameSmallPlural = str_plural(snake_case($this->getParsedNameInput()));
235
        array_push($arrayToChange, '{{$nameSmallPlural}}');
236
        array_push($arrayToChange, '{{$nameSmall}}');
237
        array_push($newChanges, $nameSmallPlural);
238
        array_push($newChanges, $nameSmall);
239
240
        $modelContent = file_get_contents(__DIR__ . '/../Stubs/Model/model.stub');
241
        $modelContentNew = str_replace($arrayToChange, $newChanges, $modelContent);
242
243
        $createFolder = $this->getAppFolderPath().DIRECTORY_SEPARATOR."Models";
244
        if (!file_exists($createFolder)) {
245
            mkdir($createFolder);
246
        }
247
248
        $modelPath = $createFolder.DIRECTORY_SEPARATOR.$name.".php";
249
        file_put_contents($modelPath, $modelContentNew);
250
251
252
253
        $resetNotificationContent = file_get_contents(__DIR__ . '/../Stubs/Notification/resetPasswordNotification.stub');
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 121 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
254
        $resetNotificationContentNew = str_replace([
255
            '{{$name}}',
256
            '{{$nameSmall}}',
257
        ], [
258
            $name,
259
            $nameSmall
260
        ], $resetNotificationContent);
261
262
        $verifyEmailContent = file_get_contents(__DIR__ . '/../Stubs/Notification/verifyEmailNotification.stub');
263
        $verifyEmailContentNew = str_replace([
264
            '{{$name}}',
265
            '{{$nameSmall}}',
266
        ], [
267
            $name,
268
            $nameSmall
269
        ], $verifyEmailContent);
270
271
        $createFolder = $this->getAppFolderPath().DIRECTORY_SEPARATOR."Notifications";
272
        if (!file_exists($createFolder)) {
273
            mkdir($createFolder);
274
        }
275
276
        $resetNotificationPath = $createFolder.DIRECTORY_SEPARATOR.$name."ResetPasswordNotification.php";
277
        file_put_contents($resetNotificationPath, $resetNotificationContentNew);
278
279
        $verifyEmailPath = $createFolder.DIRECTORY_SEPARATOR.$name."VerifyEmailNotification.php";
280
        file_put_contents($verifyEmailPath, $verifyEmailContentNew);
281
282
        return true;
283
284
    }
285
286
    /**
287
     * Install RouteMaps.
288
     *
289
     * @return boolean
290
     */
291
292
    public function installRouteMaps()
293
    {
294
        $nameSmall = snake_case($this->getParsedNameInput());
295
        $name = ucfirst($this->getParsedNameInput());
296
        $mapCallFunction = file_get_contents(__DIR__ . '/../Stubs/Route/mapRoute.stub');
297
        $mapCallFunctionNew = str_replace('{{$name}}', "$name", $mapCallFunction);
298
        $this->insert($this->getRouteServicesPath(), '$this->mapWebRoutes();', $mapCallFunctionNew, true);
299
        $mapFunction = file_get_contents(__DIR__ . '/../Stubs/Route/mapRouteFunction.stub');
300
        $mapFunctionNew = str_replace([
301
            '{{$name}}',
302
            '{{$nameSmall}}'
303
        ], [
304
            "$name",
305
            "$nameSmall"
306
        ], $mapFunction);
307
        $this->insert($this->getRouteServicesPath(), '        //
308
    }', $mapFunctionNew, true);
309
        return true;
310
    }
311
312
    /**
313
     * Install RouteFile.
314
     *
315
     * @return boolean
316
     */
317
318
    public function installRouteFiles()
319
    {
320
        $nameSmall = snake_case($this->getParsedNameInput());
321
        $name = ucfirst($this->getParsedNameInput());
322
        $createFolder = $this->getRoutesFolderPath().DIRECTORY_SEPARATOR.$nameSmall;
323
        if (!file_exists($createFolder)) {
324
            mkdir($createFolder);
325
        }
326
        $routeFileContent = file_get_contents(__DIR__ . '/../Stubs/Route/routeFile.stub');
327
        $routeFileContentNew = str_replace([
328
            '{{$name}}',
329
            '{{$nameSmall}}'
330
        ], [
331
            "$name",
332
            "$nameSmall"
333
        ], $routeFileContent);
334
        $routeFile = $createFolder.DIRECTORY_SEPARATOR.$nameSmall.".php";
335
        file_put_contents($routeFile, $routeFileContentNew);
336
        return true;
337
    }
338
339
    /**
340
     * Install Controller.
341
     *
342
     * @return boolean
343
     */
344
345
    public function installControllers()
346
    {
347
        $nameSmall = snake_case($this->getParsedNameInput());
348
        $nameSmallPlural = str_plural(snake_case($this->getParsedNameInput()));
349
        $name = ucfirst($this->getParsedNameInput());
350
351
        $nameFolder = $this->getControllersPath().DIRECTORY_SEPARATOR.$name;
352
        if (!file_exists($nameFolder)) {
353
            mkdir($nameFolder);
354
        }
355
356
        $authFolder = $nameFolder.DIRECTORY_SEPARATOR."Auth";
357
        if (!file_exists($authFolder)) {
358
            mkdir($authFolder);
359
        }
360
361
        $controllerContent = file_get_contents(__DIR__ . '/../Stubs/Controllers/Controller.stub');
362
        $homeControllerContent = file_get_contents(__DIR__ . '/../Stubs/Controllers/DashboardController.stub');
363
        $loginControllerContent = file_get_contents(__DIR__ . '/../Stubs/Controllers/Auth/LoginController.stub');
364
        $forgotControllerContent = file_get_contents(__DIR__ . '/../Stubs/Controllers/Auth/ForgotPasswordController.stub');
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 123 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
365
        $registerControllerContent = file_get_contents(__DIR__ . '/../Stubs/Controllers/Auth/RegisterController.stub');
366
        $resetControllerContent = file_get_contents(__DIR__ . '/../Stubs/Controllers/Auth/ResetPasswordController.stub');
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 121 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
367
        $myAccountControllerContent = file_get_contents(__DIR__ . '/../Stubs/Controllers/Auth/MyAccountController.stub');
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 121 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
368
        $verificationControllerContent = file_get_contents(__DIR__ . '/../Stubs/Controllers/Auth/VerificationController.stub');
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 127 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
369
370
        $controllerFileContentNew = str_replace('{{$name}}', "$name", $controllerContent);
371
372
        $homeFileContentNew = str_replace([
373
            '{{$name}}',
374
            '{{$nameSmall}}'
375
        ], [
376
            "$name",
377
            "$nameSmall"
378
        ], $homeControllerContent);
379
380
        $loginFileContentNew = str_replace([
381
            '{{$name}}',
382
            '{{$nameSmall}}'
383
        ], [
384
            "$name",
385
            "$nameSmall"
386
        ], $loginControllerContent);
387
388
        $forgotFileContentNew = str_replace([
389
            '{{$name}}',
390
            '{{$nameSmall}}',
391
            '{{$nameSmallPlural}}'
392
        ], [
393
            "$name",
394
            "$nameSmall",
395
            "$nameSmallPlural"
396
        ], $forgotControllerContent);
397
398
        $registerFileContentNew = str_replace([
399
            '{{$name}}',
400
            '{{$nameSmall}}',
401
            '{{$nameSmallPlural}}'
402
        ], [
403
            "$name",
404
            "$nameSmall",
405
            "$nameSmallPlural"
406
        ], $registerControllerContent);
407
408
        $resetFileContentNew = str_replace([
409
            '{{$name}}',
410
            '{{$nameSmall}}',
411
            '{{$nameSmallPlural}}'
412
        ], [
413
            "$name",
414
            "$nameSmall",
415
            "$nameSmallPlural"
416
        ], $resetControllerContent);
417
418
        $myAccountFileContentNew = str_replace([
419
            '{{$name}}',
420
            '{{$nameSmall}}'
421
        ], [
422
            "$name",
423
            "$nameSmall"
424
        ], $myAccountControllerContent);
425
426
        $verificationControllerContentNew = str_replace([
427
            '{{$name}}',
428
            '{{$nameSmall}}'
429
        ], [
430
            "$name",
431
            "$nameSmall"
432
        ], $verificationControllerContent);
433
434
        $controllerFile = $nameFolder.DIRECTORY_SEPARATOR."Controller.php";
435
        $homeFile = $nameFolder.DIRECTORY_SEPARATOR."DashboardController.php";
436
        $loginFile = $authFolder.DIRECTORY_SEPARATOR."LoginController.php";
437
        $forgotFile = $authFolder.DIRECTORY_SEPARATOR."ForgotPasswordController.php";
438
        $registerFile = $authFolder.DIRECTORY_SEPARATOR."RegisterController.php";
439
        $resetFile = $authFolder.DIRECTORY_SEPARATOR."ResetPasswordController.php";
440
        $verificationFile = $authFolder.DIRECTORY_SEPARATOR."VerificationController.php";
441
442
        $myAccountFile = $authFolder.DIRECTORY_SEPARATOR."{$name}AccountController.php";
443
444
445
        file_put_contents($controllerFile, $controllerFileContentNew);
446
        file_put_contents($homeFile, $homeFileContentNew);
447
        file_put_contents($loginFile, $loginFileContentNew);
448
        file_put_contents($forgotFile, $forgotFileContentNew);
449
        file_put_contents($registerFile, $registerFileContentNew);
450
        file_put_contents($resetFile, $resetFileContentNew);
451
        file_put_contents($myAccountFile, $myAccountFileContentNew);
452
        file_put_contents($verificationFile, $verificationControllerContentNew);
453
454
        return true;
455
456
    }
457
458
    /**
459
     * Install Requests.
460
     *
461
     * @return boolean
462
     */
463
464
    public function installRequests()
465
    {
466
        $nameSmall = snake_case($this->getParsedNameInput());
467
        $name = ucfirst($this->getParsedNameInput());
468
469
        $nameFolder = $this->getControllersPath().DIRECTORY_SEPARATOR.$name;
470
        if (!file_exists($nameFolder)) {
471
            mkdir($nameFolder);
472
        }
473
474
        $requestsFolder = $nameFolder.DIRECTORY_SEPARATOR."Requests";
475
        if (!file_exists($requestsFolder)) {
476
            mkdir($requestsFolder);
477
        }
478
        $accountInfoContent = file_get_contents(__DIR__ . '/../Stubs/Request/AccountInfoRequest.stub');
479
        $changePasswordContent = file_get_contents(__DIR__ . '/../Stubs/Request/ChangePasswordRequest.stub');
480
481
        $accountInfoContentNew = str_replace([
482
            '{{$name}}',
483
            '{{$nameSmall}}'
484
        ], [
485
            "$name",
486
            "$nameSmall"
487
        ], $accountInfoContent);
488
489
        $changePasswordContentNew = str_replace([
490
            '{{$name}}',
491
            '{{$nameSmall}}'
492
        ], [
493
            "$name",
494
            "$nameSmall"
495
        ], $changePasswordContent);
496
497
        $accountInfoFile = $requestsFolder.DIRECTORY_SEPARATOR."{$name}AccountInfoRequest.php";
498
        $changePasswordFile = $requestsFolder.DIRECTORY_SEPARATOR."{$name}ChangePasswordRequest.php";
499
500
        file_put_contents($accountInfoFile, $accountInfoContentNew);
501
        file_put_contents($changePasswordFile, $changePasswordContentNew);
502
503
        return true;
504
505
    }
506
507
    /**
508
     * Install Configs.
509
     *
510
     * @return boolean
511
     */
512
513
    public function installConfigs()
514
    {
515
        $nameSmall = snake_case($this->getParsedNameInput());
516
        $nameSmallPlural = str_plural(snake_case($this->getParsedNameInput()));
517
        $name = ucfirst($this->getParsedNameInput());
518
519
        $authConfigFile = $this->getConfigsFolderPath().DIRECTORY_SEPARATOR."auth.php";
520
521
        $guardContent = file_get_contents(__DIR__ . '/../Stubs/Config/guard.stub');
522
        $passwordContent = file_get_contents(__DIR__ . '/../Stubs/Config/password.stub');
523
        $providerContent = file_get_contents(__DIR__ . '/../Stubs/Config/provider.stub');
524
525
        $guardFileContentNew = str_replace([
526
            '{{$nameSmall}}',
527
            '{{$nameSmallPlural}}'
528
        ], [
529
            "$nameSmall",
530
            "$nameSmallPlural"
531
        ], $guardContent);
532
533
        $passwordFileContentNew = str_replace('{{$nameSmallPlural}}', "$nameSmallPlural", $passwordContent);
534
535
        $providerFileContentNew = str_replace([
536
            '{{$name}}',
537
            '{{$nameSmallPlural}}'
538
        ], [
539
            "$name",
540
            "$nameSmallPlural"
541
        ], $providerContent);
542
543
        $this->insert($authConfigFile, '    \'guards\' => [', $guardFileContentNew, true);
544
545
        $this->insert($authConfigFile, '    \'passwords\' => [', $passwordFileContentNew, true);
546
547
        $this->insert($authConfigFile, '    \'providers\' => [', $providerFileContentNew, true);
548
549
        return true;
550
551
    }
552
553
    /**
554
     * Install Middleware.
555
     *
556
     * @return boolean
557
     */
558
559
    public function installMiddleware()
560
    {
561
        $nameSmall = snake_case($this->getParsedNameInput());
562
563
        $redirectIfMiddlewareFile = $this->getMiddlewarePath().DIRECTORY_SEPARATOR."RedirectIfAuthenticated.php";
564
        $authenticateMiddlewareFile = $this->getMiddlewarePath().DIRECTORY_SEPARATOR."Authenticate.php";
565
        $ensureEmailIsVerifiedMiddlewareFile = $this->getMiddlewarePath().DIRECTORY_SEPARATOR."EnsureEmailIsVerified.php";
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 122 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
566
        $middlewareKernelFile = $this->getHttpPath().DIRECTORY_SEPARATOR."Kernel.php";
567
568
        $redirectIfMiddlewareFileContent = file_get_contents($redirectIfMiddlewareFile);
569
        $authenticateMiddlewareFileContent = file_get_contents($redirectIfMiddlewareFile);
570
571
        $ensureEmailIsVerifiedMiddlewareFileeContent = file_get_contents(__DIR__ . '/../Stubs/Middleware/ensureEmailIsVerified.stub');
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 134 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
572
        $redirectIfMiddlewareContentNew = file_get_contents(__DIR__ . '/../Stubs/Middleware/redirectIf.stub');
573
        $authenticateMiddlewareContentNew = file_get_contents(__DIR__ . '/../Stubs/Middleware/authenticate.stub');
574
575
        if (!file_exists($ensureEmailIsVerifiedMiddlewareFile)) {
576
            file_put_contents($ensureEmailIsVerifiedMiddlewareFile, $ensureEmailIsVerifiedMiddlewareFileeContent);
577
        }
578
579
        if (!str_contains($redirectIfMiddlewareFileContent, 'MultiAuthGuards')) {
580
            // replace old file
581
            $deleted = unlink($redirectIfMiddlewareFile);
582
            if ($deleted) {
583
                file_put_contents($redirectIfMiddlewareFile, $redirectIfMiddlewareContentNew);
584
            }
585
        }
586
587
        if (!str_contains($authenticateMiddlewareFileContent, 'MultiAuthGuards')) {
588
            // replace old file
589
            $deleted = unlink($authenticateMiddlewareFile);
590
            if ($deleted) {
591
                file_put_contents($authenticateMiddlewareFile, $authenticateMiddlewareContentNew);
592
            }
593
        }
594
595
        $redirectIfMiddlewareGroupContentNew = file_get_contents(__DIR__ . '/../Stubs/Middleware/redirectMiddleware.stub');
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 123 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
596
        $redirectIfMiddlewareGuardContentNew = file_get_contents(__DIR__ . '/../Stubs/Middleware/redirectMiddlewareGuard.stub');
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 128 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
597
        $authenticateIfMiddlewareGuardContentNew = file_get_contents(__DIR__ . '/../Stubs/Middleware/authenticateIf.stub');
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 123 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
598
599
        $redirectIfMiddlewareGroupContentNew2 = str_replace('{{$nameSmall}}', "$nameSmall",
600
            $redirectIfMiddlewareGroupContentNew);
601
        $redirectIfMiddlewareGuardContentNew2 = str_replace('{{$nameSmall}}', "$nameSmall",
602
            $redirectIfMiddlewareGuardContentNew);
603
        $authenticateIfMiddlewareGuardContentNew2 = str_replace('{{$nameSmall}}', "$nameSmall",
604
            $authenticateIfMiddlewareGuardContentNew);
605
606
        $this->insert($middlewareKernelFile, '    protected $middlewareGroups = [',
607
            $redirectIfMiddlewareGroupContentNew2, true);
608
609
        $this->insert($redirectIfMiddlewareFile, '        switch ($guard) {',
610
            $redirectIfMiddlewareGuardContentNew2, true);
611
612
        $this->insert($authenticateMiddlewareFile, '        // MultiAuthGuards',
613
            $authenticateIfMiddlewareGuardContentNew2, true);
614
615
        return true;
616
617
    }
618
619
    /**
620
     * Install Unauthenticated Handler.
621
     *
622
     * @return boolean
623
     */
624
    public function installUnauthenticated()
625
    {
626
        $nameSmall = snake_case($this->getParsedNameInput());
627
        $exceptionHandlerFile = $this->getAppFolderPath().DIRECTORY_SEPARATOR."Exceptions".DIRECTORY_SEPARATOR
628
            ."Handler.php";
629
        $exceptionHandlerFileContent = file_get_contents($exceptionHandlerFile);
630
        $exceptionHandlerFileContentNew = file_get_contents(__DIR__ . '/../Stubs/Exceptions/handlerUnauthorized.stub');
631
632
633
        if (!str_contains($exceptionHandlerFileContent, 'MultiAuthUnAuthenticated')) {
634
            // replace old file
635
            $deleted = unlink($exceptionHandlerFile);
636
            if ($deleted) {
637
                file_put_contents($exceptionHandlerFile, $exceptionHandlerFileContentNew);
638
            }
639
        }
640
641
        $exceptionHandlerGuardContentNew = file_get_contents(__DIR__ . '/../Stubs/Exceptions/handlerGuard.stub');
642
        $exceptionHandlerGuardContentNew2 = str_replace('{{$nameSmall}}', "$nameSmall",
643
            $exceptionHandlerGuardContentNew);
644
645
        $this->insert($exceptionHandlerFile, '        switch(array_get($exception->guards(), 0)) {',
646
            $exceptionHandlerGuardContentNew2, true);
647
648
        return true;
649
650
    }
651
652
    /**
653
     * Install View.
654
     *
655
     * @param string $theme_name
656
     */
657
    public function installView($theme_name = 'startui')
0 ignored issues
show
Coding Style Naming introduced by
The parameter $theme_name is not named in camelCase.

This check marks parameter names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
658
    {
659
660
        $nameSmall = snake_case($this->getParsedNameInput());
661
662
        // layouts
663
        $layoutBlade = file_get_contents(__DIR__ . '/../Stubs/Views/'.$theme_name.'/layouts/layout.blade.stub');
664
        $layoutGuestBlade = file_get_contents(__DIR__ . '/../Stubs/Views/'.$theme_name.'/layouts/layout_guest.blade.stub');
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 123 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
665
666
        // dashboard
667
        $dashboardBlade = file_get_contents(__DIR__ . '/../Stubs/Views/'.$theme_name.'/dashboard.blade.stub');
668
669
        // auth
670
        $loginBlade = file_get_contents(__DIR__ . '/../Stubs/Views/'.$theme_name.'/auth/login.blade.stub');
671
        $registerBlade = file_get_contents(__DIR__ . '/../Stubs/Views/'.$theme_name.'/auth/register.blade.stub');
672
        $verifyEmailBlade = file_get_contents(__DIR__ . '/../Stubs/Views/'.$theme_name.'/auth/verify.blade.stub');
673
674
        // auth/passwords
675
        $resetBlade = file_get_contents(__DIR__ . '/../Stubs/Views/'.$theme_name.'/auth/passwords/reset.blade.stub');
676
        $emailBlade = file_get_contents(__DIR__ . '/../Stubs/Views/'.$theme_name.'/auth/passwords/email.blade.stub');
677
678
        // auth/account
679
        $update_infoBlade = file_get_contents(__DIR__
680
            . '/../Stubs/Views/'.$theme_name.'/auth/account/update_info.blade.stub');
681
        $right_boxBlade = file_get_contents(__DIR__
682
            . '/../Stubs/Views/'.$theme_name.'/auth/account/right_box.blade.stub');
683
        $left_boxBlade = file_get_contents(__DIR__
684
            . '/../Stubs/Views/'.$theme_name.'/auth/account/left_box.blade.stub');
685
        $change_passwordBlade = file_get_contents(__DIR__
686
            . '/../Stubs/Views/'.$theme_name.'/auth/account/change_password_tab.blade.stub');
687
        $account_infoBlade = file_get_contents(__DIR__
688
            . '/../Stubs/Views/'.$theme_name.'/auth/account/account_info_tab.blade.stub');
689
690
        // inc
691
        $alertsBlade = file_get_contents(__DIR__
692
            . '/../Stubs/Views/'.$theme_name.'/layouts/inc/alerts.blade.stub');
693
        $breadcrumbBlade = file_get_contents(__DIR__
694
            . '/../Stubs/Views/'.$theme_name.'/layouts/inc/breadcrumb.blade.stub');
695
        $headBlade = file_get_contents(__DIR__
696
            . '/../Stubs/Views/'.$theme_name.'/layouts/inc/head.blade.stub');
697
        $scriptsBlade = file_get_contents(__DIR__
698
            . '/../Stubs/Views/'.$theme_name.'/layouts/inc/scripts.blade.stub');
699
700
701
        // main_header
702
        $main_headerBlade = file_get_contents(__DIR__
703
            . '/../Stubs/Views/'.$theme_name.'/layouts/main_header/main_header.blade.stub');
704
        $languagesBlade = file_get_contents(__DIR__
705
            . '/../Stubs/Views/'.$theme_name.'/layouts/main_header/languages.blade.stub');
706
        $notificationsBlade = file_get_contents(__DIR__
707
            . '/../Stubs/Views/'.$theme_name.'/layouts/main_header/notifications.blade.stub');
708
        $userBlade = file_get_contents(__DIR__
709
            . '/../Stubs/Views/'.$theme_name.'/layouts/main_header/user.blade.stub');
710
711
        // sidemenu
712
        $itemsBlade = file_get_contents(__DIR__
713
            . '/../Stubs/Views/'.$theme_name.'/layouts/sidemenu/items.blade.stub');
714
        $listBlade = file_get_contents(__DIR__
715
            . '/../Stubs/Views/'.$theme_name.'/layouts/sidemenu/list.blade.stub');
716
717
718
        $createdFolders = $this->createViewsFolders($nameSmall);
719
720
721
        file_put_contents($createdFolders[4].'/layout.blade.php', str_replace([
722
            '{{$nameSmall}}',
723
        ], [
724
            $nameSmall
725
        ], $layoutBlade));
726
727
        file_put_contents($createdFolders[4].'/layout_guest.blade.php', str_replace([
728
            '{{$nameSmall}}',
729
        ], [
730
            $nameSmall
731
        ], $layoutGuestBlade));
732
733
        file_put_contents($createdFolders[0].'/dashboard.blade.php', str_replace([
734
            '{{$nameSmall}}',
735
        ], [
736
            $nameSmall
737
        ], $dashboardBlade));
738
739
        file_put_contents($createdFolders[1].'/login.blade.php', str_replace([
740
            '{{$nameSmall}}',
741
        ], [
742
            $nameSmall
743
        ], $loginBlade));
744
745
        file_put_contents($createdFolders[1].'/register.blade.php', str_replace([
746
            '{{$nameSmall}}',
747
        ], [
748
            $nameSmall
749
        ], $registerBlade));
750
751
        file_put_contents($createdFolders[1].'/verify.blade.php', str_replace([
752
            '{{$nameSmall}}',
753
        ], [
754
            $nameSmall
755
        ], $verifyEmailBlade));
756
757
        file_put_contents($createdFolders[2].'/reset.blade.php', str_replace([
758
            '{{$nameSmall}}',
759
        ], [
760
            $nameSmall
761
        ], $resetBlade));
762
763
        file_put_contents($createdFolders[2].'/email.blade.php', str_replace([
764
            '{{$nameSmall}}',
765
        ], [
766
            $nameSmall
767
        ], $emailBlade));
768
769
        file_put_contents($createdFolders[3].'/update_info.blade.php', str_replace([
770
            '{{$nameSmall}}',
771
        ], [
772
            $nameSmall
773
        ], $update_infoBlade));
774
775
        file_put_contents($createdFolders[3].'/right_box.blade.php', str_replace([
776
            '{{$nameSmall}}',
777
        ], [
778
            $nameSmall
779
        ], $right_boxBlade));
780
781
        file_put_contents($createdFolders[3].'/left_box.blade.php', str_replace([
782
            '{{$nameSmall}}',
783
        ], [
784
            $nameSmall
785
        ], $left_boxBlade));
786
787
        file_put_contents($createdFolders[3].'/change_password_tab.blade.php', str_replace([
788
            '{{$nameSmall}}',
789
        ], [
790
            $nameSmall
791
        ], $change_passwordBlade));
792
793
        file_put_contents($createdFolders[3].'/account_info_tab.blade.php', str_replace([
794
            '{{$nameSmall}}',
795
        ], [
796
            $nameSmall
797
        ], $account_infoBlade));
798
799
        file_put_contents($createdFolders[5].'/alerts.blade.php', str_replace([
800
            '{{$nameSmall}}',
801
        ], [
802
            $nameSmall
803
        ], $alertsBlade));
804
805
        file_put_contents($createdFolders[5].'/breadcrumb.blade.php', str_replace([
806
            '{{$nameSmall}}',
807
        ], [
808
            $nameSmall
809
        ], $breadcrumbBlade));
810
811
        file_put_contents($createdFolders[5].'/head.blade.php', str_replace([
812
            '{{$nameSmall}}',
813
        ], [
814
            $nameSmall
815
        ], $headBlade));
816
817
        file_put_contents($createdFolders[5].'/scripts.blade.php', str_replace([
818
            '{{$nameSmall}}',
819
        ], [
820
            $nameSmall
821
        ], $scriptsBlade));
822
823
        file_put_contents($createdFolders[6].'/languages.blade.php', str_replace([
824
            '{{$nameSmall}}',
825
        ], [
826
            $nameSmall
827
        ], $languagesBlade));
828
829
        file_put_contents($createdFolders[6].'/main_header.blade.php', str_replace([
830
            '{{$nameSmall}}',
831
        ], [
832
            $nameSmall
833
        ], $main_headerBlade));
834
835
        file_put_contents($createdFolders[6].'/notifications.blade.php', str_replace([
836
            '{{$nameSmall}}',
837
        ], [
838
            $nameSmall
839
        ], $notificationsBlade));
840
841
        file_put_contents($createdFolders[6].'/user.blade.php', str_replace([
842
            '{{$nameSmall}}',
843
        ], [
844
            $nameSmall
845
        ], $userBlade));
846
847
        file_put_contents($createdFolders[7].'/items.blade.php', str_replace([
848
            '{{$nameSmall}}',
849
        ], [
850
            $nameSmall
851
        ], $itemsBlade));
852
853
        file_put_contents($createdFolders[7].'/list.blade.php', str_replace([
854
            '{{$nameSmall}}',
855
        ], [
856
            $nameSmall
857
        ], $listBlade));
858
859
    }
860
861
    /**
862
     * @param $nameSmall
863
     * @return string[]
864
     */
865
    protected function createViewsFolders($nameSmall) {
866
867
        // main guard folder
868
        $createFolder = $this->getViewsFolderPath().DIRECTORY_SEPARATOR."$nameSmall";
869
        if (!file_exists($createFolder)) {
870
            mkdir($createFolder);
871
        }
872
873
        // Auth folder
874
        $createFolderAuth = $this->getViewsFolderPath().DIRECTORY_SEPARATOR."$nameSmall"
875
            .DIRECTORY_SEPARATOR."auth";
876
        if (!file_exists($createFolderAuth)) {
877
            mkdir($createFolderAuth);
878
        }
879
880
        $createFolderAuthPasswords = $createFolderAuth.DIRECTORY_SEPARATOR."passwords";
881
        if (!file_exists($createFolderAuthPasswords)) {
882
            mkdir($createFolderAuthPasswords);
883
        }
884
885
        $createFolderAuthAccount = $createFolderAuth.DIRECTORY_SEPARATOR."account";
886
        if (!file_exists($createFolderAuthAccount)) {
887
            mkdir($createFolderAuthAccount);
888
        }
889
890
        // Layout folder
891
        $createFolderLayouts = $this->getViewsFolderPath().DIRECTORY_SEPARATOR
892
            ."$nameSmall"
893
            .DIRECTORY_SEPARATOR."layouts";
894
        if (!file_exists($createFolderLayouts)) {
895
            mkdir($createFolderLayouts);
896
        }
897
898
        $createFolderLayoutsInc = $createFolderLayouts .DIRECTORY_SEPARATOR."inc";
899
        if (!file_exists($createFolderLayoutsInc)) {
900
            mkdir($createFolderLayoutsInc);
901
        }
902
903
        $createFolderLayoutsMainHeader = $createFolderLayouts .DIRECTORY_SEPARATOR."main_header";
904
        if (!file_exists($createFolderLayoutsMainHeader)) {
905
            mkdir($createFolderLayoutsMainHeader);
906
        }
907
908
        $createFolderLayoutsSideMenu = $createFolderLayouts .DIRECTORY_SEPARATOR."sidemenu";
909
        if (!file_exists($createFolderLayoutsSideMenu)) {
910
            mkdir($createFolderLayoutsSideMenu);
911
        }
912
913
        return [
914
            $createFolder,
915
            $createFolderAuth,
916
            $createFolderAuthPasswords,
917
            $createFolderAuthAccount,
918
919
            $createFolderLayouts,
920
            $createFolderLayoutsInc,
921
            $createFolderLayoutsMainHeader,
922
            $createFolderLayoutsSideMenu
923
        ];
924
925
    }
926
927
    /**
928
     * Install Lang.
929
     *
930
     * @return boolean
931
     */
932
933
    public function installLangs()
934
    {
935
        $nameSmall = snake_case($this->getParsedNameInput());
936
937
        $dashboardLangFile = file_get_contents(__DIR__ . '/../Stubs/Languages/dashboard.stub');
938
        file_put_contents($this->getLangsFolderPath().'/'.$nameSmall.'_dashboard.php', $dashboardLangFile);
939
940
        return true;
941
942
    }
943
944
    /**
945
     * Publish Prologue Alert
946
     *
947
     * @return boolean
948
     */
949
    public function installPrologueAlert() {
950
        $alertsConfigFile = $this->getConfigsFolderPath().DIRECTORY_SEPARATOR."prologue/alerts.php";
951
        if (!file_exists($alertsConfigFile)) {
952
            $this->executeProcess('php artisan vendor:publish --provider="Prologue\Alerts\AlertsServiceProvider"',
953
                'publishing config for notifications - prologue/alerts');
954
        }
955
956
        return true;
957
    }
958
959
    /**
960
     * Creating notifications table if not exists
961
     */
962
    public function installNotificationsDatabase() {
963
        $notificationsTableFile = $this->getMigrationPath().DIRECTORY_SEPARATOR."*_create_notifications_table.php";
964
        $globArray = glob($notificationsTableFile);
965
        if (count($globArray) < 1) {
966
            $this->executeProcess('php artisan notifications:table',
967
                'creating notifications table');
968
        }
969
970
    }
971
972
    /**
973
     * Run a SSH command.
974
     *
975
     * @param $command
976
     * @param string $beforeNotice
977
     * @param string $afterNotice
978
     */
979
    public function executeProcess($command, $beforeNotice = '', $afterNotice = '')
980
    {
981
        if (!is_null($beforeNotice)) {
982
            $this->info('### '.$beforeNotice);
983
        } else {
984
            $this->info('### Running: '.$command);
985
        }
986
        $process = new Process($command);
987
        $process->run(function ($type, $buffer) {
988
            if (Process::ERR === $type) {
989
                echo '... > '.$buffer;
990
            } else {
991
                echo 'OUT > '.$buffer;
992
            }
993
        });
994
        // executes after the command finishes
995
        if (!$process->isSuccessful()) {
996
            throw new ProcessFailedException($process);
997
        }
998
        if (!is_null($afterNotice)) {
999
            $this->info('### '.$afterNotice);
1000
        }
1001
    }
1002
1003
    /**
1004
     * Get the desired class name from the input.
1005
     *
1006
     * @return string
1007
     */
1008
    protected function getParsedNameInput()
1009
    {
1010
        return mb_strtolower(str_singular($this->getNameInput()));
1011
    }
1012
    /**
1013
     * Get the desired class name from the input.
1014
     *
1015
     * @return string
1016
     */
1017
    protected function getNameInput()
1018
    {
1019
        $name = $this->argument('name');
1020
        return trim($name);
1021
    }
1022
1023
    /**
1024
     * Write the migration file to disk.
1025
     *
1026
     * @param $name
1027
     * @param $table
1028
     * @param $create
1029
     * @throws \Exception
1030
     */
1031
    protected function writeMigration($name, $table, $create)
1032
    {
1033
        $file = pathinfo($this->creator->create(
1034
            $name, $this->getMigrationPath(), $table, $create
1035
        ), PATHINFO_FILENAME);
1036
        $this->line("<info>Created Migration:</info> {$file}");
1037
    }
1038
1039
    /**
1040
     * Get migration path.
1041
     *
1042
     * @return string
1043
     */
1044
    protected function getMigrationPath()
1045
    {
1046
        return parent::getMigrationPath();
1047
    }
1048
1049
    /**
1050
     * Get Routes Provider Path.
1051
     *
1052
     * @return string
1053
     */
1054
    protected function getRouteServicesPath()
1055
    {
1056
        return $this->getAppFolderPath().DIRECTORY_SEPARATOR.'Providers'.DIRECTORY_SEPARATOR.'RouteServiceProvider.php';
1057
    }
1058
1059
    /**
1060
     * Get Routes Folder Path.
1061
     *
1062
     * @return string
1063
     */
1064
    protected function getAppFolderPath()
1065
    {
1066
        return $this->laravel->basePath().DIRECTORY_SEPARATOR.'app';
1067
    }
1068
1069
    /**
1070
     * Get Routes Folder Path.
1071
     *
1072
     * @return string
1073
     */
1074
    protected function getRoutesFolderPath()
1075
    {
1076
        return $this->laravel->basePath().DIRECTORY_SEPARATOR.'routes';
1077
    }
1078
1079
    /**
1080
     * Get Config Folder Path.
1081
     *
1082
     * @return string
1083
     */
1084
    protected function getConfigsFolderPath()
1085
    {
1086
        return $this->laravel->basePath().DIRECTORY_SEPARATOR.'config';
1087
    }
1088
1089
    /**
1090
     * Get Lang Folder Path.
1091
     *
1092
     * @return string
1093
     */
1094
    protected function getLangsFolderPath()
1095
    {
1096
        return $this->laravel->basePath().DIRECTORY_SEPARATOR.'resources'.DIRECTORY_SEPARATOR.'lang'.DIRECTORY_SEPARATOR.'en';
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 126 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
1097
    }
1098
1099
    /**
1100
     * Get Views Folder Path.
1101
     *
1102
     * @return string
1103
     */
1104
    protected function getViewsFolderPath()
1105
    {
1106
        return $this->laravel->basePath().DIRECTORY_SEPARATOR.'resources'.DIRECTORY_SEPARATOR.'views';
1107
    }
1108
1109
    /**
1110
     * Get Controllers Path.
1111
     *
1112
     * @return string
1113
     */
1114
    protected function getControllersPath()
1115
    {
1116
        return $this->getAppFolderPath().DIRECTORY_SEPARATOR.'Http'.DIRECTORY_SEPARATOR.'Controllers';
1117
    }
1118
1119
    /**
1120
     * Get Http Path.
1121
     *
1122
     * @return string
1123
     */
1124
    protected function getHttpPath()
1125
    {
1126
        return $this->getAppFolderPath().DIRECTORY_SEPARATOR.'Http';
1127
    }
1128
1129
    /**
1130
     * Get Middleware Path.
1131
     *
1132
     * @return string
1133
     */
1134
    protected function getMiddlewarePath()
1135
    {
1136
        return $this->getAppFolderPath().DIRECTORY_SEPARATOR.'Http'.DIRECTORY_SEPARATOR.'Middleware';
1137
    }
1138
1139
    /**
1140
     * insert text into file
1141
     *
1142
     * @param string $filePath
1143
     * @param string $insertMarker
1144
     * @param string $text
1145
     * @param boolean $after
1146
     *
1147
     * @return integer
1148
     */
1149
    public function insertIntoFile($filePath, $insertMarker, $text, $after = true) {
1150
        $contents = file_get_contents($filePath);
1151
        $new_contents = preg_replace($insertMarker,($after) ? '$0' . $text : $text . '$0', $contents);
1152
        return file_put_contents($filePath, $new_contents);
1153
    }
1154
1155
    /**
1156
     * insert text into file
1157
     *
1158
     * @param string $filePath
1159
     * @param string $keyword
1160
     * @param string $body
1161
     * @param boolean $after
1162
     *
1163
     * @return integer
1164
     */
1165
    public function insert($filePath, $keyword, $body, $after = true) {
1166
1167
        $contents = file_get_contents($filePath);
1168
        $new_contents = substr_replace($contents, PHP_EOL . $body,
1169
            ($after) ? strpos($contents, $keyword) + strlen($keyword) : strpos($contents, $keyword)
1170
            , 0);
1171
        return file_put_contents($filePath, $new_contents);
1172
    }
1173
}
1174