Passed
Push — main ( 275957...473697 )
by PRATIK
04:03
created

CRUDGeneratorService::fileMadeSuccess()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 3
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Pratiksh\Adminetic\Services;
4
5
use Illuminate\Support\Facades\Artisan;
6
use Illuminate\Support\Str;
7
use Pratiksh\Adminetic\Services\Helper\CommandHelper;
8
9
class CRUDGeneratorService extends CommandHelper
10
{
11
    public static function makeCRUD($name, $console)
12
    {
13
        self::makeController($name, $console);
14
        self::makeModel($name, $console);
15
        self::makeViews($name, $console);
16
        self::makeOthers($name, $console);
17
    }
18
19
    // Make Controller
20
    protected static function makeController($name, $console)
21
    {
22
        if (!file_exists($path = app_path('/Http/Controllers/Admin'))) {
23
            mkdir($path, 0777, true);
24
        }
25
        $controllerTemplate = str_replace(
26
            [
27
                '{{modelName}}',
28
                '{{modelNamePluralLowerCase}}',
29
                '{{modelNameSingularLowerCase}}',
30
            ],
31
            [
32
                $name,
33
                strtolower(Str::plural($name)),
34
                strtolower($name),
35
            ],
36
            self::getStub('Controller')
37
        );
38
        $file = app_path("/Http/Controllers/Admin/{$name}Controller.php");
39
        file_put_contents(app_path("/Http/Controllers/Admin/{$name}Controller.php"), $controllerTemplate);
40
        self::fileMadeSuccess($console, $file, 'Controller');
41
    }
42
43
    // Make Model
44
    protected static function makeModel($name, $console)
45
    {
46
        if (!file_exists($path = app_path('/Models/Admin'))) {
47
            mkdir($path, 0777, true);
48
        }
49
        $modelTemplate = str_replace(
50
            [
51
                '{{modelName}}',
52
                '{{modelNamePluralLowerCase}}',
53
                '{{modelNameSingularLowerCase}}',
54
            ],
55
            [
56
                $name,
57
                strtolower(Str::plural($name)),
58
                strtolower($name),
59
            ],
60
            self::getStub('Model')
61
        );
62
        $file = app_path("/Models/Admin/{$name}.php");
63
        file_put_contents(app_path("/Models/Admin/{$name}.php"), $modelTemplate);
64
        self::fileMadeSuccess($console, $file, 'Model');
65
    }
66
67
    // Make View
68
    protected static function makeViews($name, $console)
69
    {
70
        $lowername = strtolower($name);
71
        if (!file_exists($path = resource_path('views/admin/' . $lowername))) {
72
            mkdir($path, 0777, true);
73
        }
74
75
        if (!file_exists($path = resource_path('views/admin/layouts/modules/' . $lowername))) {
76
            mkdir($path, 0777, true);
77
        }
78
79
        self::makeIndexView($name, $lowername, $console);
80
        self::makeCreateView($name, $lowername, $console);
81
        self::makeEditView($name, $lowername, $console);
82
        self::makeShowView($name, $lowername, $console);
83
        self::createLayoutBlades($lowername, $console);
84
    }
85
86
    // Make Index View
87
    protected static function makeIndexView($name, $lowername, $console)
88
    {
89
        $modelTemplate = str_replace(
90
            [
91
                '{{modelNameSinglularLowerCase}}',
92
                '{{modelNamePluralLowerCase}}',
93
            ],
94
            [
95
                strtolower($name),
96
                strtolower(Str::plural($name)),
97
            ],
98
            self::getStub('CRUD/IndexView')
99
        );
100
        $file = resource_path("views/admin/{$lowername}/index.blade.php");
101
        file_put_contents(resource_path("views/admin/{$lowername}/index.blade.php"), $modelTemplate);
102
        self::fileMadeSuccess($console, $file, 'Index file');
103
    }
104
105
    // Make Create View
106
    protected static function makeCreateView($name, $lowername, $console)
107
    {
108
        $modelTemplate = str_replace(
109
            [
110
                '{{modelNameSinglularLowerCase}}',
111
            ],
112
            [
113
                strtolower($name),
114
            ],
115
            self::getStub('CRUD/CreateView')
116
        );
117
118
        $file = resource_path("views/admin/{$lowername}/create.blade.php");
119
        file_put_contents(resource_path("views/admin/{$lowername}/create.blade.php"), $modelTemplate);
120
        self::fileMadeSuccess($console, $file, 'Create file');
121
    }
122
123
    // Make Edit View
124
    protected static function makeEditView($name, $lowername, $console)
125
    {
126
        $modelTemplate = str_replace(
127
            [
128
                '{{modelNameSinglularLowerCase}}',
129
            ],
130
            [
131
                strtolower($name),
132
            ],
133
            self::getStub('CRUD/EditView')
134
        );
135
136
        $file = resource_path("views/admin/{$lowername}/edit.blade.php");
137
        file_put_contents(resource_path("views/admin/{$lowername}/edit.blade.php"), $modelTemplate);
138
        self::fileMadeSuccess($console, $file, 'Edit file');
139
    }
140
141
    // Make Show View
142
    protected static function makeShowView($name, $lowername, $console)
143
    {
144
        $modelTemplate = str_replace(
145
            [
146
                '{{modelNameSinglularLowerCase}}',
147
            ],
148
            [
149
                strtolower($name),
150
            ],
151
            self::getStub('CRUD/ShowView')
152
        );
153
154
        $file = resource_path("views/admin/{$lowername}/show.blade.php");
155
        file_put_contents(resource_path("views/admin/{$lowername}/show.blade.php"), $modelTemplate);
156
        self::fileMadeSuccess($console, $file, 'Show file');
157
    }
158
159
    // Make Layout Blades
160
    protected static function createLayoutBlades($lowername, $console)
161
    {
162
        $edit_add_file = resource_path("views/admin/layouts/modules/{$lowername}/edit_add.blade.php");
163
        file_put_contents(resource_path("views/admin/layouts/modules/{$lowername}/edit_add.blade.php"), '');
164
        self::fileMadeSuccess($console, $edit_add_file, 'Edit add extended file');
165
166
        $script_file = resource_path("views/admin/layouts/modules/{$lowername}/scripts.blade.php");
167
        file_put_contents(resource_path("views/admin/layouts/modules/{$lowername}/scripts.blade.php"), '');
168
        self::fileMadeSuccess($console, $script_file, 'Script file');
169
    }
170
171
    // Make Other neccesary CRUD files
172
    protected static function makeOthers($name, $console)
173
    {
174
        Artisan::call('make:migration create_' . strtolower(Str::plural($name)) . '_table --create=' . strtolower(Str::plural($name)));
175
        $console->info('Migration file created named create_' . strtolower(Str::plural($name)) . '_table ... ✅');
176
177
        Artisan::call('make:seeder ' . $name . 'Seeder');
178
        $console->info('Seeder file created ... ✅');
179
180
        Artisan::call('make:repo ' . $name);
181
        $console->info('Repository and Interface created ... ✅');
182
183
        Artisan::call('make:request ' . $name . 'Request');
184
        $console->info('Request file created ... ✅');
185
    }
186
187
    protected static function fileMadeSuccess($console, $file, $type)
188
    {
189
        if (file_exists($file)) {
190
            $console->info($type . ' created successfully ... ✅');
191
        } else {
192
            $console->error('Failed to create ' . $type . ' ...');
193
        }
194
    }
195
}
196