Passed
Push — 1.2 ( 2544d0...f6cea4 )
by Quentin
07:12
created

ModuleMake::checkOption()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 11
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 17
rs 9.9
1
<?php
2
3
namespace A17\Twill\Commands;
4
5
use Illuminate\Config\Repository as Config;
6
use Illuminate\Console\Command;
7
use Illuminate\Filesystem\Filesystem;
8
use Illuminate\Support\Collection;
9
use Illuminate\Support\Composer;
10
use Illuminate\Support\Str;
11
12
class ModuleMake extends Command
13
{
14
    /**
15
     * The name and signature of the console command.
16
     *
17
     * @var string
18
     */
19
    protected $signature = 'twill:module {moduleName}
20
        {--B|hasBlocks}
21
        {--T|hasTranslation}
22
        {--S|hasSlug}
23
        {--M|hasMedias}
24
        {--F|hasFiles}
25
        {--P|hasPosition}
26
        {--R|hasRevisions}
27
        {--all}';
28
29
    /**
30
     * The console command description.
31
     *
32
     * @var string
33
     */
34
    protected $description = 'Create a new Twill Module';
35
36
    /**
37
     * @var Filesystem
38
     */
39
    protected $files;
40
41
    /**
42
     * @var Composer
43
     */
44
    protected $composer;
45
46
    /**
47
     * @var string[]
48
     */
49
    protected $modelTraits;
50
51
    /**
52
     * @var string[]
53
     */
54
    protected $repositoryTraits;
55
56
    /**
57
     * @var Config
58
     */
59
    protected $config;
60
61
    /**
62
     * @param Filesystem $files
63
     * @param Composer $composer
64
     * @param Config $config
65
     */
66
    public function __construct(Filesystem $files, Composer $composer, Config $config)
67
    {
68
        parent::__construct();
69
70
        $this->files = $files;
71
        $this->composer = $composer;
72
        $this->config = $config;
73
74
        $this->blockable = false;
0 ignored issues
show
Bug Best Practice introduced by
The property blockable does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
75
        $this->translatable = false;
0 ignored issues
show
Bug Best Practice introduced by
The property translatable does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
76
        $this->sluggable = false;
0 ignored issues
show
Bug Best Practice introduced by
The property sluggable does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
77
        $this->mediable = false;
0 ignored issues
show
Bug Best Practice introduced by
The property mediable does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
78
        $this->fileable = false;
0 ignored issues
show
Bug Best Practice introduced by
The property fileable does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
79
        $this->sortable = false;
0 ignored issues
show
Bug Best Practice introduced by
The property sortable does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
80
        $this->revisionable = false;
0 ignored issues
show
Bug Best Practice introduced by
The property revisionable does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
81
82
        $this->defaultsAnswserToNo = false;
0 ignored issues
show
Bug Best Practice introduced by
The property defaultsAnswserToNo does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
83
84
        $this->modelTraits = ['HasBlocks', 'HasTranslation', 'HasSlug', 'HasMedias', 'HasFiles', 'HasRevisions', 'HasPosition'];
85
        $this->repositoryTraits = ['HandleBlocks', 'HandleTranslations', 'HandleSlugs', 'HandleMedias', 'HandleFiles', 'HandleRevisions'];
86
    }
87
88
    /**
89
     * Executes the console command.
90
     *
91
     * @return mixed
92
     */
93
    public function handle()
94
    {
95
        $moduleName = Str::plural(lcfirst($this->argument('moduleName')));
0 ignored issues
show
Bug introduced by
It seems like $this->argument('moduleName') can also be of type string[]; however, parameter $str of lcfirst() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

95
        $moduleName = Str::plural(lcfirst(/** @scrutinizer ignore-type */ $this->argument('moduleName')));
Loading history...
96
97
        $enabledOptions = Collection::make($this->options())->only([
98
            'hasBlocks',
99
            'hasTranslation',
100
            'hasSlug',
101
            'hasMedias',
102
            'hasFiles',
103
            'hasPosition',
104
            'hasRevisions',
105
        ])->filter(function ($enabled) {
106
            return $enabled;
107
        });
108
109
        if (count($enabledOptions) > 0) {
110
            $this->defaultsAnswserToNo = true;
0 ignored issues
show
Bug Best Practice introduced by
The property defaultsAnswserToNo does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
111
        }
112
113
        $this->blockable = $this->checkOption('hasBlocks');
0 ignored issues
show
Bug Best Practice introduced by
The property blockable does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
114
        $this->translatable = $this->checkOption('hasTranslation');
0 ignored issues
show
Bug Best Practice introduced by
The property translatable does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
115
        $this->sluggable = $this->checkOption('hasSlug');
0 ignored issues
show
Bug Best Practice introduced by
The property sluggable does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
116
        $this->mediable = $this->checkOption('hasMedias');
0 ignored issues
show
Bug Best Practice introduced by
The property mediable does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
117
        $this->fileable = $this->checkOption('hasFiles');
0 ignored issues
show
Bug Best Practice introduced by
The property fileable does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
118
        $this->sortable = $this->checkOption('hasPosition');
0 ignored issues
show
Bug Best Practice introduced by
The property sortable does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
119
        $this->revisionable = $this->checkOption('hasRevisions');
0 ignored issues
show
Bug Best Practice introduced by
The property revisionable does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
120
121
        $activeTraits = [
122
            $this->blockable,
123
            $this->translatable,
124
            $this->sluggable,
125
            $this->mediable,
126
            $this->fileable,
127
            $this->revisionable,
128
            $this->sortable,
129
        ];
130
131
        $modelName = Str::studly(Str::singular($moduleName));
132
133
        $this->createMigration($moduleName);
134
        $this->createModels($modelName, $activeTraits);
135
        $this->createRepository($modelName, $activeTraits);
136
        $this->createController($moduleName, $modelName);
137
        $this->createRequest($modelName);
138
        $this->createViews($moduleName);
139
140
        $this->info("Add Route::module('{$moduleName}'); to your admin routes file.");
141
        $this->info("Setup a new CMS menu item in config/twill-navigation.php:");
142
143
        $navTitle = Str::studly($moduleName);
144
        $this->info("
145
            '{$moduleName}' => [
146
                'title' => '{$navTitle}',
147
                'module' => true
148
            ]
149
        ");
150
151
        $this->info("Migrate your database.\n");
152
153
        $this->info("Enjoy.");
154
155
        $this->composer->dumpAutoloads();
156
    }
157
158
    /**
159
     * Creates a new module database migration file.
160
     *
161
     * @param string $moduleName
162
     * @return void
163
     * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
164
     */
165
    private function createMigration($moduleName = 'items')
166
    {
167
        $table = Str::snake($moduleName);
168
        $tableClassName = Str::studly($table);
169
170
        $className = "Create{$tableClassName}Tables";
0 ignored issues
show
Unused Code introduced by
The assignment to $className is dead and can be removed.
Loading history...
171
172
        $migrationName = 'create_' . $table . '_tables';
173
174
        if (!count(glob(database_path('migrations/*' . $migrationName . '.php')))) {
0 ignored issues
show
Bug introduced by
It seems like glob(database_path('migr...igrationName . '.php')) can also be of type false; however, parameter $var of count() does only seem to accept Countable|array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

174
        if (!count(/** @scrutinizer ignore-type */ glob(database_path('migrations/*' . $migrationName . '.php')))) {
Loading history...
175
            $migrationPath = $this->laravel->databasePath() . '/migrations';
176
177
            $fullPath = $this->laravel['migration.creator']->create($migrationName, $migrationPath);
178
179
            $stub = str_replace(
180
                ['{{table}}', '{{singularTableName}}', '{{tableClassName}}'],
181
                [$table, Str::singular($table), $tableClassName],
182
                $this->files->get(__DIR__ . '/stubs/migration.stub')
183
            );
184
185
            if ($this->translatable) {
186
                $stub = preg_replace('/{{!hasTranslation}}[\s\S]+?{{\/!hasTranslation}}/', '', $stub);
187
            } else {
188
                $stub = str_replace([
189
                    '{{!hasTranslation}}',
190
                    '{{/!hasTranslation}}',
191
                ], '', $stub);
192
            }
193
194
            $stub = $this->renderStubForOption($stub, 'hasTranslation', $this->translatable);
195
            $stub = $this->renderStubForOption($stub, 'hasSlug', $this->sluggable);
196
            $stub = $this->renderStubForOption($stub, 'hasRevisions', $this->revisionable);
197
            $stub = $this->renderStubForOption($stub, 'hasPosition', $this->sortable);
198
199
            $stub = preg_replace('/\}\);[\s\S]+?Schema::create/', "});\n\n        Schema::create", $stub);
200
201
            $this->files->put($fullPath, $stub);
202
203
            $this->info("Migration created successfully! Add some fields!");
204
        }
205
    }
206
207
    /**
208
     * Creates new model class files for the given model name and traits.
209
     *
210
     * @param string $modelName
211
     * @param array $activeTraits
212
     * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
213
     */
214
    private function createModels($modelName = 'Item', $activeTraits = [])
215
    {
216
        if (!$this->files->isDirectory(twill_path('Models'))) {
217
            $this->files->makeDirectory(twill_path('Models'));
218
        }
219
220
        if ($this->translatable) {
221
            if (!$this->files->isDirectory(twill_path('Models/Translations'))) {
222
                $this->files->makeDirectory(twill_path('Models/Translations'));
223
            }
224
225
            $modelTranslationClassName = $modelName . 'Translation';
226
227
            $stub = str_replace('{{modelTranslationClassName}}', $modelTranslationClassName, $this->files->get(__DIR__ . '/stubs/model_translation.stub'));
228
229
            $this->files->put(twill_path('Models/Translations/' . $modelTranslationClassName . '.php'), $stub);
230
        }
231
232
        if ($this->sluggable) {
233
            if (!$this->files->isDirectory(twill_path('Models/Slugs'))) {
234
                $this->files->makeDirectory(twill_path('Models/Slugs'));
235
            }
236
237
            $modelSlugClassName = $modelName . 'Slug';
238
239
            $stub = str_replace(['{{modelSlugClassName}}', '{{modelName}}'], [$modelSlugClassName, Str::snake($modelName)], $this->files->get(__DIR__ . '/stubs/model_slug.stub'));
240
241
            $this->files->put(twill_path('Models/Slugs/' . $modelSlugClassName . '.php'), $stub);
242
        }
243
244
        if ($this->revisionable) {
245
            if (!$this->files->isDirectory(twill_path('Models/Revisions'))) {
246
                $this->files->makeDirectory(twill_path('Models/Revisions'));
247
            }
248
249
            $modelRevisionClassName = $modelName . 'Revision';
250
251
            $stub = str_replace(['{{modelRevisionClassName}}', '{{modelName}}'], [$modelRevisionClassName, Str::snake($modelName)], $this->files->get(__DIR__ . '/stubs/model_revision.stub'));
252
253
            $this->files->put(twill_path('Models/Revisions/' . $modelRevisionClassName . '.php'), $stub);
254
        }
255
256
        $modelClassName = $modelName;
257
258
        $activeModelTraits = [];
259
260
        foreach ($activeTraits as $index => $traitIsActive) {
261
            if ($traitIsActive) {
262
                !isset($this->modelTraits[$index]) ?: $activeModelTraits[] = $this->modelTraits[$index];
263
            }
264
        }
265
266
        $activeModelTraitsString = empty($activeModelTraits) ? '' : 'use ' . rtrim(implode(', ', $activeModelTraits), ', ') . ';';
267
268
        $activeModelTraitsImports = empty($activeModelTraits) ? '' : "use A17\Twill\Models\Behaviors\\" . implode(";\nuse A17\Twill\Models\Behaviors\\", $activeModelTraits) . ";";
269
270
        $activeModelImplements = $this->sortable ? 'implements Sortable' : '';
271
272
        if ($this->sortable) {
273
            $activeModelTraitsImports .= "\nuse A17\Twill\Models\Behaviors\Sortable;";
274
        }
275
276
        $stub = str_replace([
277
            '{{modelClassName}}',
278
            '{{modelTraits}}',
279
            '{{modelImports}}',
280
            '{{modelImplements}}',
281
        ], [
282
            $modelClassName,
283
            $activeModelTraitsString,
284
            $activeModelTraitsImports,
285
            $activeModelImplements,
286
        ], $this->files->get(__DIR__ . '/stubs/model.stub'));
287
288
        $stub = $this->renderStubForOption($stub, 'hasTranslation', $this->translatable);
289
        $stub = $this->renderStubForOption($stub, 'hasSlug', $this->sluggable);
290
        $stub = $this->renderStubForOption($stub, 'hasMedias', $this->mediable);
291
        $stub = $this->renderStubForOption($stub, 'hasPosition', $this->sortable);
292
293
        $this->files->put(twill_path('Models/' . $modelClassName . '.php'), $stub);
294
295
        $this->info("Models created successfully! Fill your fillables!");
296
    }
297
298
    private function renderStubForOption($stub, $option, $enabled)
299
    {
300
        if ($enabled) {
301
            $stub = str_replace([
302
                '{{' . $option . '}}',
303
                '{{/' . $option . '}}',
304
            ], '', $stub);
305
        } else {
306
            $stub = preg_replace('/{{' . $option . '}}[\s\S]+?{{\/' . $option . '}}/', '', $stub);
307
        }
308
309
        return $stub;
310
    }
311
312
    /**
313
     * Creates new repository class file for the given model name.
314
     *
315
     * @param string $modelName
316
     * @param array $activeTraits
317
     * @return void
318
     * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
319
     */
320
    private function createRepository($modelName = 'Item', $activeTraits = [])
321
    {
322
        if (!$this->files->isDirectory(twill_path('Repositories'))) {
323
            $this->files->makeDirectory(twill_path('Repositories'));
324
        }
325
326
        $repositoryClassName = $modelName . 'Repository';
327
328
        $activeRepositoryTraits = [];
329
330
        foreach ($activeTraits as $index => $traitIsActive) {
331
            if ($traitIsActive) {
332
                !isset($this->repositoryTraits[$index]) ?: $activeRepositoryTraits[] = $this->repositoryTraits[$index];
333
            }
334
        }
335
336
        $activeRepositoryTraitsString = empty($activeRepositoryTraits) ? '' : 'use ' . (empty($activeRepositoryTraits) ? "" : rtrim(implode(', ', $activeRepositoryTraits), ', ') . ';');
337
338
        $activeRepositoryTraitsImports = empty($activeRepositoryTraits) ? '' : "use A17\Twill\Repositories\Behaviors\\" . implode(";\nuse A17\Twill\Repositories\Behaviors\\", $activeRepositoryTraits) . ";";
339
340
        $stub = str_replace(['{{repositoryClassName}}', '{{modelName}}', '{{repositoryTraits}}', '{{repositoryImports}}'], [$repositoryClassName, $modelName, $activeRepositoryTraitsString, $activeRepositoryTraitsImports], $this->files->get(__DIR__ . '/stubs/repository.stub'));
341
342
        $this->files->put(twill_path('Repositories/' . $repositoryClassName . '.php'), $stub);
343
344
        $this->info("Repository created successfully! Control all the things!");
345
    }
346
347
    /**
348
     * Create a new controller class file for the given module name and model name.
349
     *
350
     * @param string $moduleName
351
     * @param string $modelName
352
     * @return void
353
     * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
354
     */
355
    private function createController($moduleName = 'items', $modelName = 'Item')
356
    {
357
        if (!$this->files->isDirectory(twill_path('Http/Controllers/Admin'))) {
358
            $this->files->makeDirectory(twill_path('Http/Controllers/Admin'));
359
        }
360
361
        $controllerClassName = $modelName . 'Controller';
362
363
        $stub = str_replace(
364
            ['{{moduleName}}', '{{controllerClassName}}'],
365
            [$moduleName, $controllerClassName],
366
            $this->files->get(__DIR__ . '/stubs/controller.stub')
367
        );
368
369
        $this->files->put(twill_path('Http/Controllers/Admin/' . $controllerClassName . '.php'), $stub);
370
371
        $this->info("Controller created successfully! Define your index/browser/form endpoints options!");
372
    }
373
374
    /**
375
     * Creates a new request class file for the given model name.
376
     *
377
     * @param string $modelName
378
     * @return void
379
     * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
380
     */
381
    private function createRequest($modelName = 'Item')
382
    {
383
        if (!$this->files->isDirectory(twill_path('Http/Requests/Admin'))) {
384
            $this->files->makeDirectory(twill_path('Http/Requests/Admin'), 0755, true);
385
        }
386
387
        $requestClassName = $modelName . 'Request';
388
389
        $stub = str_replace('{{requestClassName}}', $requestClassName, $this->files->get(__DIR__ . '/stubs/request.stub'));
390
391
        $this->files->put(twill_path('Http/Requests/Admin/' . $requestClassName . '.php'), $stub);
392
393
        $this->info("Form request created successfully! Add some validation rules!");
394
    }
395
396
    /**
397
     * Creates appropriate module Blade view files.
398
     *
399
     * @param string $moduleName
400
     * @return void
401
     * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
402
     */
403
    private function createViews($moduleName = 'items')
404
    {
405
        $viewsPath = $this->config->get('view.paths')[0] . '/admin/' . $moduleName;
406
407
        if (!$this->files->isDirectory($viewsPath)) {
408
            $this->files->makeDirectory($viewsPath, 0755, true);
409
        }
410
411
        $formView = $this->translatable ? 'form_translatable' : 'form';
412
413
        $this->files->put($viewsPath . '/form.blade.php', $this->files->get(__DIR__ . '/stubs/' . $formView . '.blade.stub'));
414
415
        $this->info("Form view created successfully! Include your form fields using @formField directives!");
416
    }
417
418
    private function checkOption($option)
419
    {
420
        if ($this->option($option) || $this->option('all')) {
421
            return true;
422
        }
423
424
        $questions = [
425
            'hasBlocks' => 'Do you need to use the block editor on this module?',
426
            'hasTranslation' => 'Do you need to translate content on this module?',
427
            'hasSlug' => 'Do you need to generate slugs on this module?',
428
            'hasMedias' => 'Do you need to attach images on this module?',
429
            'hasFiles' => 'Do you need to attach files on this module?',
430
            'hasPosition' => 'Do you need to manage the position of records on this module?',
431
            'hasRevisions' => 'Do you need to enable revisions on this module?',
432
        ];
433
434
        return 'yes' === $this->choice($questions[$option], ['no', 'yes'], $this->defaultsAnswserToNo ? 0 : 1);
435
    }
436
}
437