1
|
|
|
<?php namespace Wn\Generators\Commands; |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
class MorphTableCommand extends BaseCommand { |
5
|
|
|
|
6
|
|
|
protected $signature = 'wn:morph-table |
7
|
|
|
{model : Name of the persistant model or table} |
8
|
|
|
{morphable : Name of the morphable identifier} |
9
|
|
|
{--add= : specifies additional columns like timestamps, softDeletes, rememberToken and nullableTimestamps.} |
10
|
|
|
{--file= : name of the migration file (to use only for testing purpose).} |
11
|
|
|
{--force= : override the existing files} |
12
|
|
|
'; |
13
|
|
|
|
14
|
|
|
protected $description = 'Generates creation migration for a morphable pivot table'; |
15
|
|
|
|
16
|
|
|
protected $fields; |
17
|
|
|
|
18
|
|
|
public function handle() |
19
|
|
|
{ |
20
|
|
|
$this->parseFields(); |
21
|
|
|
|
22
|
|
|
$this->call('wn:migration', [ |
23
|
|
|
'table' => snake_case(str_plural($this->argument('morphable'))), |
|
|
|
|
24
|
|
|
'--schema' => $this->schema(), |
25
|
|
|
'--keys' => $this->keys(), |
26
|
|
|
'--file' => $this->option('file'), |
27
|
|
|
'--parsed' => false, |
28
|
|
|
'--force' => $this->option('force'), |
29
|
|
|
'--add' => $this->option('add') |
30
|
|
|
]); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
protected function parseFields() |
34
|
|
|
{ |
35
|
|
|
$this->fields = array_map(function($arg, $app) { |
36
|
|
|
return snake_case(str_singular($this->argument($arg))) . "_" . $app; |
|
|
|
|
37
|
|
|
}, ['model', 'morphable', 'morphable'], ['id', 'id', 'type']); |
38
|
|
|
|
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
protected function schema() |
42
|
|
|
{ |
43
|
|
|
return implode(' ', array_map(function($field) { |
44
|
|
|
return $field . ':' . (substr($field, -3) == '_id' ? 'integer:unsigned' : 'string.50') . ':index'; |
45
|
|
|
}, $this->fields)); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
protected function keys() |
49
|
|
|
{ |
50
|
|
|
// return implode(' ', $this->fields); |
51
|
|
|
return snake_case(str_singular($this->argument('model'))) . "_id"; |
|
|
|
|
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
} |
55
|
|
|
|
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.