CRUDGeneratorService   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 225
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 118
c 0
b 0
f 0
dl 0
loc 225
rs 10
wmc 17

12 Methods

Rating   Name   Duplication   Size   Complexity  
A makeOthers() 0 13 1
A makeCRUD() 0 7 1
A makeModel() 0 21 2
A makeEditView() 0 15 1
A makeController() 0 21 2
A makeViews() 0 16 3
A makeIndexView() 0 16 1
A createLayoutBlades() 0 9 1
A makeCreateView() 0 15 1
A makeShowView() 0 15 1
A fileMadeSuccess() 0 6 2
A addFileContent() 0 37 1
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
        self::addFileContent($name, $console);
18
    }
19
20
    // Make Controller
21
    protected static function makeController($name, $console)
22
    {
23
        if (! file_exists($path = app_path('/Http/Controllers/Admin'))) {
24
            mkdir($path, 0777, true);
25
        }
26
        $controllerTemplate = str_replace(
27
            [
28
                '{{modelName}}',
29
                '{{modelNamePluralLowerCase}}',
30
                '{{modelNameSingularLowerCase}}',
31
            ],
32
            [
33
                $name,
34
                strtolower(Str::plural($name)),
35
                strtolower($name),
36
            ],
37
            self::getStub('Controller')
38
        );
39
        $file = app_path("/Http/Controllers/Admin/{$name}Controller.php");
40
        file_put_contents(app_path("/Http/Controllers/Admin/{$name}Controller.php"), $controllerTemplate);
41
        self::fileMadeSuccess($console, $file, 'Controller');
42
    }
43
44
    // Make Model
45
    protected static function makeModel($name, $console)
46
    {
47
        if (! file_exists($path = app_path('/Models/Admin'))) {
48
            mkdir($path, 0777, true);
49
        }
50
        $modelTemplate = str_replace(
51
            [
52
                '{{modelName}}',
53
                '{{modelNamePluralLowerCase}}',
54
                '{{modelNameSingularLowerCase}}',
55
            ],
56
            [
57
                $name,
58
                strtolower(Str::plural($name)),
59
                strtolower($name),
60
            ],
61
            self::getStub('Model')
62
        );
63
        $file = app_path("/Models/Admin/{$name}.php");
64
        file_put_contents(app_path("/Models/Admin/{$name}.php"), $modelTemplate);
65
        self::fileMadeSuccess($console, $file, 'Model');
66
    }
67
68
    // Make View
69
    protected static function makeViews($name, $console)
70
    {
71
        $lowername = strtolower($name);
72
        if (! file_exists($path = resource_path('views/admin/'.$lowername))) {
73
            mkdir($path, 0777, true);
74
        }
75
76
        if (! file_exists($path = resource_path('views/admin/layouts/modules/'.$lowername))) {
77
            mkdir($path, 0777, true);
78
        }
79
80
        self::makeIndexView($name, $lowername, $console);
81
        self::makeCreateView($name, $lowername, $console);
82
        self::makeEditView($name, $lowername, $console);
83
        self::makeShowView($name, $lowername, $console);
84
        self::createLayoutBlades($lowername, $console);
85
    }
86
87
    // Make Index View
88
    protected static function makeIndexView($name, $lowername, $console)
89
    {
90
        $modelTemplate = str_replace(
91
            [
92
                '{{modelNameSinglularLowerCase}}',
93
                '{{modelNamePluralLowerCase}}',
94
            ],
95
            [
96
                strtolower($name),
97
                strtolower(Str::plural($name)),
98
            ],
99
            self::getStub('CRUD/IndexView')
100
        );
101
        $file = resource_path("views/admin/{$lowername}/index.blade.php");
102
        file_put_contents(resource_path("views/admin/{$lowername}/index.blade.php"), $modelTemplate);
103
        self::fileMadeSuccess($console, $file, 'Index file');
104
    }
105
106
    // Make Create View
107
    protected static function makeCreateView($name, $lowername, $console)
108
    {
109
        $modelTemplate = str_replace(
110
            [
111
                '{{modelNameSinglularLowerCase}}',
112
            ],
113
            [
114
                strtolower($name),
115
            ],
116
            self::getStub('CRUD/CreateView')
117
        );
118
119
        $file = resource_path("views/admin/{$lowername}/create.blade.php");
120
        file_put_contents(resource_path("views/admin/{$lowername}/create.blade.php"), $modelTemplate);
121
        self::fileMadeSuccess($console, $file, 'Create file');
122
    }
123
124
    // Make Edit View
125
    protected static function makeEditView($name, $lowername, $console)
126
    {
127
        $modelTemplate = str_replace(
128
            [
129
                '{{modelNameSinglularLowerCase}}',
130
            ],
131
            [
132
                strtolower($name),
133
            ],
134
            self::getStub('CRUD/EditView')
135
        );
136
137
        $file = resource_path("views/admin/{$lowername}/edit.blade.php");
138
        file_put_contents(resource_path("views/admin/{$lowername}/edit.blade.php"), $modelTemplate);
139
        self::fileMadeSuccess($console, $file, 'Edit file');
140
    }
141
142
    // Make Show View
143
    protected static function makeShowView($name, $lowername, $console)
144
    {
145
        $modelTemplate = str_replace(
146
            [
147
                '{{modelNameSinglularLowerCase}}',
148
            ],
149
            [
150
                strtolower($name),
151
            ],
152
            self::getStub('CRUD/ShowView')
153
        );
154
155
        $file = resource_path("views/admin/{$lowername}/show.blade.php");
156
        file_put_contents(resource_path("views/admin/{$lowername}/show.blade.php"), $modelTemplate);
157
        self::fileMadeSuccess($console, $file, 'Show file');
158
    }
159
160
    // Make Layout Blades
161
    protected static function createLayoutBlades($lowername, $console)
162
    {
163
        $form_file = resource_path("views/admin/layouts/modules/{$lowername}/form.blade.php");
164
        file_put_contents(resource_path("views/admin/layouts/modules/{$lowername}/form.blade.php"), '');
165
        self::fileMadeSuccess($console, $form_file, 'Edit add extended file');
166
167
        $script_file = resource_path("views/admin/layouts/modules/{$lowername}/scripts.blade.php");
168
        file_put_contents(resource_path("views/admin/layouts/modules/{$lowername}/scripts.blade.php"), '');
169
        self::fileMadeSuccess($console, $script_file, 'Script file');
170
    }
171
172
    // Make Other neccesary CRUD files
173
    protected static function makeOthers($name, $console)
174
    {
175
        Artisan::call('make:migration create_'.strtolower(Str::plural($name)).'_table --create='.strtolower(Str::plural($name)));
176
        $console->info('Migration file created named create_'.strtolower(Str::plural($name)).'_table ... ✅');
177
178
        Artisan::call('make:seeder '.$name.'Seeder');
179
        $console->info('Seeder file created ... ✅');
180
181
        Artisan::call('make:repo '.$name);
182
        $console->info('Repository and Interface created ... ✅');
183
184
        Artisan::call('make:request '.$name.'Request');
185
        $console->info('Request file created ... ✅');
186
    }
187
188
    // Make Other Necessary CRUD Files
189
    protected static function addFileContent($name, $console)
190
    {
191
        // Adding Route
192
        $lowercased_name = strtolower($name);
193
        $route = "Route::resource('admin/{$lowercased_name}',\App\Http\Controllers\Admin\\{$name}Controller::class);";
194
        file_put_contents('routes/web.php', "\n", FILE_APPEND | LOCK_EX);
195
        file_put_contents('routes/web.php', $route, FILE_APPEND | LOCK_EX);
196
197
        $console->info('Route  added to web.php ... ✅');
198
199
        // Adding Route Interface Binding
200
        $repository_interface_binding = '$this->app->bind(\App\Contracts\\'.$name.'RepositoryInterface::class, \App\Repositories\\'.$name.'Repository::class);';
201
        $provider_path = app_path('Providers/AdminServiceProvider.php');
202
        putContentToClassFunction($provider_path, 'protected function repos', $repository_interface_binding);
203
204
        // Adding Module To Menu
205
        $menu_content = "],[\n".
206
            "'type' => 'menu',\n".
207
            "'name' => '$name',\n".
208
            "'icon' => 'fa fa-wrench',\n".
209
            "'is_active' => request()->routeIs('$lowercased_name*') ? 'active' : '',\n".
210
            "'conditions' => [\n".
211
            "[\n".
212
            "'type' => 'or',\n".
213
            "'condition' => auth()->user()->can('view-any', \App\Models\Admin\\".$name."::class),\n".
214
            "],\n".
215
            "[\n".
216
            "'type' => 'or',\n".
217
            "'condition' => auth()->user()->can('create', \App\Models\Admin\\".$name."::class),\n".
218
            "],\n".
219
            "],\n";
220
        $menu_content = $menu_content.'"children" => $this->indexCreateChildren("'.$lowercased_name.'", \App\Models\Admin\\'.$name.'::class),';
221
        $menu_content = "\n".$menu_content."\n";
222
        $menu_path = app_path('Services/MyMenu.php');
223
        putContentToClassFunction($menu_path, 'return [', $menu_content, ']');
224
225
        $console->info('Menu added to Menu.php ... ✅');
226
    }
227
228
    protected static function fileMadeSuccess($console, $file, $type)
229
    {
230
        if (file_exists($file)) {
231
            $console->info($type.' created successfully ... ✅');
232
        } else {
233
            $console->error('Failed to create '.$type.' ...');
234
        }
235
    }
236
}
237