Completed
Pull Request — master (#532)
by Quentin
24:53 queued 19:46
created

ModuleMake::createMigration()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 39
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 3.0175

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 25
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 39
ccs 21
cts 24
cp 0.875
crap 3.0175
rs 9.52
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 55
    public function __construct(Filesystem $files, Composer $composer, Config $config)
67
    {
68 55
        parent::__construct();
69
70 55
        $this->files = $files;
71 55
        $this->composer = $composer;
72 55
        $this->config = $config;
73
74 55
        $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 55
        $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 55
        $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 55
        $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 55
        $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 55
        $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 55
        $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 55
        $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 55
        $this->modelTraits = ['HasBlocks', 'HasTranslation', 'HasSlug', 'HasMedias', 'HasFiles', 'HasRevisions', 'HasPosition'];
85 55
        $this->repositoryTraits = ['HandleBlocks', 'HandleTranslations', 'HandleSlugs', 'HandleMedias', 'HandleFiles', 'HandleRevisions'];
86 55
    }
87
88
    /**
89
     * Executes the console command.
90
     *
91
     * @return mixed
92
     */
93 1
    public function handle()
94
    {
95 1
        $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 1
        $enabledOptions = Collection::make($this->options())->only([
98 1
            'hasBlocks',
99
            'hasTranslation',
100
            'hasSlug',
101
            'hasMedias',
102
            'hasFiles',
103
            'hasPosition',
104
            'hasRevisions',
105
        ])->filter(function ($enabled) {
106 1
            return $enabled;
107 1
        });
108
109 1
        if (count($enabledOptions) > 0) {
110 1
            $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 1
        $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 1
        $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 1
        $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 1
        $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 1
        $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 1
        $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 1
        $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 1
            $this->blockable,
123 1
            $this->translatable,
124 1
            $this->sluggable,
125 1
            $this->mediable,
126 1
            $this->fileable,
127 1
            $this->revisionable,
128 1
            $this->sortable,
129
        ];
130
131 1
        $modelName = Str::studly(Str::singular($moduleName));
132
133 1
        $this->createMigration($moduleName);
134 1
        $this->createModels($modelName, $activeTraits);
135 1
        $this->createRepository($modelName, $activeTraits);
136 1
        $this->createController($moduleName, $modelName);
137 1
        $this->createRequest($modelName);
138 1
        $this->createViews($moduleName);
139
140 1
        $this->info("Add Route::module('{$moduleName}'); to your admin routes file.");
141 1
        $this->info("Setup a new CMS menu item in config/twill-navigation.php:");
142
143 1
        $navTitle = Str::studly($moduleName);
144 1
        $this->info("
145 1
            '{$moduleName}' => [
146 1
                'title' => '{$navTitle}',
147
                'module' => true
148
            ]
149
        ");
150
151 1
        $this->info("Migrate your database.\n");
152
153 1
        $this->info("Enjoy.");
154
155 1
        $this->composer->dumpAutoloads();
156 1
    }
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 1
    private function createMigration($moduleName = 'items')
166
    {
167 1
        $table = Str::snake($moduleName);
168 1
        $tableClassName = Str::studly($table);
169
170 1
        $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 1
        $migrationName = 'create_' . $table . '_tables';
173
174 1
        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 1
            $migrationPath = $this->laravel->databasePath() . '/migrations';
176
177 1
            $fullPath = $this->laravel['migration.creator']->create($migrationName, $migrationPath);
178
179 1
            $stub = str_replace(
180 1
                ['{{table}}', '{{singularTableName}}', '{{tableClassName}}'],
181 1
                [$table, Str::singular($table), $tableClassName],
182 1
                $this->files->get(__DIR__ . '/stubs/migration.stub')
183
            );
184
185 1
            if ($this->translatable) {
186 1
                $stub = preg_replace('/{{!hasTranslation}}[\s\S]+?{{\/!hasTranslation}}/', '', $stub);
187
            } else {
188
                $stub = str_replace([
189
                    '{{!hasTranslation}}',
190
                    '{{/!hasTranslation}}',
191
                ], '', $stub);
192
            }
193
194 1
            $stub = $this->renderStubForOption($stub, 'hasTranslation', $this->translatable);
195 1
            $stub = $this->renderStubForOption($stub, 'hasSlug', $this->sluggable);
196 1
            $stub = $this->renderStubForOption($stub, 'hasRevisions', $this->revisionable);
197 1
            $stub = $this->renderStubForOption($stub, 'hasPosition', $this->sortable);
198
199 1
            $stub = preg_replace('/\}\);[\s\S]+?Schema::create/', "});\n\n        Schema::create", $stub);
200
201 1
            twill_put_stub($fullPath, $stub);
202
203 1
            $this->info("Migration created successfully! Add some fields!");
204
        }
205 1
    }
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 1
    private function createModels($modelName = 'Item', $activeTraits = [])
215
    {
216 1
        $modelClassName = $modelName;
217
218 1
        make_twill_directory('Models');
219
220 1
        if ($this->translatable) {
221 1
            make_twill_directory('Models/Translations');
222
223 1
            $modelTranslationClassName = $modelName . 'Translation';
224
225 1
            $stub = str_replace(
226 1
                ['{{modelTranslationClassName}}', '{{modelClassName}}'],
227 1
                [$modelTranslationClassName, $modelClassName],
228 1
                $this->files->get(__DIR__ . '/stubs/model_translation.stub')
229
            );
230
231 1
            twill_put_stub(twill_path('Models/Translations/' . $modelTranslationClassName . '.php'), $stub);
232
        }
233
234 1
        if ($this->sluggable) {
235 1
            make_twill_directory('Models/Slugs');
236
237 1
            $modelSlugClassName = $modelName . 'Slug';
238
239 1
            $stub = str_replace(['{{modelSlugClassName}}', '{{modelName}}'], [$modelSlugClassName, Str::snake($modelName)], $this->files->get(__DIR__ . '/stubs/model_slug.stub'));
240
241 1
            twill_put_stub(twill_path('Models/Slugs/' . $modelSlugClassName . '.php'), $stub);
242
        }
243
244 1
        if ($this->revisionable) {
245 1
            make_twill_directory('Models/Revisions');
246
247 1
            $modelRevisionClassName = $modelName . 'Revision';
248
249 1
            $stub = str_replace(['{{modelRevisionClassName}}', '{{modelName}}'], [$modelRevisionClassName, Str::snake($modelName)], $this->files->get(__DIR__ . '/stubs/model_revision.stub'));
250
251 1
            twill_put_stub(twill_path('Models/Revisions/' . $modelRevisionClassName . '.php'), $stub);
252
        }
253
254 1
        $activeModelTraits = [];
255
256 1
        foreach ($activeTraits as $index => $traitIsActive) {
257 1
            if ($traitIsActive) {
258 1
                !isset($this->modelTraits[$index]) ?: $activeModelTraits[] = $this->modelTraits[$index];
259
            }
260
        }
261
262 1
        $activeModelTraitsString = empty($activeModelTraits) ? '' : 'use ' . rtrim(implode(', ', $activeModelTraits), ', ') . ';';
263
264 1
        $activeModelTraitsImports = empty($activeModelTraits) ? '' : "use A17\Twill\Models\Behaviors\\" . implode(";\nuse A17\Twill\Models\Behaviors\\", $activeModelTraits) . ";";
265
266 1
        $activeModelImplements = $this->sortable ? 'implements Sortable' : '';
267
268 1
        if ($this->sortable) {
269 1
            $activeModelTraitsImports .= "\nuse A17\Twill\Models\Behaviors\Sortable;";
270
        }
271
272 1
        $stub = str_replace([
273 1
            '{{modelClassName}}',
274
            '{{modelTraits}}',
275
            '{{modelImports}}',
276
            '{{modelImplements}}',
277
        ], [
278 1
            $modelClassName,
279 1
            $activeModelTraitsString,
280 1
            $activeModelTraitsImports,
281 1
            $activeModelImplements,
282 1
        ], $this->files->get(__DIR__ . '/stubs/model.stub'));
283
284 1
        $stub = $this->renderStubForOption($stub, 'hasTranslation', $this->translatable);
285 1
        $stub = $this->renderStubForOption($stub, 'hasSlug', $this->sluggable);
286 1
        $stub = $this->renderStubForOption($stub, 'hasMedias', $this->mediable);
287 1
        $stub = $this->renderStubForOption($stub, 'hasPosition', $this->sortable);
288
289 1
        twill_put_stub(twill_path('Models/' . $modelClassName . '.php'), $stub);
290
291 1
        $this->info("Models created successfully! Fill your fillables!");
292 1
    }
293
294 1
    private function renderStubForOption($stub, $option, $enabled)
295
    {
296 1
        if ($enabled) {
297 1
            $stub = str_replace([
298 1
                '{{' . $option . '}}',
299 1
                '{{/' . $option . '}}',
300 1
            ], '', $stub);
301
        } else {
302
            $stub = preg_replace('/{{' . $option . '}}[\s\S]+?{{\/' . $option . '}}/', '', $stub);
303
        }
304
305 1
        return $stub;
306
    }
307
308
    /**
309
     * Creates new repository class file for the given model name.
310
     *
311
     * @param string $modelName
312
     * @param array $activeTraits
313
     * @return void
314
     * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
315
     */
316 1
    private function createRepository($modelName = 'Item', $activeTraits = [])
317
    {
318 1
        make_twill_directory('Repositories');
319
320 1
        $repositoryClassName = $modelName . 'Repository';
321
322 1
        $activeRepositoryTraits = [];
323
324 1
        foreach ($activeTraits as $index => $traitIsActive) {
325 1
            if ($traitIsActive) {
326 1
                !isset($this->repositoryTraits[$index]) ?: $activeRepositoryTraits[] = $this->repositoryTraits[$index];
327
            }
328
        }
329
330 1
        $activeRepositoryTraitsString = empty($activeRepositoryTraits) ? '' : 'use ' . (empty($activeRepositoryTraits) ? "" : rtrim(implode(', ', $activeRepositoryTraits), ', ') . ';');
331
332 1
        $activeRepositoryTraitsImports = empty($activeRepositoryTraits) ? '' : "use A17\Twill\Repositories\Behaviors\\" . implode(";\nuse A17\Twill\Repositories\Behaviors\\", $activeRepositoryTraits) . ";";
333
334 1
        $stub = str_replace(['{{repositoryClassName}}', '{{modelName}}', '{{repositoryTraits}}', '{{repositoryImports}}'], [$repositoryClassName, $modelName, $activeRepositoryTraitsString, $activeRepositoryTraitsImports], $this->files->get(__DIR__ . '/stubs/repository.stub'));
335
336 1
        twill_put_stub(twill_path('Repositories/' . $repositoryClassName . '.php'), $stub);
337
338 1
        $this->info("Repository created successfully! Control all the things!");
339 1
    }
340
341
    /**
342
     * Create a new controller class file for the given module name and model name.
343
     *
344
     * @param string $moduleName
345
     * @param string $modelName
346
     * @return void
347
     * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
348
     */
349 1
    private function createController($moduleName = 'items', $modelName = 'Item')
350
    {
351 1
        make_twill_directory('Http/Controllers/Admin');
352
353 1
        $controllerClassName = $modelName . 'Controller';
354
355 1
        $stub = str_replace(
356 1
            ['{{moduleName}}', '{{controllerClassName}}'],
357 1
            [$moduleName, $controllerClassName],
358 1
            $this->files->get(__DIR__ . '/stubs/controller.stub')
359
        );
360
361 1
        twill_put_stub(twill_path('Http/Controllers/Admin/' . $controllerClassName . '.php'), $stub);
362
363 1
        $this->info("Controller created successfully! Define your index/browser/form endpoints options!");
364 1
    }
365
366
    /**
367
     * Creates a new request class file for the given model name.
368
     *
369
     * @param string $modelName
370
     * @return void
371
     * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
372
     */
373 1
    private function createRequest($modelName = 'Item')
374
    {
375 1
        make_twill_directory('Http/Requests/Admin');
376
377 1
        $requestClassName = $modelName . 'Request';
378
379 1
        $stub = str_replace('{{requestClassName}}', $requestClassName, $this->files->get(__DIR__ . '/stubs/request.stub'));
380
381 1
        twill_put_stub(twill_path('Http/Requests/Admin/' . $requestClassName . '.php'), $stub);
382
383 1
        $this->info("Form request created successfully! Add some validation rules!");
384 1
    }
385
386
    /**
387
     * Creates appropriate module Blade view files.
388
     *
389
     * @param string $moduleName
390
     * @return void
391
     * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
392
     */
393 1
    private function createViews($moduleName = 'items')
394
    {
395 1
        $viewsPath = $this->config->get('view.paths')[0] . '/admin/' . $moduleName;
396
397 1
        make_twill_directory($viewsPath);
398
399 1
        $formView = $this->translatable ? 'form_translatable' : 'form';
400
401 1
        twill_put_stub($viewsPath . '/form.blade.php', $this->files->get(__DIR__ . '/stubs/' . $formView . '.blade.stub'));
402
403 1
        $this->info("Form view created successfully! Include your form fields using @formField directives!");
404 1
    }
405
406 1
    private function checkOption($option)
407
    {
408 1
        if ($this->option($option) || $this->option('all')) {
409 1
            return true;
410
        }
411
412
        $questions = [
413
            'hasBlocks' => 'Do you need to use the block editor on this module?',
414
            'hasTranslation' => 'Do you need to translate content on this module?',
415
            'hasSlug' => 'Do you need to generate slugs on this module?',
416
            'hasMedias' => 'Do you need to attach images on this module?',
417
            'hasFiles' => 'Do you need to attach files on this module?',
418
            'hasPosition' => 'Do you need to manage the position of records on this module?',
419
            'hasRevisions' => 'Do you need to enable revisions on this module?',
420
        ];
421
422
        return 'yes' === $this->choice($questions[$option], ['no', 'yes'], $this->defaultsAnswserToNo ? 0 : 1);
423
    }
424
}
425