1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace A17\Twill\Commands; |
4
|
|
|
|
5
|
|
|
use Illuminate\Config\Repository as Config; |
6
|
|
|
use Illuminate\Filesystem\Filesystem; |
7
|
|
|
use Illuminate\Support\Collection; |
8
|
|
|
use Illuminate\Support\Composer; |
9
|
|
|
use Illuminate\Support\Facades\File; |
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:make: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
|
|
|
* @var bool |
63
|
|
|
*/ |
64
|
|
|
protected $blockable; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @var bool |
68
|
|
|
*/ |
69
|
|
|
protected $translatable; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @var bool |
73
|
|
|
*/ |
74
|
|
|
protected $sluggable; |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @var bool |
78
|
|
|
*/ |
79
|
|
|
protected $mediable; |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @var bool |
83
|
|
|
*/ |
84
|
|
|
protected $fileable; |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @var bool |
88
|
|
|
*/ |
89
|
|
|
protected $sortable; |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @var bool |
93
|
|
|
*/ |
94
|
|
|
protected $revisionable; |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @var bool |
98
|
|
|
*/ |
99
|
|
|
protected $defaultsAnswserToNo; |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @var bool |
103
|
|
|
*/ |
104
|
|
|
protected $isCapsule = false; |
105
|
69 |
|
|
106
|
|
|
/** |
107
|
69 |
|
* @var string |
108
|
|
|
*/ |
109
|
69 |
|
protected $moduleBasePath; |
110
|
69 |
|
|
111
|
69 |
|
/** |
112
|
|
|
* @var string |
113
|
69 |
|
*/ |
114
|
69 |
|
protected $capsule; |
115
|
69 |
|
|
116
|
69 |
|
/** |
117
|
69 |
|
* @param Filesystem $files |
118
|
69 |
|
* @param Composer $composer |
119
|
69 |
|
* @param Config $config |
120
|
|
|
*/ |
121
|
69 |
|
public function __construct(Filesystem $files, Composer $composer, Config $config) |
122
|
|
|
{ |
123
|
69 |
|
parent::__construct(); |
124
|
69 |
|
|
125
|
69 |
|
$this->files = $files; |
126
|
|
|
$this->composer = $composer; |
127
|
|
|
$this->config = $config; |
128
|
|
|
|
129
|
|
|
$this->blockable = false; |
130
|
|
|
$this->translatable = false; |
131
|
|
|
$this->sluggable = false; |
132
|
1 |
|
$this->mediable = false; |
133
|
|
|
$this->fileable = false; |
134
|
1 |
|
$this->sortable = false; |
135
|
|
|
$this->revisionable = false; |
136
|
1 |
|
|
137
|
1 |
|
$this->defaultsAnswserToNo = false; |
138
|
|
|
|
139
|
|
|
$this->modelTraits = ['HasBlocks', 'HasTranslation', 'HasSlug', 'HasMedias', 'HasFiles', 'HasRevisions', 'HasPosition']; |
140
|
|
|
$this->repositoryTraits = ['HandleBlocks', 'HandleTranslations', 'HandleSlugs', 'HandleMedias', 'HandleFiles', 'HandleRevisions']; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
protected function checkCapsuleDirectory($dir) |
144
|
|
|
{ |
145
|
1 |
|
if (file_exists($dir)) { |
146
|
1 |
|
if (!$this->option('force')) { |
147
|
|
|
$answer = $this->choice("Capsule path exists ({$dir}). Erase and overwrite?", |
148
|
1 |
|
['no', 'yes'], $this->defaultsAnswserToNo |
149
|
1 |
|
? 0 |
150
|
|
|
: 1); |
151
|
|
|
} |
152
|
1 |
|
|
153
|
1 |
|
if ('yes' === ($answer ?? 'no') || $this->option('force')) { |
154
|
1 |
|
File::deleteDirectory($dir); |
155
|
1 |
|
|
156
|
1 |
|
if (file_exists($dir)) { |
157
|
1 |
|
$this->info("Directory could not be deleted. Aborted."); |
158
|
1 |
|
die; |
|
|
|
|
159
|
|
|
} |
160
|
|
|
} else { |
161
|
1 |
|
$this->info("Aborted"); |
162
|
1 |
|
|
163
|
1 |
|
die; |
|
|
|
|
164
|
1 |
|
} |
165
|
1 |
|
} |
166
|
1 |
|
} |
167
|
1 |
|
|
168
|
|
|
/** |
169
|
|
|
* Executes the console command. |
170
|
1 |
|
* |
171
|
|
|
* @return mixed |
172
|
1 |
|
*/ |
173
|
1 |
|
public function handle() |
174
|
1 |
|
{ |
175
|
1 |
|
$moduleName = Str::camel(Str::plural(lcfirst($this->argument('moduleName')))); |
|
|
|
|
176
|
1 |
|
|
177
|
1 |
|
$this->capsule = app('twill.capsules.manager')->makeCapsule(['name' => $moduleName], config("twill.capsules.path")); |
|
|
|
|
178
|
|
|
|
179
|
1 |
|
$enabledOptions = Collection::make($this->options())->only([ |
180
|
1 |
|
'hasBlocks', |
181
|
|
|
'hasTranslation', |
182
|
1 |
|
'hasSlug', |
183
|
1 |
|
'hasMedias', |
184
|
1 |
|
'hasFiles', |
185
|
1 |
|
'hasPosition', |
186
|
|
|
'hasRevisions', |
187
|
|
|
])->filter(function ($enabled) { |
188
|
|
|
return $enabled; |
189
|
|
|
}); |
190
|
1 |
|
|
191
|
|
|
if (count($enabledOptions) > 0) { |
192
|
1 |
|
$this->defaultsAnswserToNo = true; |
193
|
|
|
} |
194
|
1 |
|
|
195
|
1 |
|
$this->blockable = $this->checkOption('hasBlocks'); |
196
|
|
|
$this->translatable = $this->checkOption('hasTranslation'); |
197
|
|
|
$this->sluggable = $this->checkOption('hasSlug'); |
198
|
|
|
$this->mediable = $this->checkOption('hasMedias'); |
199
|
|
|
$this->fileable = $this->checkOption('hasFiles'); |
200
|
|
|
$this->sortable = $this->checkOption('hasPosition'); |
201
|
|
|
$this->revisionable = $this->checkOption('hasRevisions'); |
202
|
|
|
|
203
|
|
|
$activeTraits = [ |
204
|
1 |
|
$this->blockable, |
205
|
|
|
$this->translatable, |
206
|
1 |
|
$this->sluggable, |
207
|
1 |
|
$this->mediable, |
208
|
|
|
$this->fileable, |
209
|
1 |
|
$this->revisionable, |
210
|
|
|
$this->sortable, |
211
|
1 |
|
]; |
212
|
|
|
|
213
|
1 |
|
$modelName = Str::studly(Str::singular($moduleName)); |
214
|
1 |
|
|
215
|
|
|
$this->createCapsuleNamespace(Str::studly($moduleName), $modelName); |
216
|
1 |
|
|
217
|
|
|
$this->createCapsulePath(Str::studly($moduleName), $modelName); |
218
|
1 |
|
|
219
|
1 |
|
$this->createMigration($moduleName); |
220
|
1 |
|
$this->createModels($modelName, $activeTraits); |
221
|
1 |
|
$this->createRepository($modelName, $activeTraits); |
222
|
|
|
$this->createController($moduleName, $modelName); |
223
|
|
|
$this->createRequest($modelName); |
224
|
1 |
|
$this->createViews($moduleName); |
225
|
1 |
|
|
226
|
|
|
if ($this->isCapsule) { |
227
|
|
|
$this->createRoutes($moduleName); |
|
|
|
|
228
|
|
|
$this->createSeed($moduleName); |
229
|
|
|
} else { |
230
|
|
|
$this->info("Add Route::module('{$moduleName}'); to your admin routes file."); |
231
|
|
|
} |
232
|
|
|
|
233
|
1 |
|
$this->info("Setup a new CMS menu item in config/twill-navigation.php:"); |
234
|
1 |
|
|
235
|
1 |
|
$navTitle = Str::studly($moduleName); |
236
|
1 |
|
|
237
|
|
|
$this->info(" |
238
|
1 |
|
'{$moduleName}' => [ |
239
|
|
|
'title' => '{$navTitle}', |
240
|
1 |
|
'module' => true |
241
|
|
|
] |
242
|
1 |
|
"); |
243
|
|
|
|
244
|
1 |
|
if ($this->isCapsule) { |
245
|
|
|
$this->info("Setup your new Capsule on config/twill.php:"); |
246
|
|
|
|
247
|
|
|
$navTitle = Str::studly($moduleName); |
|
|
|
|
248
|
|
|
|
249
|
|
|
$this->info(" |
250
|
|
|
'capsules' => [ |
251
|
|
|
'list' => [ |
252
|
|
|
[ |
253
|
1 |
|
'name' => '{$this->capsule['name']}', |
254
|
|
|
'enabled' => true |
255
|
1 |
|
] |
256
|
|
|
] |
257
|
1 |
|
] |
258
|
|
|
"); |
259
|
1 |
|
} |
260
|
1 |
|
|
261
|
|
|
$this->info("Migrate your database.\n"); |
262
|
1 |
|
|
263
|
|
|
$this->info("Enjoy."); |
264
|
1 |
|
|
265
|
1 |
|
$this->composer->dumpAutoloads(); |
266
|
1 |
|
} |
267
|
1 |
|
|
268
|
|
|
/** |
269
|
|
|
* Creates a new module database migration file. |
270
|
1 |
|
* |
271
|
|
|
* @param string $moduleName |
272
|
|
|
* @return void |
273
|
1 |
|
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException |
274
|
1 |
|
*/ |
275
|
|
|
private function createMigration($moduleName = 'items') |
276
|
1 |
|
{ |
277
|
|
|
$table = Str::snake($moduleName); |
278
|
1 |
|
$tableClassName = Str::studly($table); |
279
|
|
|
|
280
|
1 |
|
$className = "Create{$tableClassName}Tables"; |
|
|
|
|
281
|
|
|
|
282
|
|
|
$migrationName = 'create_' . $table . '_tables'; |
283
|
1 |
|
|
284
|
1 |
|
if (!count(glob($this->databasePath('migrations/*' . $migrationName . '.php')))) { |
285
|
|
|
$migrationPath = $this->databasePath() . '/migrations'; |
286
|
1 |
|
|
287
|
|
|
$this->makeDir($migrationPath); |
288
|
1 |
|
|
289
|
|
|
$fullPath = $this->laravel['migration.creator']->create($migrationName, $migrationPath); |
290
|
1 |
|
|
291
|
|
|
$stub = str_replace( |
292
|
|
|
['{{table}}', '{{singularTableName}}', '{{tableClassName}}'], |
293
|
1 |
|
[$table, Str::singular($table), $tableClassName], |
294
|
|
|
$this->files->get(__DIR__ . '/stubs/migration.stub') |
295
|
1 |
|
); |
296
|
1 |
|
|
297
|
1 |
|
if ($this->translatable) { |
298
|
|
|
$stub = preg_replace('/{{!hasTranslation}}[\s\S]+?{{\/!hasTranslation}}/', '', $stub); |
299
|
|
|
} else { |
300
|
|
|
$stub = str_replace([ |
301
|
1 |
|
'{{!hasTranslation}}', |
302
|
|
|
'{{/!hasTranslation}}', |
303
|
1 |
|
], '', $stub); |
304
|
|
|
} |
305
|
1 |
|
|
306
|
|
|
$stub = $this->renderStubForOption($stub, 'hasTranslation', $this->translatable); |
307
|
1 |
|
$stub = $this->renderStubForOption($stub, 'hasSlug', $this->sluggable); |
308
|
1 |
|
$stub = $this->renderStubForOption($stub, 'hasRevisions', $this->revisionable); |
309
|
|
|
$stub = $this->renderStubForOption($stub, 'hasPosition', $this->sortable); |
310
|
|
|
|
311
|
1 |
|
$stub = preg_replace('/\}\);[\s\S]+?Schema::create/', "});\n\n Schema::create", $stub); |
312
|
1 |
|
|
313
|
|
|
$this->files->put($fullPath, $stub); |
314
|
|
|
|
315
|
|
|
$this->info("Migration created successfully! Add some fields!"); |
316
|
|
|
} |
317
|
1 |
|
} |
318
|
1 |
|
|
319
|
1 |
|
/** |
320
|
1 |
|
* Creates new model class files for the given model name and traits. |
321
|
1 |
|
* |
322
|
|
|
* @param string $modelName |
323
|
1 |
|
* @param array $activeTraits |
324
|
1 |
|
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException |
325
|
1 |
|
*/ |
326
|
1 |
|
private function createModels($modelName = 'Item', $activeTraits = []) |
327
|
|
|
{ |
328
|
1 |
|
$modelClassName = $this->namespace('models', 'Models', $modelName); |
329
|
|
|
|
330
|
1 |
|
$modelsDir = $this->isCapsule ? $this->capsule['models_dir'] : 'Models'; |
331
|
1 |
|
|
332
|
|
|
$this->makeTwillDirectory($modelsDir); |
333
|
1 |
|
|
334
|
|
|
if ($this->translatable) { |
335
|
1 |
|
$this->makeTwillDirectory($baseDir = $this->isCapsule ? $modelsDir : "{$modelsDir}/Translations"); |
336
|
1 |
|
|
337
|
1 |
|
$modelTranslationClassName = $modelName . 'Translation'; |
338
|
1 |
|
|
339
|
1 |
|
$stub = str_replace( |
340
|
|
|
['{{modelTranslationClassName}}', '{{modelClassWithNamespace}}', '{{modelClassName}}', '{{namespace}}', '{{baseTranslationModel}}'], |
341
|
|
|
[$modelTranslationClassName, $modelClassName, $modelName, $this->namespace('models', 'Models\Translations'), config('twill.base_translation_model')], |
342
|
|
|
$this->files->get(__DIR__ . '/stubs/model_translation.stub') |
343
|
|
|
); |
344
|
1 |
|
|
345
|
|
|
twill_put_stub(twill_path("{$baseDir}/" . $modelTranslationClassName . '.php'), $stub); |
346
|
|
|
} |
347
|
|
|
|
348
|
|
|
if ($this->sluggable) { |
349
|
|
|
$this->makeTwillDirectory($baseDir = $this->isCapsule ? $modelsDir : "{$modelsDir}/Slugs"); |
350
|
|
|
|
351
|
|
|
$modelSlugClassName = $modelName . 'Slug'; |
352
|
|
|
|
353
|
|
|
$stub = str_replace( |
354
|
|
|
['{{modelSlugClassName}}', '{{modelClassWithNamespace}}', '{{modelName}}', '{{namespace}}', '{{baseSlugModel}}'], |
355
|
1 |
|
[$modelSlugClassName, $modelClassName, Str::snake($modelName), $this->namespace('models', 'Models\Slugs'), config('twill.base_slug_model')], |
356
|
|
|
$this->files->get(__DIR__ . '/stubs/model_slug.stub') |
357
|
1 |
|
); |
358
|
|
|
|
359
|
1 |
|
twill_put_stub(twill_path("{$baseDir}/" . $modelSlugClassName . '.php'), $stub); |
360
|
|
|
} |
361
|
1 |
|
|
362
|
|
|
if ($this->revisionable) { |
363
|
1 |
|
$this->makeTwillDirectory($baseDir = $this->isCapsule ? $modelsDir : "{$modelsDir}/Revisions"); |
364
|
1 |
|
|
365
|
1 |
|
$modelRevisionClassName = $modelName . 'Revision'; |
366
|
|
|
|
367
|
|
|
$stub = str_replace( |
368
|
|
|
['{{modelRevisionClassName}}', '{{modelClassWithNamespace}}', '{{modelName}}', '{{namespace}}', '{{baseRevisionModel}}'], |
369
|
1 |
|
[$modelRevisionClassName, $modelClassName, Str::snake($modelName), $this->namespace('models', 'Models\Revisions'), config('twill.base_revision_model')], |
370
|
|
|
$this->files->get(__DIR__ . '/stubs/model_revision.stub') |
371
|
1 |
|
); |
372
|
|
|
|
373
|
1 |
|
twill_put_stub(twill_path("{$baseDir}/" . $modelRevisionClassName . '.php'), $stub); |
374
|
|
|
} |
375
|
1 |
|
|
376
|
|
|
$activeModelTraits = []; |
377
|
1 |
|
|
378
|
1 |
|
foreach ($activeTraits as $index => $traitIsActive) { |
379
|
|
|
if ($traitIsActive) { |
380
|
|
|
!isset($this->modelTraits[$index]) ?: $activeModelTraits[] = $this->modelTraits[$index]; |
381
|
|
|
} |
382
|
|
|
} |
383
|
|
|
|
384
|
|
|
$activeModelTraitsString = empty($activeModelTraits) ? '' : 'use ' . rtrim(implode(', ', $activeModelTraits), ', ') . ';'; |
385
|
|
|
|
386
|
|
|
$activeModelTraitsImports = empty($activeModelTraits) ? '' : "use A17\Twill\Models\Behaviors\\" . implode(";\nuse A17\Twill\Models\Behaviors\\", $activeModelTraits) . ";"; |
387
|
|
|
|
388
|
1 |
|
$activeModelImplements = $this->sortable ? 'implements Sortable' : ''; |
389
|
|
|
|
390
|
1 |
|
if ($this->sortable) { |
391
|
|
|
$activeModelTraitsImports .= "\nuse A17\Twill\Models\Behaviors\Sortable;"; |
392
|
1 |
|
} |
393
|
|
|
|
394
|
1 |
|
$stub = str_replace([ |
395
|
1 |
|
'{{modelClassName}}', |
396
|
1 |
|
'{{modelTraits}}', |
397
|
1 |
|
'{{modelImports}}', |
398
|
|
|
'{{modelImplements}}', |
399
|
|
|
'{{namespace}}', |
400
|
1 |
|
'{{baseModel}}', |
401
|
|
|
], [ |
402
|
1 |
|
$modelName, |
403
|
1 |
|
$activeModelTraitsString, |
404
|
|
|
$activeModelTraitsImports, |
405
|
|
|
$activeModelImplements, |
406
|
|
|
$this->namespace('models', 'Models'), |
407
|
|
|
config('twill.base_model'), |
408
|
|
|
], $this->files->get(__DIR__ . '/stubs/model.stub')); |
409
|
|
|
|
410
|
|
|
$stub = $this->renderStubForOption($stub, 'hasTranslation', $this->translatable); |
411
|
|
|
$stub = $this->renderStubForOption($stub, 'hasSlug', $this->sluggable); |
412
|
1 |
|
$stub = $this->renderStubForOption($stub, 'hasMedias', $this->mediable); |
413
|
|
|
$stub = $this->renderStubForOption($stub, 'hasPosition', $this->sortable); |
414
|
1 |
|
|
415
|
|
|
twill_put_stub(twill_path("{$modelsDir}/" . $modelName . '.php'), $stub); |
416
|
1 |
|
|
417
|
|
|
$this->info("Models created successfully! Fill your fillables!"); |
418
|
1 |
|
} |
419
|
|
|
|
420
|
1 |
|
private function renderStubForOption($stub, $option, $enabled) |
421
|
|
|
{ |
422
|
1 |
|
if ($enabled) { |
423
|
1 |
|
$stub = str_replace([ |
424
|
|
|
'{{' . $option . '}}', |
425
|
|
|
'{{/' . $option . '}}', |
426
|
|
|
], '', $stub); |
427
|
|
|
} else { |
428
|
|
|
$stub = preg_replace('/{{' . $option . '}}[\s\S]+?{{\/' . $option . '}}/', '', $stub); |
429
|
|
|
} |
430
|
|
|
|
431
|
|
|
return $stub; |
432
|
1 |
|
} |
433
|
|
|
|
434
|
1 |
|
/** |
435
|
|
|
* Creates new repository class file for the given model name. |
436
|
1 |
|
* |
437
|
|
|
* @param string $modelName |
438
|
1 |
|
* @param array $activeTraits |
439
|
|
|
* @return void |
440
|
1 |
|
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException |
441
|
|
|
*/ |
442
|
1 |
|
private function createRepository($modelName = 'Item', $activeTraits = []) |
443
|
1 |
|
{ |
444
|
|
|
$modelsDir = $this->isCapsule ? $this->capsule['repositories_dir'] : 'Repositories'; |
445
|
1 |
|
|
446
|
|
|
$modelClass = $this->isCapsule ? $this->capsule['model'] : "App\Models\\{$this->capsule['singular']}"; |
447
|
1 |
|
|
448
|
1 |
|
$this->makeTwillDirectory($modelsDir); |
449
|
|
|
|
450
|
|
|
$repositoryClassName = $modelName . 'Repository'; |
451
|
|
|
|
452
|
|
|
$activeRepositoryTraits = []; |
453
|
|
|
|
454
|
|
|
foreach ($activeTraits as $index => $traitIsActive) { |
455
|
|
|
if ($traitIsActive) { |
456
|
|
|
!isset($this->repositoryTraits[$index]) ?: $activeRepositoryTraits[] = $this->repositoryTraits[$index]; |
457
|
|
|
} |
458
|
|
|
} |
459
|
|
|
|
460
|
|
|
$activeRepositoryTraitsString = empty($activeRepositoryTraits) ? '' : 'use ' . (empty($activeRepositoryTraits) ? "" : rtrim(implode(', ', $activeRepositoryTraits), ', ') . ';'); |
461
|
|
|
|
462
|
|
|
$activeRepositoryTraitsImports = empty($activeRepositoryTraits) ? '' : "use A17\Twill\Repositories\Behaviors\\" . implode(";\nuse A17\Twill\Repositories\Behaviors\\", $activeRepositoryTraits) . ";"; |
463
|
|
|
|
464
|
|
|
$stub = str_replace( |
465
|
|
|
['{{repositoryClassName}}', '{{modelName}}', '{{repositoryTraits}}', '{{repositoryImports}}', '{{namespace}}', '{{modelClass}}', '{{baseRepository}}'], |
466
|
|
|
[$repositoryClassName, $modelName, $activeRepositoryTraitsString, $activeRepositoryTraitsImports, $this->namespace('repositories', 'Repositories'), $modelClass, config('twill.base_repository')], |
467
|
|
|
$this->files->get(__DIR__ . '/stubs/repository.stub') |
468
|
|
|
); |
469
|
|
|
|
470
|
|
|
twill_put_stub(twill_path("{$modelsDir}/" . $repositoryClassName . '.php'), $stub); |
471
|
|
|
|
472
|
|
|
$this->info("Repository created successfully! Control all the things!"); |
473
|
|
|
} |
474
|
|
|
|
475
|
|
|
/** |
476
|
|
|
* Create a new controller class file for the given module name and model name. |
477
|
|
|
* |
478
|
|
|
* @param string $moduleName |
479
|
|
|
* @param string $modelName |
480
|
|
|
* @return void |
481
|
|
|
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException |
482
|
|
|
*/ |
483
|
|
|
private function createController($moduleName = 'items', $modelName = 'Item') |
484
|
|
|
{ |
485
|
|
|
$controllerClassName = $modelName . 'Controller'; |
486
|
|
|
|
487
|
|
|
$dir = $this->isCapsule ? $this->capsule['controllers_dir'] : 'Http/Controllers/Admin'; |
488
|
|
|
|
489
|
|
|
$this->makeTwillDirectory($dir); |
490
|
|
|
|
491
|
|
|
$stub = str_replace( |
492
|
|
|
['{{moduleName}}', '{{controllerClassName}}', '{{namespace}}', '{{baseController}}'], |
493
|
|
|
[$moduleName, $controllerClassName, $this->namespace('controllers', 'Http\Controllers\Admin'), config('twill.base_controller')], |
494
|
|
|
$this->files->get(__DIR__ . '/stubs/controller.stub') |
495
|
|
|
); |
496
|
|
|
|
497
|
|
|
twill_put_stub(twill_path("{$dir}/" . $controllerClassName . '.php'), $stub); |
498
|
|
|
|
499
|
|
|
$this->info("Controller created successfully! Define your index/browser/form endpoints options!"); |
500
|
|
|
} |
501
|
|
|
|
502
|
|
|
/** |
503
|
|
|
* Creates a new request class file for the given model name. |
504
|
|
|
* |
505
|
|
|
* @param string $modelName |
506
|
|
|
* @return void |
507
|
|
|
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException |
508
|
|
|
*/ |
509
|
|
|
private function createRequest($modelName = 'Item') |
510
|
|
|
{ |
511
|
|
|
$dir = $this->isCapsule ? $this->capsule['requests_dir'] : 'Http/Requests/Admin'; |
512
|
|
|
|
513
|
|
|
$this->makeTwillDirectory($dir); |
514
|
|
|
|
515
|
|
|
$requestClassName = $modelName . 'Request'; |
516
|
|
|
|
517
|
|
|
$stub = str_replace( |
518
|
|
|
['{{requestClassName}}', '{{namespace}}', '{{baseRequest}}'], |
519
|
|
|
[$requestClassName, $this->namespace('requests', 'Http\Requests\Admin'), config('twill.base_request')], |
520
|
|
|
$this->files->get(__DIR__ . '/stubs/request.stub') |
521
|
|
|
); |
522
|
|
|
|
523
|
|
|
twill_put_stub(twill_path("{$dir}/" . $requestClassName . '.php'), $stub); |
524
|
|
|
|
525
|
|
|
$this->info("Form request created successfully! Add some validation rules!"); |
526
|
|
|
} |
527
|
|
|
|
528
|
|
|
/** |
529
|
|
|
* Creates appropriate module Blade view files. |
530
|
|
|
* |
531
|
|
|
* @param string $moduleName |
532
|
|
|
* @return void |
533
|
|
|
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException |
534
|
|
|
*/ |
535
|
|
|
private function createViews($moduleName = 'items') |
536
|
|
|
{ |
537
|
|
|
$viewsPath = $this->viewPath($moduleName); |
538
|
|
|
|
539
|
|
|
$this->makeTwillDirectory($viewsPath); |
540
|
|
|
|
541
|
|
|
$formView = $this->translatable ? 'form_translatable' : 'form'; |
542
|
|
|
|
543
|
|
|
twill_put_stub($viewsPath . '/form.blade.php', $this->files->get(__DIR__ . '/stubs/' . $formView . '.blade.stub')); |
544
|
|
|
|
545
|
|
|
$this->info("Form view created successfully! Include your form fields using @formField directives!"); |
546
|
|
|
} |
547
|
|
|
|
548
|
|
|
/** |
549
|
|
|
* Creates a basic routes file for the Capsule. |
550
|
|
|
* |
551
|
|
|
* @param string $moduleName |
552
|
|
|
* @return void |
553
|
|
|
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException |
554
|
|
|
*/ |
555
|
|
|
public function createRoutes() |
556
|
|
|
{ |
557
|
|
|
$this->makeDir($this->capsule['routes_file']); |
558
|
|
|
|
559
|
|
|
$contents = str_replace( |
560
|
|
|
'{{moduleName}}', |
561
|
|
|
$this->capsule['module'], |
562
|
|
|
$this->files->get(__DIR__ . '/stubs/routes_admin.stub') |
563
|
|
|
); |
564
|
|
|
|
565
|
|
|
twill_put_stub($this->capsule['routes_file'], $contents); |
566
|
|
|
|
567
|
|
|
$this->info("Routes file created successfully!"); |
568
|
|
|
} |
569
|
|
|
|
570
|
|
|
/** |
571
|
|
|
* Creates a new module database seed file. |
572
|
|
|
* |
573
|
|
|
* @param string $moduleName |
574
|
|
|
* @return void |
575
|
|
|
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException |
576
|
|
|
*/ |
577
|
|
|
private function createSeed($moduleName = 'items') |
578
|
|
|
{ |
579
|
|
|
$this->makeTwillDirectory($this->capsule['seeds_psr4_path']); |
580
|
|
|
|
581
|
|
|
$stub = $this->files->get(__DIR__ . '/stubs/database_seeder.stub'); |
582
|
|
|
|
583
|
|
|
$stub = str_replace('{moduleName}', $this->capsule['plural'], $stub); |
584
|
|
|
|
585
|
|
|
$this->files->put("{$this->capsule['seeds_psr4_path']}/DatabaseSeeder.php", $stub); |
586
|
|
|
|
587
|
|
|
$this->info("Seed created successfully!"); |
588
|
|
|
} |
589
|
|
|
|
590
|
|
|
private function checkOption($option) |
591
|
|
|
{ |
592
|
|
|
if ($this->option($option) || $this->option('all')) { |
593
|
|
|
return true; |
594
|
|
|
} |
595
|
|
|
|
596
|
|
|
$questions = [ |
597
|
|
|
'hasBlocks' => 'Do you need to use the block editor on this module?', |
598
|
|
|
'hasTranslation' => 'Do you need to translate content on this module?', |
599
|
|
|
'hasSlug' => 'Do you need to generate slugs on this module?', |
600
|
|
|
'hasMedias' => 'Do you need to attach images on this module?', |
601
|
|
|
'hasFiles' => 'Do you need to attach files on this module?', |
602
|
|
|
'hasPosition' => 'Do you need to manage the position of records on this module?', |
603
|
|
|
'hasRevisions' => 'Do you need to enable revisions on this module?', |
604
|
|
|
]; |
605
|
|
|
|
606
|
|
|
return 'yes' === $this->choice($questions[$option], ['no', 'yes'], $this->defaultsAnswserToNo ? 0 : 1); |
607
|
|
|
} |
608
|
|
|
|
609
|
|
|
public function createCapsulePath($moduleName, $modelName) |
610
|
|
|
{ |
611
|
|
|
if (!$this->isCapsule) { |
612
|
|
|
$this->moduleBasePath = base_path(); |
613
|
|
|
|
614
|
|
|
return; |
615
|
|
|
} |
616
|
|
|
|
617
|
|
|
$this->checkCapsuleDirectory( |
618
|
|
|
$this->moduleBasePath = config('twill.capsules.path') . "/{$moduleName}" |
619
|
|
|
); |
620
|
|
|
|
621
|
|
|
$this->makeDir($this->moduleBasePath); |
622
|
|
|
} |
623
|
|
|
|
624
|
|
|
public function createCapsuleNamespace($module, $model) |
625
|
|
|
{ |
626
|
|
|
$base = config('twill.capsules.namespace'); |
627
|
|
|
|
628
|
|
|
$this->capsuleNamespace = "{$base}\\{$module}"; |
|
|
|
|
629
|
|
|
} |
630
|
|
|
|
631
|
|
|
public function databasePath($path = '') |
632
|
|
|
{ |
633
|
|
|
if (!$this->isCapsule) { |
634
|
|
|
return database_path($path); |
635
|
|
|
} |
636
|
|
|
|
637
|
|
|
return "{$this->moduleBasePath}/database" . (filled($path) ? "/{$path}" : ''); |
638
|
|
|
} |
639
|
|
|
|
640
|
|
|
public function makeDir($dir) |
641
|
|
|
{ |
642
|
|
|
$info = pathinfo($dir); |
643
|
|
|
|
644
|
|
|
$dir = isset($info['extension']) ? $info['dirname'] : $dir; |
645
|
|
|
|
646
|
|
|
if (!is_dir($dir)) { |
647
|
|
|
mkdir($dir, 0755, true); |
648
|
|
|
} |
649
|
|
|
|
650
|
|
|
if (!is_dir($dir)) { |
651
|
|
|
$this->info("It wasn't possible to create capsule directory {$dir}"); |
652
|
|
|
|
653
|
|
|
die; |
|
|
|
|
654
|
|
|
} |
655
|
|
|
} |
656
|
|
|
|
657
|
|
|
public function makeTwillDirectory($path) |
658
|
|
|
{ |
659
|
|
|
make_twill_directory($path); |
660
|
|
|
} |
661
|
|
|
|
662
|
|
|
public function namespace ($type, $suffix, $class = null) { |
663
|
|
|
$class = (filled($class) ? "\\$class" : ''); |
664
|
|
|
|
665
|
|
|
if (!$this->isCapsule) { |
666
|
|
|
return "App\\{$suffix}{$class}"; |
667
|
|
|
} |
668
|
|
|
|
669
|
|
|
return $this->capsule[$type] . $class; |
670
|
|
|
} |
671
|
|
|
|
672
|
|
|
public function viewPath($moduleName) |
673
|
|
|
{ |
674
|
|
|
if (!$this->isCapsule) { |
675
|
|
|
return $viewsPath = $this->config->get('view.paths')[0] . '/admin/' . $moduleName; |
|
|
|
|
676
|
|
|
} |
677
|
|
|
|
678
|
|
|
$this->makeDir($dir = "{$this->moduleBasePath}/resources/views/admin"); |
679
|
|
|
|
680
|
|
|
return $dir; |
681
|
|
|
} |
682
|
|
|
} |
683
|
|
|
|
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.