|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace A17\Twill\Commands; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\File; |
|
6
|
|
|
use Illuminate\Config\Repository as Config; |
|
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: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::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 |
|
$this->createRoutes($moduleName); |
|
|
|
|
|
|
226
|
|
|
|
|
227
|
|
|
$this->info("Add Route::module('{$moduleName}'); to your admin routes file."); |
|
228
|
|
|
$this->info("Setup a new CMS menu item in config/twill-navigation.php:"); |
|
229
|
|
|
|
|
230
|
|
|
$navTitle = Str::studly($moduleName); |
|
231
|
|
|
|
|
232
|
|
|
$this->info(" |
|
233
|
1 |
|
'{$moduleName}' => [ |
|
234
|
1 |
|
'title' => '{$navTitle}', |
|
235
|
1 |
|
'module' => true |
|
236
|
1 |
|
] |
|
237
|
|
|
"); |
|
238
|
1 |
|
|
|
239
|
|
|
if ($this->isCapsule) { |
|
240
|
1 |
|
$this->info("Setup your new Capsule on config/twill.php:"); |
|
241
|
|
|
|
|
242
|
1 |
|
$navTitle = Str::studly($moduleName); |
|
|
|
|
|
|
243
|
|
|
|
|
244
|
1 |
|
$this->info(" |
|
245
|
|
|
'capsules' => [ |
|
246
|
|
|
'name' => '{$this->capsule['name']}', |
|
247
|
|
|
'enabled' => true |
|
248
|
|
|
] |
|
249
|
|
|
"); |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
$this->info("Migrate your database.\n"); |
|
253
|
1 |
|
|
|
254
|
|
|
$this->info("Enjoy."); |
|
255
|
1 |
|
|
|
256
|
|
|
$this->composer->dumpAutoloads(); |
|
257
|
1 |
|
} |
|
258
|
|
|
|
|
259
|
1 |
|
/** |
|
260
|
1 |
|
* Creates a new module database migration file. |
|
261
|
|
|
* |
|
262
|
1 |
|
* @param string $moduleName |
|
263
|
|
|
* @return void |
|
264
|
1 |
|
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException |
|
265
|
1 |
|
*/ |
|
266
|
1 |
|
private function createMigration($moduleName = 'items') |
|
267
|
1 |
|
{ |
|
268
|
|
|
$table = Str::snake($moduleName); |
|
269
|
|
|
$tableClassName = Str::studly($table); |
|
270
|
1 |
|
|
|
271
|
|
|
$className = "Create{$tableClassName}Tables"; |
|
|
|
|
|
|
272
|
|
|
|
|
273
|
1 |
|
$migrationName = 'create_' . $table . '_tables'; |
|
274
|
1 |
|
|
|
275
|
|
|
if (!count(glob($this->databasePath('migrations/*' . $migrationName . '.php')))) { |
|
|
|
|
|
|
276
|
1 |
|
$migrationPath = $this->databasePath() . '/migrations'; |
|
277
|
|
|
|
|
278
|
1 |
|
$this->makeDir($migrationPath); |
|
279
|
|
|
|
|
280
|
1 |
|
$fullPath = $this->laravel['migration.creator']->create($migrationName, $migrationPath); |
|
281
|
|
|
|
|
282
|
|
|
$stub = str_replace( |
|
283
|
1 |
|
['{{table}}', '{{singularTableName}}', '{{tableClassName}}'], |
|
284
|
1 |
|
[$table, Str::singular($table), $tableClassName], |
|
285
|
|
|
$this->files->get(__DIR__ . '/stubs/migration.stub') |
|
286
|
1 |
|
); |
|
287
|
|
|
|
|
288
|
1 |
|
if ($this->translatable) { |
|
289
|
|
|
$stub = preg_replace('/{{!hasTranslation}}[\s\S]+?{{\/!hasTranslation}}/', '', $stub); |
|
290
|
1 |
|
} else { |
|
291
|
|
|
$stub = str_replace([ |
|
292
|
|
|
'{{!hasTranslation}}', |
|
293
|
1 |
|
'{{/!hasTranslation}}', |
|
294
|
|
|
], '', $stub); |
|
295
|
1 |
|
} |
|
296
|
1 |
|
|
|
297
|
1 |
|
$stub = $this->renderStubForOption($stub, 'hasTranslation', $this->translatable); |
|
298
|
|
|
$stub = $this->renderStubForOption($stub, 'hasSlug', $this->sluggable); |
|
299
|
|
|
$stub = $this->renderStubForOption($stub, 'hasRevisions', $this->revisionable); |
|
300
|
|
|
$stub = $this->renderStubForOption($stub, 'hasPosition', $this->sortable); |
|
301
|
1 |
|
|
|
302
|
|
|
$stub = preg_replace('/\}\);[\s\S]+?Schema::create/', "});\n\n Schema::create", $stub); |
|
303
|
1 |
|
|
|
304
|
|
|
$this->files->put($fullPath, $stub); |
|
305
|
1 |
|
|
|
306
|
|
|
$this->info("Migration created successfully! Add some fields!"); |
|
307
|
1 |
|
} |
|
308
|
1 |
|
} |
|
309
|
|
|
|
|
310
|
|
|
/** |
|
311
|
1 |
|
* Creates new model class files for the given model name and traits. |
|
312
|
1 |
|
* |
|
313
|
|
|
* @param string $modelName |
|
314
|
|
|
* @param array $activeTraits |
|
315
|
|
|
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException |
|
316
|
|
|
*/ |
|
317
|
1 |
|
private function createModels($modelName = 'Item', $activeTraits = []) |
|
318
|
1 |
|
{ |
|
319
|
1 |
|
$modelClassName = $this->namespace('models', 'Models', $modelName); |
|
320
|
1 |
|
|
|
321
|
1 |
|
$modelsDir = $this->isCapsule ? $this->capsule['models_dir'] : 'Models'; |
|
322
|
|
|
|
|
323
|
1 |
|
$this->makeTwillDirectory($modelsDir); |
|
324
|
1 |
|
|
|
325
|
1 |
|
if ($this->translatable) { |
|
326
|
1 |
|
$this->makeTwillDirectory($baseDir = $this->isCapsule ? $modelsDir : "{$modelsDir}/Translations"); |
|
327
|
|
|
|
|
328
|
1 |
|
$modelTranslationClassName = $modelName . 'Translation'; |
|
329
|
|
|
|
|
330
|
1 |
|
$stub = str_replace( |
|
331
|
1 |
|
['{{modelTranslationClassName}}', '{{modelClassWithNamespace}}', '{{modelClassName}}', '{{namespace}}'], |
|
332
|
|
|
[$modelTranslationClassName, $modelClassName, $modelName, $this->namespace('models', 'Models\Translations')], |
|
333
|
1 |
|
$this->files->get(__DIR__ . '/stubs/model_translation.stub') |
|
334
|
|
|
); |
|
335
|
1 |
|
|
|
336
|
1 |
|
twill_put_stub(twill_path("{$baseDir}/" . $modelTranslationClassName . '.php'), $stub); |
|
337
|
1 |
|
} |
|
338
|
1 |
|
|
|
339
|
1 |
|
if ($this->sluggable) { |
|
340
|
|
|
$this->makeTwillDirectory($baseDir = $this->isCapsule ? $modelsDir : "{$modelsDir}/Slugs"); |
|
341
|
|
|
|
|
342
|
|
|
$modelSlugClassName = $modelName . 'Slug'; |
|
343
|
|
|
|
|
344
|
1 |
|
$stub = str_replace( |
|
345
|
|
|
['{{modelSlugClassName}}', '{{modelClassWithNamespace}}', '{{modelName}}', '{{namespace}}'], |
|
346
|
|
|
[$modelSlugClassName, $modelClassName, Str::snake($modelName), $this->namespace('models', 'Models\Slugs')], |
|
347
|
|
|
$this->files->get(__DIR__ . '/stubs/model_slug.stub') |
|
348
|
|
|
); |
|
349
|
|
|
|
|
350
|
|
|
twill_put_stub(twill_path("{$baseDir}/" . $modelSlugClassName . '.php'), $stub); |
|
351
|
|
|
} |
|
352
|
|
|
|
|
353
|
|
|
if ($this->revisionable) { |
|
354
|
|
|
$this->makeTwillDirectory($baseDir = $this->isCapsule ? $modelsDir : "{$modelsDir}/Revisions"); |
|
355
|
1 |
|
|
|
356
|
|
|
$modelRevisionClassName = $modelName . 'Revision'; |
|
357
|
1 |
|
|
|
358
|
|
|
$stub = str_replace( |
|
359
|
1 |
|
['{{modelRevisionClassName}}', '{{modelClassWithNamespace}}', '{{modelName}}', '{{namespace}}'], |
|
360
|
|
|
[$modelRevisionClassName, $modelClassName, Str::snake($modelName), $this->namespace('models', 'Models\Revisions')], |
|
361
|
1 |
|
$this->files->get(__DIR__ . '/stubs/model_revision.stub') |
|
362
|
|
|
); |
|
363
|
1 |
|
|
|
364
|
1 |
|
twill_put_stub(twill_path("{$baseDir}/" . $modelRevisionClassName . '.php'), $stub); |
|
365
|
1 |
|
} |
|
366
|
|
|
|
|
367
|
|
|
$activeModelTraits = []; |
|
368
|
|
|
|
|
369
|
1 |
|
foreach ($activeTraits as $index => $traitIsActive) { |
|
370
|
|
|
if ($traitIsActive) { |
|
371
|
1 |
|
!isset($this->modelTraits[$index]) ?: $activeModelTraits[] = $this->modelTraits[$index]; |
|
372
|
|
|
} |
|
373
|
1 |
|
} |
|
374
|
|
|
|
|
375
|
1 |
|
$activeModelTraitsString = empty($activeModelTraits) ? '' : 'use ' . rtrim(implode(', ', $activeModelTraits), ', ') . ';'; |
|
376
|
|
|
|
|
377
|
1 |
|
$activeModelTraitsImports = empty($activeModelTraits) ? '' : "use A17\Twill\Models\Behaviors\\" . implode(";\nuse A17\Twill\Models\Behaviors\\", $activeModelTraits) . ";"; |
|
378
|
1 |
|
|
|
379
|
|
|
$activeModelImplements = $this->sortable ? 'implements Sortable' : ''; |
|
380
|
|
|
|
|
381
|
|
|
if ($this->sortable) { |
|
382
|
|
|
$activeModelTraitsImports .= "\nuse A17\Twill\Models\Behaviors\Sortable;"; |
|
383
|
|
|
} |
|
384
|
|
|
|
|
385
|
|
|
$stub = str_replace([ |
|
386
|
|
|
'{{modelClassName}}', |
|
387
|
|
|
'{{modelTraits}}', |
|
388
|
1 |
|
'{{modelImports}}', |
|
389
|
|
|
'{{modelImplements}}', |
|
390
|
1 |
|
'{{namespace}}', |
|
391
|
|
|
], [ |
|
392
|
1 |
|
$modelName, |
|
393
|
|
|
$activeModelTraitsString, |
|
394
|
1 |
|
$activeModelTraitsImports, |
|
395
|
1 |
|
$activeModelImplements, |
|
396
|
1 |
|
$this->namespace('models', 'Models') |
|
397
|
1 |
|
], $this->files->get(__DIR__ . '/stubs/model.stub')); |
|
398
|
|
|
|
|
399
|
|
|
$stub = $this->renderStubForOption($stub, 'hasTranslation', $this->translatable); |
|
400
|
1 |
|
$stub = $this->renderStubForOption($stub, 'hasSlug', $this->sluggable); |
|
401
|
|
|
$stub = $this->renderStubForOption($stub, 'hasMedias', $this->mediable); |
|
402
|
1 |
|
$stub = $this->renderStubForOption($stub, 'hasPosition', $this->sortable); |
|
403
|
1 |
|
|
|
404
|
|
|
twill_put_stub(twill_path("{$modelsDir}/" . $modelName . '.php'), $stub); |
|
405
|
|
|
|
|
406
|
|
|
$this->info("Models created successfully! Fill your fillables!"); |
|
407
|
|
|
} |
|
408
|
|
|
|
|
409
|
|
|
private function renderStubForOption($stub, $option, $enabled) |
|
410
|
|
|
{ |
|
411
|
|
|
if ($enabled) { |
|
412
|
1 |
|
$stub = str_replace([ |
|
413
|
|
|
'{{' . $option . '}}', |
|
414
|
1 |
|
'{{/' . $option . '}}', |
|
415
|
|
|
], '', $stub); |
|
416
|
1 |
|
} else { |
|
417
|
|
|
$stub = preg_replace('/{{' . $option . '}}[\s\S]+?{{\/' . $option . '}}/', '', $stub); |
|
418
|
1 |
|
} |
|
419
|
|
|
|
|
420
|
1 |
|
return $stub; |
|
421
|
|
|
} |
|
422
|
1 |
|
|
|
423
|
1 |
|
/** |
|
424
|
|
|
* Creates new repository class file for the given model name. |
|
425
|
|
|
* |
|
426
|
|
|
* @param string $modelName |
|
427
|
|
|
* @param array $activeTraits |
|
428
|
|
|
* @return void |
|
429
|
|
|
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException |
|
430
|
|
|
*/ |
|
431
|
|
|
private function createRepository($modelName = 'Item', $activeTraits = []) |
|
432
|
1 |
|
{ |
|
433
|
|
|
$modelsDir = $this->isCapsule ? $this->capsule['repositories_dir'] : 'Repositories'; |
|
434
|
1 |
|
|
|
435
|
|
|
$modelClass = $this->isCapsule ? $this->capsule['model'] : "App\Models\\{$this->capsule['singular']}"; |
|
436
|
1 |
|
|
|
437
|
|
|
$this->makeTwillDirectory($modelsDir); |
|
438
|
1 |
|
|
|
439
|
|
|
$repositoryClassName = $modelName . 'Repository'; |
|
440
|
1 |
|
|
|
441
|
|
|
$activeRepositoryTraits = []; |
|
442
|
1 |
|
|
|
443
|
1 |
|
foreach ($activeTraits as $index => $traitIsActive) { |
|
444
|
|
|
if ($traitIsActive) { |
|
445
|
1 |
|
!isset($this->repositoryTraits[$index]) ?: $activeRepositoryTraits[] = $this->repositoryTraits[$index]; |
|
446
|
|
|
} |
|
447
|
1 |
|
} |
|
448
|
1 |
|
|
|
449
|
|
|
$activeRepositoryTraitsString = empty($activeRepositoryTraits) ? '' : 'use ' . (empty($activeRepositoryTraits) ? "" : rtrim(implode(', ', $activeRepositoryTraits), ', ') . ';'); |
|
450
|
|
|
|
|
451
|
|
|
$activeRepositoryTraitsImports = empty($activeRepositoryTraits) ? '' : "use A17\Twill\Repositories\Behaviors\\" . implode(";\nuse A17\Twill\Repositories\Behaviors\\", $activeRepositoryTraits) . ";"; |
|
452
|
|
|
|
|
453
|
|
|
$stub = str_replace( |
|
454
|
|
|
['{{repositoryClassName}}', '{{modelName}}', '{{repositoryTraits}}', '{{repositoryImports}}', '{{namespace}}', '{{modelClass}}'], |
|
455
|
|
|
[$repositoryClassName, $modelName, $activeRepositoryTraitsString, $activeRepositoryTraitsImports,$this->namespace('repositories', 'Repositories'), $modelClass], |
|
456
|
|
|
$this->files->get(__DIR__ . '/stubs/repository.stub') |
|
457
|
|
|
); |
|
458
|
|
|
|
|
459
|
|
|
twill_put_stub(twill_path("{$modelsDir}/" . $repositoryClassName . '.php'), $stub); |
|
460
|
|
|
|
|
461
|
|
|
$this->info("Repository created successfully! Control all the things!"); |
|
462
|
|
|
} |
|
463
|
|
|
|
|
464
|
|
|
/** |
|
465
|
|
|
* Create a new controller class file for the given module name and model name. |
|
466
|
|
|
* |
|
467
|
|
|
* @param string $moduleName |
|
468
|
|
|
* @param string $modelName |
|
469
|
|
|
* @return void |
|
470
|
|
|
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException |
|
471
|
|
|
*/ |
|
472
|
|
|
private function createController($moduleName = 'items', $modelName = 'Item') |
|
473
|
|
|
{ |
|
474
|
|
|
$controllerClassName = $modelName . 'Controller'; |
|
475
|
|
|
|
|
476
|
|
|
$dir = $this->isCapsule ? $this->capsule['controllers_dir'] : 'Http/Controllers/Admin'; |
|
477
|
|
|
|
|
478
|
|
|
$this->makeTwillDirectory($dir); |
|
479
|
|
|
|
|
480
|
|
|
$stub = str_replace( |
|
481
|
|
|
['{{moduleName}}', '{{controllerClassName}}', '{{namespace}}'], |
|
482
|
|
|
[$moduleName, $controllerClassName,$this->namespace('controllers', 'Http\Controllers\Admin')], |
|
483
|
|
|
$this->files->get(__DIR__ . '/stubs/controller.stub') |
|
484
|
|
|
); |
|
485
|
|
|
|
|
486
|
|
|
twill_put_stub(twill_path("{$dir}/" . $controllerClassName . '.php'), $stub); |
|
487
|
|
|
|
|
488
|
|
|
$this->info("Controller created successfully! Define your index/browser/form endpoints options!"); |
|
489
|
|
|
} |
|
490
|
|
|
|
|
491
|
|
|
/** |
|
492
|
|
|
* Creates a new request class file for the given model name. |
|
493
|
|
|
* |
|
494
|
|
|
* @param string $modelName |
|
495
|
|
|
* @return void |
|
496
|
|
|
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException |
|
497
|
|
|
*/ |
|
498
|
|
|
private function createRequest($modelName = 'Item') |
|
499
|
|
|
{ |
|
500
|
|
|
$dir = $this->isCapsule ? $this->capsule['requests_dir'] : 'Http/Requests/Admin'; |
|
501
|
|
|
|
|
502
|
|
|
$this->makeTwillDirectory($dir); |
|
503
|
|
|
|
|
504
|
|
|
$requestClassName = $modelName . 'Request'; |
|
505
|
|
|
|
|
506
|
|
|
$stub = str_replace( |
|
507
|
|
|
['{{requestClassName}}','{{namespace}}'], |
|
508
|
|
|
[$requestClassName,$this->namespace('requests', 'Http\Requests\Admin')], |
|
509
|
|
|
$this->files->get(__DIR__ . '/stubs/request.stub') |
|
510
|
|
|
); |
|
511
|
|
|
|
|
512
|
|
|
twill_put_stub(twill_path("{$dir}/" . $requestClassName . '.php'), $stub); |
|
513
|
|
|
|
|
514
|
|
|
$this->info("Form request created successfully! Add some validation rules!"); |
|
515
|
|
|
} |
|
516
|
|
|
|
|
517
|
|
|
/** |
|
518
|
|
|
* Creates appropriate module Blade view files. |
|
519
|
|
|
* |
|
520
|
|
|
* @param string $moduleName |
|
521
|
|
|
* @return void |
|
522
|
|
|
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException |
|
523
|
|
|
*/ |
|
524
|
|
|
private function createViews($moduleName = 'items') |
|
525
|
|
|
{ |
|
526
|
|
|
$viewsPath = $this->viewPath($moduleName); |
|
527
|
|
|
|
|
528
|
|
|
$this->makeTwillDirectory($viewsPath); |
|
529
|
|
|
|
|
530
|
|
|
$formView = $this->translatable ? 'form_translatable' : 'form'; |
|
531
|
|
|
|
|
532
|
|
|
twill_put_stub($viewsPath . '/form.blade.php', $this->files->get(__DIR__ . '/stubs/' . $formView . '.blade.stub')); |
|
533
|
|
|
|
|
534
|
|
|
$this->info("Form view created successfully! Include your form fields using @formField directives!"); |
|
535
|
|
|
} |
|
536
|
|
|
|
|
537
|
|
|
/** |
|
538
|
|
|
* Creates a basic routes file for the Capsule. |
|
539
|
|
|
* |
|
540
|
|
|
* @param string $moduleName |
|
541
|
|
|
* @return void |
|
542
|
|
|
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException |
|
543
|
|
|
*/ |
|
544
|
|
|
public function createRoutes() |
|
545
|
|
|
{ |
|
546
|
|
|
$this->makeDir($this->capsule['routes_file']); |
|
547
|
|
|
|
|
548
|
|
|
$contents = str_replace( |
|
549
|
|
|
'{{moduleName}}', |
|
550
|
|
|
$this->capsule['module'], |
|
551
|
|
|
$this->files->get(__DIR__ . '/stubs/routes_admin.stub') |
|
552
|
|
|
); |
|
553
|
|
|
|
|
554
|
|
|
twill_put_stub($this->capsule['routes_file'], $contents); |
|
555
|
|
|
|
|
556
|
|
|
$this->info("Routes file created successfully!"); |
|
557
|
|
|
} |
|
558
|
|
|
|
|
559
|
|
|
private function checkOption($option) |
|
560
|
|
|
{ |
|
561
|
|
|
if ($this->option($option) || $this->option('all')) { |
|
562
|
|
|
return true; |
|
563
|
|
|
} |
|
564
|
|
|
|
|
565
|
|
|
$questions = [ |
|
566
|
|
|
'hasBlocks' => 'Do you need to use the block editor on this module?', |
|
567
|
|
|
'hasTranslation' => 'Do you need to translate content on this module?', |
|
568
|
|
|
'hasSlug' => 'Do you need to generate slugs on this module?', |
|
569
|
|
|
'hasMedias' => 'Do you need to attach images on this module?', |
|
570
|
|
|
'hasFiles' => 'Do you need to attach files on this module?', |
|
571
|
|
|
'hasPosition' => 'Do you need to manage the position of records on this module?', |
|
572
|
|
|
'hasRevisions' => 'Do you need to enable revisions on this module?', |
|
573
|
|
|
]; |
|
574
|
|
|
|
|
575
|
|
|
return 'yes' === $this->choice($questions[$option], ['no', 'yes'], $this->defaultsAnswserToNo ? 0 : 1); |
|
576
|
|
|
} |
|
577
|
|
|
|
|
578
|
|
|
public function createCapsulePath($moduleName, $modelName) |
|
579
|
|
|
{ |
|
580
|
|
|
if (!$this->isCapsule) { |
|
581
|
|
|
$this->moduleBasePath = base_path(); |
|
582
|
|
|
|
|
583
|
|
|
return; |
|
584
|
|
|
} |
|
585
|
|
|
|
|
586
|
|
|
$this->checkCapsuleDirectory( |
|
587
|
|
|
$this->moduleBasePath = config('twill.capsules.path')."/{$moduleName}" |
|
588
|
|
|
); |
|
589
|
|
|
|
|
590
|
|
|
$this->makeDir($this->moduleBasePath); |
|
591
|
|
|
} |
|
592
|
|
|
|
|
593
|
|
|
public function createCapsuleNamespace($module, $model) |
|
594
|
|
|
{ |
|
595
|
|
|
$base = config('twill.capsules.namespace'); |
|
596
|
|
|
|
|
597
|
|
|
$this->capsuleNamespace = "{$base}\\{$module}"; |
|
|
|
|
|
|
598
|
|
|
} |
|
599
|
|
|
|
|
600
|
|
|
public function databasePath($path = '') |
|
601
|
|
|
{ |
|
602
|
|
|
if (!$this->isCapsule) { |
|
603
|
|
|
return database_path($path); |
|
604
|
|
|
} |
|
605
|
|
|
|
|
606
|
|
|
return "{$this->moduleBasePath}/database" . (filled($path) ? "/{$path}" : ''); |
|
607
|
|
|
} |
|
608
|
|
|
|
|
609
|
|
|
public function makeDir($dir) |
|
610
|
|
|
{ |
|
611
|
|
|
$info = pathinfo($dir); |
|
612
|
|
|
|
|
613
|
|
|
$dir = isset($info['extension']) ? $info['dirname'] : $dir; |
|
614
|
|
|
|
|
615
|
|
|
if (!is_dir($dir)) |
|
616
|
|
|
{ |
|
617
|
|
|
mkdir($dir, 0755, true); |
|
618
|
|
|
} |
|
619
|
|
|
|
|
620
|
|
|
if (!is_dir($dir)) { |
|
621
|
|
|
$this->info("It wasn't possible to create capsule directory {$dir}"); |
|
622
|
|
|
|
|
623
|
|
|
die; |
|
|
|
|
|
|
624
|
|
|
} |
|
625
|
|
|
} |
|
626
|
|
|
|
|
627
|
|
|
public function makeTwillDirectory($path) |
|
628
|
|
|
{ |
|
629
|
|
|
make_twill_directory($path); |
|
630
|
|
|
} |
|
631
|
|
|
|
|
632
|
|
|
public function namespace($type, $suffix, $class = null) |
|
633
|
|
|
{ |
|
634
|
|
|
$class = (filled($class) ? "\\$class" : ''); |
|
635
|
|
|
|
|
636
|
|
|
if (!$this->isCapsule) { |
|
637
|
|
|
return "App\\{$suffix}{$class}"; |
|
638
|
|
|
} |
|
639
|
|
|
|
|
640
|
|
|
return $this->capsule[$type] . $class; |
|
641
|
|
|
} |
|
642
|
|
|
|
|
643
|
|
|
public function viewPath($moduleName) |
|
644
|
|
|
{ |
|
645
|
|
|
if (!$this->isCapsule) { |
|
646
|
|
|
return $viewsPath = $this->config->get('view.paths')[0] . '/admin/' . $moduleName; |
|
|
|
|
|
|
647
|
|
|
} |
|
648
|
|
|
|
|
649
|
|
|
$this->makeDir($dir = "{$this->moduleBasePath}/resources/views/admin"); |
|
650
|
|
|
|
|
651
|
|
|
return $dir; |
|
652
|
|
|
} |
|
653
|
|
|
} |
|
654
|
|
|
|
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.