1
|
|
|
<?php namespace Wn\Generators\Commands; |
2
|
|
|
|
3
|
|
|
use Symfony\Component\Yaml\Yaml; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
class ResourcesCommand extends BaseCommand { |
7
|
|
|
|
8
|
|
|
protected $signature = 'wn:resources |
9
|
|
|
{file : Path to the file containing resources declarations} |
10
|
|
|
{--path=app : where to store the model files.} |
11
|
|
|
{--force= : override the existing files} |
12
|
|
|
'; |
13
|
|
|
|
14
|
|
|
protected $description = 'Generates multiple resources from a file'; |
15
|
|
|
|
16
|
|
|
protected $pivotTables = []; |
17
|
|
|
|
18
|
|
|
public function handle() |
19
|
|
|
{ |
20
|
|
|
$content = $this->fs->get($this->argument('file')); |
|
|
|
|
21
|
|
|
$content = Yaml::parse($content); |
22
|
|
|
|
23
|
|
|
$modelIndex = 0; |
24
|
|
|
foreach ($content as $model => $i){ |
|
|
|
|
25
|
|
|
$i = $this->getResourceParams($model, $i); |
26
|
|
|
$migrationName = 'Create' . ucwords(str_plural($i['name'])); |
27
|
|
|
$migrationFile = date('Y_m_d_His') . '-' . str_pad($modelIndex , 3, 0, STR_PAD_LEFT) . '_' . snake_case($migrationName) . '_table'; |
28
|
|
|
|
29
|
|
|
|
30
|
|
|
$this->call('wn:resource', [ |
31
|
|
|
'name' => $i['name'], |
32
|
|
|
'fields' => $i['fields'], |
33
|
|
|
'--add' => $i['add'], |
34
|
|
|
'--has-many' => $i['hasMany'], |
35
|
|
|
'--has-one' => $i['hasOne'], |
36
|
|
|
'--belongs-to' => $i['belongsTo'], |
37
|
|
|
'--belongs-to-many' => $i['belongsToMany'], |
38
|
|
|
'--path' => $this->option('path'), |
39
|
|
|
'--force' => $this->option('force'), |
40
|
|
|
'--migration-file' => $migrationFile |
41
|
|
|
]); |
42
|
|
|
$modelIndex++; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
// $this->call('migrate'); // actually needed for pivot seeders ! |
|
|
|
|
46
|
|
|
|
47
|
|
|
$this->pivotTables = array_map( |
48
|
|
|
'unserialize', |
49
|
|
|
array_unique(array_map('serialize', $this->pivotTables)) |
50
|
|
|
); |
51
|
|
|
|
52
|
|
|
foreach ($this->pivotTables as $tables) { |
53
|
|
|
$this->call('wn:pivot-table', [ |
54
|
|
|
'model1' => $tables[0], |
55
|
|
|
'model2' => $tables[1], |
56
|
|
|
'--force' => $this->option('force') |
57
|
|
|
]); |
58
|
|
|
|
59
|
|
|
// $this->call('wn:pivot-seeder', [ |
|
|
|
|
60
|
|
|
// 'model1' => $tables[0], |
|
|
|
|
61
|
|
|
// 'model2' => $tables[1], |
|
|
|
|
62
|
|
|
// '--force' => $this->option('force') |
|
|
|
|
63
|
|
|
// ]); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$this->call('migrate'); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
protected function getResourceParams($modelName, $i) |
70
|
|
|
{ |
71
|
|
|
$i['name'] = snake_case($modelName); |
72
|
|
|
|
73
|
|
|
foreach(['hasMany', 'hasOne', 'add', 'belongsTo', 'belongsToMany'] as $relation){ |
74
|
|
|
if(isset($i[$relation])){ |
75
|
|
|
$i[$relation] = $this->convertArray($i[$relation], ' ', ','); |
76
|
|
|
} else { |
77
|
|
|
$i[$relation] = false; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
if($i['belongsToMany']){ |
82
|
|
|
$relations = $this->getArgumentParser('relations')->parse($i['belongsToMany']); |
83
|
|
|
foreach ($relations as $relation){ |
84
|
|
|
$table = ''; |
|
|
|
|
85
|
|
|
|
86
|
|
|
if(! $relation['model']){ |
87
|
|
|
$table = snake_case($relation['name']); |
88
|
|
|
} else { |
89
|
|
|
$names = array_reverse(explode("\\", $relation['model'])); |
90
|
|
|
$table = snake_case($names[0]); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$tables = [ str_singular($table), $i['name'] ]; |
94
|
|
|
sort($tables); |
95
|
|
|
$this->pivotTables[] = $tables; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$fields = []; |
100
|
|
|
foreach($i['fields'] as $name => $value) { |
|
|
|
|
101
|
|
|
$value['name'] = $name; |
102
|
|
|
$fields[] = $this->serializeField($value); |
103
|
|
|
} |
104
|
|
|
$i['fields'] = implode(' ', $fields); |
105
|
|
|
|
106
|
|
|
return $i; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
protected function serializeField($field) |
110
|
|
|
{ |
111
|
|
|
$name = $field['name']; |
112
|
|
|
$schema = $this->convertArray(str_replace(':', '.', $field['schema']), ' ', ':'); |
113
|
|
|
$rules = (isset($field['rules'])) ? trim($field['rules']) : ''; |
114
|
|
|
// Replace space by comma |
115
|
|
|
$rules = str_replace(' ', ',', $rules); |
116
|
|
|
|
117
|
|
|
$tags = $this->convertArray($field['tags'], ' ', ','); |
118
|
|
|
|
119
|
|
|
$string = "{$name};{$schema};{$rules};{$tags}"; |
120
|
|
|
|
121
|
|
|
if(isset($field['factory']) && !empty($field['factory'])){ |
122
|
|
|
$string .= ';' . $field['factory']; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
return $string; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
protected function convertArray($list, $old, $new) |
129
|
|
|
{ |
130
|
|
|
return implode($new, array_filter(explode($old, $list), function($item){ |
131
|
|
|
return !empty($item); |
132
|
|
|
})); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
} |
136
|
|
|
|
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.