checkIfCRUDViewsExistsOrNot()   A
last analyzed

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\IMGenerateCrudCommand\Command;
4
5
use Illuminate\Console\Command;
6
use Illuminate\Database\Console\Migrations\BaseCommand;
7
use Illuminate\Support\Composer;
8
use Illuminate\Database\Migrations\MigrationCreator;
9
use Illuminate\Support\Facades\Artisan;
10
use Illuminate\Support\Str;
11
use Symfony\Component\Process\Exception\ProcessFailedException;
12
use Symfony\Component\Process\Process;
13
use Illuminate\Support\Arr;
14
15
class IMGenerateCrudCommand extends BaseCommand
16
{
17
    /**
18
     * The name and signature of the console command.
19
     *
20
     * @var string
21
     */
22
    protected $signature = 'make:im_crud {name} {guard_name} {--admin_theme= : chose the theme you want} {--model} : create crud model {--migration} : create crud migration';
23
24
    /**
25
     * The console command description.
26
     *
27
     * @var string
28
     */
29
    protected $description = 'create crud system for guards generated by iMokhles\'s MultiAuth package';
30
31
    /**
32
     * @var
33
     */
34
    protected $progressBar;
35
36
    /**
37
     * Create a new command instance.
38
     *
39
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
40
     */
41
    public function __construct()
42
    {
43
        parent::__construct();
44
    }
45
46
    /**
47
     * Execute the console command.
48
     *
49
     * @return mixed
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use boolean.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
50
     */
51
    public function handle()
52
    {
53
        //
54
        $this->progressBar = $this->output->createProgressBar(15);
55
        $this->progressBar->start();
56
57
        $this->line(" Preparing For CRUD. Please wait...");
58
        $this->progressBar->advance();
59
60
        $name = ucfirst($this->getParsedNameInput());
61
        $guardName = ucfirst($this->getParsedGuardNameInput());
62
63
        $admin_theme = $this->option('admin_theme');
64
        if (is_null($admin_theme)) {
65
            $admin_theme = 'oneui';
66
        }
67
68
        if ($this->checkIfThemeFileExistsOrNot($admin_theme)) {
69
            if (!$this->checkIfResponseHelperExistsOrNot()) {
70
                $this->line(" installing response...");
71
                $this->installResponseHelper();
72
                $this->progressBar->advance();
73
            }
74
75
            if (!$this->checkIfAppBaseControllerExistsOrNot()) {
76
                $this->line(" installing appbasecontroller...");
77
                $this->installAppBaseController();
78
                $this->progressBar->advance();
79
            }
80
81
            if (!$this->checkIfCRUDControllerExistsOrNot()) {
82
                $this->line(" installing crudcontroller...");
83
                $this->installCRUDController();
84
                $this->progressBar->advance();
85
            }
86
87
            if (!$this->checkIfBaseRepositoryExistsOrNot()) {
88
                $this->line(" installing baserepository...");
89
                $this->installBaseRepository();
90
                $this->progressBar->advance();
91
            }
92
93
            if (!$this->checkIfCRUDRepositoryExistsOrNot()) {
94
                $this->line(" installing crudrepository...");
95
                $this->installCRUDRepository();
96
                $this->progressBar->advance();
97
            }
98
99
            if (!$this->checkIfCRUDRequestsExistsOrNot()) {
100
                $this->line(" installing crudrequests...");
101
                $this->installCRUDRequests();
102
                $this->progressBar->advance();
103
            }
104
105
            if ($this->checkIfGuardRouteExistsOrNot()) {
106
                if (!$this->checkIfCRUDRouteExistsOrNot()) {
107
                    $this->line(" installing crudroute...");
108
                    $this->installCRUDRoute();
109
                    $this->progressBar->advance();
110
                }
111
            }
112
113
            if (!$this->checkIfCRUDViewsExistsOrNot()) {
114
                $this->line(" installing crudviews...");
115
                $this->installCRUDFields($admin_theme);
116
                $this->installCRUDViews($admin_theme);
117
                $this->progressBar->advance();
118
            }
119
120
            if ($this->option('model') == true) {
121
                if (!$this->checkIfCRUDModelExistsOrNot()) {
122
                    $this->installCRUDModel();
123
                }
124
            }
125
126
            if ($this->option('migration') == true) {
127
                if (!$this->checkIfCRUDMigrationExistsOrNot()) {
128
                    $this->installCRUDMigration();
129
                }
130
            }
131
132
            $this->progressBar->finish();
133
            $this->info(" finished ".$name." CRUD for Guard ".$guardName." setup.");
134
135
        } else {
136
            $this->progressBar->advance();
137
            $this->progressBar->finish();
138
            $this->line(" failed: ".$admin_theme." theme not found");
139
        }
140
        return true;
141
    }
142
143
    /**
144
     * @param $admin_theme
145
     * @return bool
146
     */
147
    private function checkIfThemeFileExistsOrNot($admin_theme) {
148
        $this->line(__DIR__ . "..".'/Stubs/Views/'.$admin_theme.'/table.blade.stub');
149
        return file_exists(__DIR__ . "..".'/Stubs/Views/'.$admin_theme.'/table.blade.stub');
150
    }
151
152
    /**
153
     * @return bool
154
     */
155
    private function checkIfResponseHelperExistsOrNot() {
156
        return file_exists($this->getHelpersPath().DIRECTORY_SEPARATOR.'ResponseHelper.php');
157
    }
158
159
    /**
160
     * @return bool
161
     */
162
    private function checkIfAppBaseControllerExistsOrNot() {
163
        return file_exists($this->getGuardCRUDControllersPath().DIRECTORY_SEPARATOR.'AppBaseController.php');
164
    }
165
166
    /**
167
     * @return bool
168
     */
169
    private function checkIfCRUDControllerExistsOrNot() {
170
        $name = ucfirst($this->getParsedNameInput());
171
        return file_exists($this->getGuardCRUDControllersPath().DIRECTORY_SEPARATOR.$name.'Controller.php');
172
    }
173
174
    /**
175
     * @return bool
176
     */
177
    private function checkIfBaseRepositoryExistsOrNot() {
178
        return file_exists($this->getRepositoriesPath().DIRECTORY_SEPARATOR.'BaseRepository.php');
179
    }
180
181
    /**
182
     * @return bool
183
     */
184
    private function checkIfCRUDRepositoryExistsOrNot() {
185
        $name = ucfirst($this->getParsedNameInput());
186
        return file_exists($this->getRepositoriesPath().DIRECTORY_SEPARATOR.$name.'Repository.php');
187
    }
188
189
    /**
190
     * @return bool
191
     */
192
    private function checkIfCRUDRequestsExistsOrNot() {
193
        $name = ucfirst($this->getParsedNameInput());
194
        return (file_exists($this->getGuardRequestsControllersPath().DIRECTORY_SEPARATOR.'Create'.$name.'Request.php')
195
        || file_exists($this->getGuardRequestsControllersPath().DIRECTORY_SEPARATOR.'Update'.$name.'Request.php'));
196
    }
197
198
    /**
199
     * @return bool
200
     */
201
    private function checkIfGuardRouteExistsOrNot() {
202
        $guardNameSmall = Str::snake($this->getParsedGuardNameInput());
203
        return file_exists($this->getGuardRoutesFolderPath().DIRECTORY_SEPARATOR.$guardNameSmall.'.php');
204
    }
205
206
    /**
207
     * @return bool
208
     */
209
    private function checkIfCRUDViewsExistsOrNot() {
210
        $name = ucfirst($this->getParsedNameInput());
0 ignored issues
show
Unused Code introduced by
$name is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
211
        return file_exists($this->getCRUDViewsFolderPath().DIRECTORY_SEPARATOR.'create.blade.php');
212
    }
213
214
    /**
215
     * @return bool
216
     */
217
    private function checkIfCRUDModelExistsOrNot() {
218
        $name = ucfirst($this->getParsedNameInput());
219
        return file_exists($this->getModelsPath().DIRECTORY_SEPARATOR.$name.'.php');
220
    }
221
222
    /**
223
     * Creating notifications table if not exists
224
     */
225
    public function checkIfCRUDMigrationExistsOrNot() {
226
        $nameSmallPlural = Str::plural(Str::snake($this->getParsedNameInput()));
227
        $notificationsTableFile = $this->getMigrationPath().DIRECTORY_SEPARATOR."*_create_".$nameSmallPlural."_table.php";
228
        $globArray = glob($notificationsTableFile);
229
        if (count($globArray) < 1) {
0 ignored issues
show
Unused Code introduced by
This if statement, and the following return statement can be replaced with return !(count($globArray) < 1);.
Loading history...
230
            return false;
231
        }
232
        return true;
233
    }
234
235
    /**
236
     * @return bool
237
     */
238
    private function checkIfCRUDRouteExistsOrNot() {
239
        $nameSmallPlural = Str::plural(Str::snake($this->getParsedNameInput()));
240
        $name = ucfirst($this->getParsedNameInput());
241
        $guardNameSmall = Str::snake($this->getParsedGuardNameInput());
242
        $fileContent = file_get_contents( $this->getGuardRoutesFolderPath().DIRECTORY_SEPARATOR.$guardNameSmall.'.php');
243
        $fileContent2 = file_get_contents(__DIR__ . "..".'/Stubs/Route/route.stub');
244
        $fileContentRoutes = str_replace([
245
            '{{$name}}',
246
            '{{$nameSmallPlural}}',
247
        ], [
248
            $name,
249
            $nameSmallPlural
250
        ], $fileContent2);
251
        return (Str::contains($fileContent, $fileContentRoutes));
252
    }
253
254
    /**
255
     * Install Response Helper.
256
     *
257
     * @return boolean
258
     */
259 View Code Duplication
    public function installResponseHelper()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
260
    {
261
        $createFolder = $this->getHelpersPath();
262
        if (!file_exists($createFolder)) {
263
            mkdir($createFolder);
264
        }
265
266
        $fileContent = file_get_contents(__DIR__ . "..".'/Stubs/Helpers/ResponseHelper.stub');
267
268
        $filePath = $createFolder.DIRECTORY_SEPARATOR."ResponseHelper.php";
269
        file_put_contents($filePath, $fileContent);
270
271
        return true;
272
    }
273
274
    /**
275
     * Install AppBaseController Helper.
276
     *
277
     * @return boolean
278
     */
279
    public function installAppBaseController()
280
    {
281
        $guardName = ucfirst($this->getParsedGuardNameInput());
282
        $createFolder = $this->getGuardCRUDControllersPath();
283
        if (!file_exists($createFolder)) {
284
            mkdir($createFolder);
285
        }
286
287
        $fileContent = file_get_contents(__DIR__ . "..".'/Stubs/Controllers/AppBaseController.stub');
288
        $fileContentNew = str_replace([
289
            '{{$guardName}}',
290
        ], [
291
            $guardName,
292
        ], $fileContent);
293
294
        $filePath = $createFolder.DIRECTORY_SEPARATOR."AppBaseController.php";
295
        file_put_contents($filePath, $fileContentNew);
296
297
        return true;
298
    }
299
300
    /**
301
     * Install CRUDController Helper.
302
     *
303
     * @return boolean
304
     */
305
    public function installCRUDController()
306
    {
307
        $nameSmall =  Str::snake($this->getParsedNameInput());
308
        $nameSmallPlural = Str::plural(Str::snake($this->getParsedNameInput()));
309
        $name = ucfirst($this->getParsedNameInput());
310
        $namePlural =  Str::plural($name);
311
312
        $guardNameSmall = Str::snake($this->getParsedGuardNameInput());
313
        $guardName = ucfirst($this->getParsedGuardNameInput());
314
315
316
        $createFolder = $this->getGuardCRUDControllersPath();
317
        if (!file_exists($createFolder)) {
318
            mkdir($createFolder);
319
        }
320
321
        $fileContent = file_get_contents(__DIR__ . "..".'/Stubs/Controllers/Controller.stub');
322
        $fileContentNew = str_replace([
323
            '{{$namePlural}}',
324
            '{{$nameSmallPlural}}',
325
            '{{$nameSmall}}',
326
            '{{$name}}',
327
            '{{$guardName}}',
328
            '{{$guardNameSmall}}'
329
        ], [
330
            $namePlural,
331
            $nameSmallPlural,
332
            $nameSmall,
333
            $name,
334
            $guardName,
335
            $guardNameSmall
336
        ], $fileContent);
337
338
339
        $filePath = $createFolder.DIRECTORY_SEPARATOR.$name."Controller.php";
340
        file_put_contents($filePath, $fileContentNew);
341
342
        $this->line(" created controller file ".$name."Controller.php");
343
        return true;
344
    }
345
346
    /**
347
     * Install BaseRepository Helper.
348
     *
349
     * @return boolean
350
     */
351 View Code Duplication
    public function installBaseRepository()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
352
    {
353
354
        $createFolder = $this->getRepositoriesPath();
355
        if (!file_exists($createFolder)) {
356
            mkdir($createFolder);
357
        }
358
359
        $fileContent = file_get_contents(__DIR__ . "..".'/Stubs/Repositories/BaseRepository.stub');
360
361
362
        $filePath = $createFolder.DIRECTORY_SEPARATOR."BaseRepository.php";
363
        file_put_contents($filePath, $fileContent);
364
365
        return true;
366
    }
367
368
    /**
369
     * Install CRUDRepository Helper.
370
     *
371
     * @return boolean
372
     */
373
    public function installCRUDRepository()
374
    {
375
        $name = ucfirst($this->getParsedNameInput());
376
377
        $createFolder = $this->getRepositoriesPath();
378
        if (!file_exists($createFolder)) {
379
            mkdir($createFolder);
380
        }
381
382
        $fileContent = file_get_contents(__DIR__ . "..".'/Stubs/Repositories/Repository.stub');
383
        $fileContentNew = str_replace([
384
            '{{$name}}',
385
        ], [
386
            $name,
387
        ], $fileContent);
388
389
390
        $filePath = $createFolder.DIRECTORY_SEPARATOR.$name."Repository.php";
391
        file_put_contents($filePath, $fileContentNew);
392
393
        $this->line(" created repository file ".$name."Repository.php");
394
395
        return true;
396
    }
397
398
    /**
399
     * Install CRUDRequests Helper.
400
     *
401
     * @return boolean
402
     */
403
    public function installCRUDRequests()
404
    {
405
        $name = ucfirst($this->getParsedNameInput());
406
        $guardName = ucfirst($this->getParsedGuardNameInput());
407
408
        $createFolder = $this->getGuardRequestsControllersPath();
409
        if (!file_exists($createFolder)) {
410
            mkdir($createFolder);
411
        }
412
413
        $fileContent = file_get_contents(__DIR__ . "..".'/Stubs/Requests/CreateRequest.stub');
414
        $fileContentNew = str_replace([
415
            '{{$name}}',
416
            '{{$guardName}}',
417
        ], [
418
            $name,
419
            $guardName
420
        ], $fileContent);
421
422
423
        $filePath = $createFolder.DIRECTORY_SEPARATOR."Create".$name."Request.php";
424
        file_put_contents($filePath, $fileContentNew);
425
426
        $this->line(" created CreateRequest file "."Create".$name."Request.php");
427
428
        $fileContent = file_get_contents(__DIR__ . "..".'/Stubs/Requests/UpdateRequest.stub');
429
        $fileContentNew = str_replace([
430
            '{{$name}}',
431
            '{{$guardName}}',
432
        ], [
433
            $name,
434
            $guardName
435
        ], $fileContent);
436
437
438
        $filePath = $createFolder.DIRECTORY_SEPARATOR."Update".$name."Request.php";
439
        file_put_contents($filePath, $fileContentNew);
440
441
        $this->line(" created UpdateRequest file "."Update".$name."Request.php");
442
        return true;
443
    }
444
445
    /**
446
     * Install CRUDRoute Helper.
447
     *
448
     * @return boolean
449
     */
450
    public function installCRUDRoute()
451
    {
452
        $name = ucfirst($this->getParsedNameInput());
453
        $nameSmallPlural = Str::plural(Str::snake($this->getParsedNameInput()));
454
        $guardNameSmall = Str::snake($this->getParsedGuardNameInput());
455
456
        $createFolder = $this->getGuardRoutesFolderPath();
457
        if (!file_exists($createFolder)) {
458
            mkdir($createFolder);
459
        }
460
461
        $fileContent = file_get_contents(__DIR__ . "..".'/Stubs/Route/route.stub');
462
        $fileContentRoutes = str_replace([
463
            '{{$name}}',
464
            '{{$nameSmallPlural}}',
465
        ], [
466
            $name,
467
            $nameSmallPlural
468
        ], $fileContent);
469
470
471
472
        $filePath = $this->getGuardRoutesFolderPath().DIRECTORY_SEPARATOR.$guardNameSmall.'.php';
473
        $fileContentNew = file_get_contents($filePath);
474
        if (file_exists($filePath)) {
475
            unlink($filePath);
476
        }
477
        file_put_contents($filePath, $fileContentNew.PHP_EOL.$fileContentRoutes);
478
479
        return true;
480
    }
481
482
    /**
483
     * Install CRUDFields Helper.
484
     *
485
     * @param string $theme_name
486
     * @return boolean
487
     */
488
    public function installCRUDFields($theme_name = 'adminlte2')
489
    {
490
        $name = ucfirst($this->getParsedNameInput());
0 ignored issues
show
Unused Code introduced by
$name is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
491
        $nameSmallPlural = Str::plural(Str::snake($this->getParsedNameInput()));
0 ignored issues
show
Unused Code introduced by
$nameSmallPlural is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
492
        $createFolder = $this->getCRUDViewsFolderPath().DIRECTORY_SEPARATOR.'fields';
493
        if (!file_exists($createFolder)) {
494
            mkdir($createFolder);
495
        }
496
497
        if (!file_exists($createFolder.DIRECTORY_SEPARATOR."textarea.blade.php")) {
498
            $fieldsArray = [
499
                __DIR__ . "..".'/Stubs/Views/'.$theme_name.'/fields/checkbox.blade.stub',
500
                __DIR__ . "..".'/Stubs/Views/'.$theme_name.'/fields/datepicker.blade.stub',
501
                __DIR__ . "..".'/Stubs/Views/'.$theme_name.'/fields/datetimepicker.blade.stub',
502
                __DIR__ . "..".'/Stubs/Views/'.$theme_name.'/fields/email.blade.stub',
503
                __DIR__ . "..".'/Stubs/Views/'.$theme_name.'/fields/password.blade.stub',
504
                __DIR__ . "..".'/Stubs/Views/'.$theme_name.'/fields/select2.blade.stub',
505
                __DIR__ . "..".'/Stubs/Views/'.$theme_name.'/fields/summernote.blade.stub',
506
                __DIR__ . "..".'/Stubs/Views/'.$theme_name.'/fields/switch.blade.stub',
507
                __DIR__ . "..".'/Stubs/Views/'.$theme_name.'/fields/text.blade.stub',
508
                __DIR__ . "..".'/Stubs/Views/'.$theme_name.'/fields/textarea.blade.stub',
509
            ];
510
            foreach ($fieldsArray as $field) {
511
512
                $fieldFileName = basename($field, ".stub");
513
                $fieldFileName = $fieldFileName.".php";
514
                $newFieldPath = $createFolder.DIRECTORY_SEPARATOR.$fieldFileName;
515
516
                copy($field, $newFieldPath);
517
518
                $this->line(" created field file $fieldFileName");
519
            }
520
        }
521
522
        return true;
523
    }
524
525
    /**
526
     * Install CRUDFields Helper.
527
     *
528
     * @param string $theme_name
529
     * @return boolean
530
     */
531
    public function installCRUDViews($theme_name = 'adminlte2')
532
    {
533
        $nameSmall =  Str::snake($this->getParsedNameInput());
534
        $nameSmallPlural = Str::plural(Str::snake($this->getParsedNameInput()));
535
        $name = ucfirst($this->getParsedNameInput());
536
        $namePlural =  Str::plural($name);
537
538
        $guardNameSmall = Str::snake($this->getParsedGuardNameInput());
539
        $guardName = ucfirst($this->getParsedGuardNameInput());
0 ignored issues
show
Unused Code introduced by
$guardName is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
540
541
        $createFolder = $this->getCRUDViewsFolderPath().DIRECTORY_SEPARATOR.$nameSmallPlural;
542
        if (!file_exists($createFolder)) {
543
            mkdir($createFolder);
544
        }
545
546
        $viewsArray = [
547
            [
548
                'file' => __DIR__ . "..".'/Stubs/Views/'.$theme_name.'/create.blade.stub',
549
                'originals' => [
550
                    '{{$guardNameSmall}}',
551
                    '{{$namePlural}}',
552
                    '{{$nameSmallPlural}}',
553
                ],
554
                'replaces' => [
555
                    $guardNameSmall,
556
                    $namePlural,
557
                    $nameSmallPlural
558
                ]
559
            ],
560
            [
561
                'file' => __DIR__ . "..".'/Stubs/Views/'.$theme_name.'/create_fields.blade.stub',
562
                'originals' => [
563
                    '{{$nameSmallPlural}}',
564
                ],
565
                'replaces' => [
566
                    $nameSmallPlural
567
                ]
568
            ],
569
            [
570
                'file' => __DIR__ . "..".'/Stubs/Views/'.$theme_name.'/edit.blade.stub',
571
                'originals' => [
572
                    '{{$guardNameSmall}}',
573
                    '{{$namePlural}}',
574
                    '{{$nameSmallPlural}}',
575
                    '{{$nameSmall}}',
576
                ],
577
                'replaces' => [
578
                    $guardNameSmall,
579
                    $namePlural,
580
                    $nameSmallPlural,
581
                    $nameSmall
582
                ]
583
            ],
584
            [
585
                'file' => __DIR__ . "..".'/Stubs/Views/'.$theme_name.'/edit_fields.blade.stub',
586
                'originals' => [
587
                    '{{$nameSmallPlural}}',
588
                    '{{$nameSmall}}',
589
                ],
590
                'replaces' => [
591
                    $nameSmallPlural,
592
                    $nameSmall
593
                ]
594
            ],
595
            [
596
                'file' => __DIR__ . "..".'/Stubs/Views/'.$theme_name.'/index.blade.stub',
597
                'originals' => [
598
                    '{{$guardNameSmall}}',
599
                    '{{$namePlural}}',
600
                    '{{$nameSmallPlural}}',
601
                ],
602
                'replaces' => [
603
                    $guardNameSmall,
604
                    $namePlural,
605
                    $nameSmallPlural
606
                ]
607
            ],
608
            [
609
                'file' => __DIR__ . "..".'/Stubs/Views/'.$theme_name.'/show.blade.stub',
610
                'originals' => [
611
                    '{{$guardNameSmall}}',
612
                    '{{$namePlural}}',
613
                    '{{$nameSmallPlural}}',
614
                ],
615
                'replaces' => [
616
                    $guardNameSmall,
617
                    $namePlural,
618
                    $nameSmallPlural
619
                ]
620
            ],
621
            [
622
                'file' => __DIR__ . "..".'/Stubs/Views/'.$theme_name.'/show_fields.blade.stub',
623
                'originals' => [
624
                    '{{$nameSmall}}'
625
                ],
626
                'replaces' => [
627
                    $nameSmall
628
                ]
629
            ],
630
            [
631
                'file' => __DIR__ . "..".'/Stubs/Views/'.$theme_name.'/table.blade.stub',
632
                'originals' => [
633
                    '{{$nameSmallPlural}}',
634
                    '{{$nameSmall}}'
635
                ],
636
                'replaces' => [
637
                    $nameSmallPlural,
638
                    $nameSmall
639
                ]
640
            ],
641
        ];
642
        foreach ($viewsArray as $view) {
643
644
            $fileContent = file_get_contents($view['file']);
645
            if (count($view['originals'])) {
646
                $fileContent = str_replace($view['originals'], $view['replaces'], $fileContent);
647
            }
648
649
650
            $viewFileName = basename($view['file'], ".stub");
651
            $viewFileName = $viewFileName.".php";
652
            $newViewPath = $createFolder.DIRECTORY_SEPARATOR.$viewFileName;
653
654
            file_put_contents($newViewPath, $fileContent);
655
656
            $this->line(" created view file $viewFileName");
657
658
        }
659
660
        return true;
661
    }
662
663
    /**
664
     * Install CRUDModel.
665
     *
666
     * @return boolean
667
     */
668
    public function installCRUDModel()
669
    {
670
        $nameSmallPlural = Str::plural(Str::snake($this->getParsedNameInput()));
671
        $name = ucfirst($this->getParsedNameInput());
672
        $createFolder = $this->getModelsPath();
673
        if (!file_exists($createFolder)) {
674
            mkdir($createFolder);
675
        }
676
677
        $fileContent = file_get_contents(__DIR__ . "..".'/Stubs/Models/model.stub');
678
        $fileContentNew = str_replace([
679
            '{{$name}}',
680
            '{{$nameSmallPlural}}',
681
        ], [
682
            $name,
683
            $nameSmallPlural,
684
        ], $fileContent);
685
686
        $filePath = $createFolder.DIRECTORY_SEPARATOR.$name.".php";
687
        file_put_contents($filePath, $fileContentNew);
688
689
        $this->line(" created model file $name.php");
690
691
        return true;
692
    }
693
694
    /**
695
     * Install CRUDMigration.
696
     *
697
     * @return boolean
698
     */
699
    public function installCRUDMigration()
700
    {
701
        $nameSmallPlural = Str::plural(Str::snake($this->getParsedNameInput()));
702
        $name = ucfirst($this->getParsedNameInput());
703
        $namePlural =  Str::plural($name);
704
705
        $createFolder = $this->getModelsPath();
706
        if (!file_exists($createFolder)) {
707
            mkdir($createFolder);
708
        }
709
710
        $fileContent = file_get_contents(__DIR__ . "..".'/Stubs/Migrations/modelTable.stub');
711
        $fileContentNew = str_replace([
712
            '{{$namePlural}}',
713
            '{{$nameSmallPlural}}',
714
        ], [
715
            $namePlural,
716
            $nameSmallPlural,
717
        ], $fileContent);
718
719
        $migrationName = date('Y_m_d_His') . '_'.'create_' . $nameSmallPlural .'_table.php';
720
        $migrationModelPath = $this->getMigrationPath().DIRECTORY_SEPARATOR.$migrationName;
721
        file_put_contents($migrationModelPath, $fileContentNew);
722
723
        $this->line(" created migration file $migrationName");
724
725
        return true;
726
    }
727
728
    /**
729
     * Get the desired class name from the input.
730
     *
731
     * @return string
732
     */
733
    protected function getParsedNameInput()
734
    {
735
        return mb_strtolower( Str::singular($this->getNameInput()));
736
    }
737
738
    /**
739
     * Get the desired class name from the input.
740
     *
741
     * @return string
742
     */
743
    protected function getNameInput()
744
    {
745
        $name = $this->argument('name');
746
        return trim($name);
747
    }
748
749
    /**
750
     * Get the desired class guard name from the input.
751
     *
752
     * @return string
753
     */
754
    protected function getParsedGuardNameInput()
755
    {
756
        return mb_strtolower( Str::singular($this->getGuardNameInput()));
757
    }
758
759
    /**
760
     * Get the desired class guard name from the input.
761
     *
762
     * @return string
763
     */
764
    protected function getGuardNameInput()
765
    {
766
        $name = $this->argument('guard_name');
767
        return trim($name);
768
    }
769
770
    /**
771
     * Get Views Folder Path.
772
     *
773
     * @return string
774
     */
775
    protected function getViewsFolderPath()
776
    {
777
        return $this->laravel->basePath().DIRECTORY_SEPARATOR.'resources'.DIRECTORY_SEPARATOR.'views';
778
    }
779
780
    /**
781
     * Get CRUD Views Folder Path.
782
     *
783
     * @return string
784
     */
785
    protected function getCRUDViewsFolderPath()
786
    {
787
        $crudDirectory = $this->getViewsFolderPath().DIRECTORY_SEPARATOR.'crud';
788
        if (!file_exists($crudDirectory)) {
789
            mkdir($crudDirectory);
790
        }
791
        return $crudDirectory;
792
    }
793
794
    /**
795
     * Get Guard CRUD Controllers Path.
796
     *
797
     * @return string
798
     */
799
    protected function getGuardCRUDControllersPath()
800
    {
801
        return $this->getGuardControllersPath().DIRECTORY_SEPARATOR.'CRUD';
802
    }
803
804
    /**
805
     * Get Guard CRUD Controllers Path.
806
     *
807
     * @return string
808
     */
809
    protected function getGuardRequestsControllersPath()
810
    {
811
        return $this->getGuardControllersPath().DIRECTORY_SEPARATOR.'Requests';
812
    }
813
814
    /**
815
     * Get Guard Controllers Path.
816
     *
817
     * @return string
818
     */
819
    protected function getGuardControllersPath()
820
    {
821
        $guardName = ucfirst($this->getParsedGuardNameInput());
822
        return $this->getControllersPath().DIRECTORY_SEPARATOR.$guardName;
823
    }
824
825
    /**
826
     * Get Controllers Path.
827
     *
828
     * @return string
829
     */
830
    protected function getControllersPath()
831
    {
832
        return $this->getAppFolderPath().DIRECTORY_SEPARATOR.'Http'.DIRECTORY_SEPARATOR.'Controllers';
833
    }
834
835
    /**
836
     * Get Repositories Path.
837
     *
838
     * @return string
839
     */
840
    protected function getRepositoriesPath()
841
    {
842
        return $this->getAppFolderPath().DIRECTORY_SEPARATOR.'Repositories';
843
    }
844
845
    /**
846
     * Get Helpers Path.
847
     *
848
     * @return string
849
     */
850
    protected function getHelpersPath()
851
    {
852
        return $this->getAppFolderPath().DIRECTORY_SEPARATOR.'Helpers';
853
    }
854
855
    /**
856
     * Get Http Path.
857
     *
858
     * @return string
859
     */
860
    protected function getHttpPath()
861
    {
862
        return $this->getAppFolderPath().DIRECTORY_SEPARATOR.'Http';
863
    }
864
865
    /**
866
     * Get Models Path.
867
     *
868
     * @return string
869
     */
870
    protected function getModelsPath()
871
    {
872
        return $this->getAppFolderPath().DIRECTORY_SEPARATOR.'Models';
873
    }
874
875
    /**
876
     * Get Routes Folder Path.
877
     *
878
     * @return string
879
     */
880
    protected function getRoutesFolderPath()
881
    {
882
        return $this->laravel->basePath().DIRECTORY_SEPARATOR.'routes';
883
    }
884
885
    /**
886
     * Get GuardRoutes Folder Path.
887
     *
888
     * @return string
889
     */
890
    protected function getGuardRoutesFolderPath()
891
    {
892
        $guardNameSmall = Str::snake($this->getParsedGuardNameInput());
893
        return $this->laravel->basePath().DIRECTORY_SEPARATOR.'routes'.DIRECTORY_SEPARATOR.$guardNameSmall;
894
    }
895
896
    /**
897
     * Get migration path.
898
     *
899
     * @return string
900
     */
901
    protected function getMigrationPath()
902
    {
903
        return parent::getMigrationPath();
904
    }
905
906
    /**
907
     * Get Routes Folder Path.
908
     *
909
     * @return string
910
     */
911
    protected function getAppFolderPath()
912
    {
913
        return $this->laravel->basePath().DIRECTORY_SEPARATOR.'app';
914
    }
915
}
916