|
1
|
|
|
<?php namespace Wn\Generators\Commands; |
|
2
|
|
|
|
|
3
|
|
|
use InvalidArgumentException; |
|
4
|
|
|
use Symfony\Component\Yaml\Yaml; |
|
5
|
|
|
|
|
6
|
|
|
|
|
7
|
|
|
class ResourcesCommand extends BaseCommand { |
|
8
|
|
|
|
|
9
|
|
|
protected $signature = 'wn:resources |
|
10
|
|
|
{files* : Paths to the files containing resources declarations} |
|
11
|
|
|
{--path= : where to store the model files.} |
|
12
|
|
|
{--routes= : where to store the routes.} |
|
13
|
|
|
{--no-routes : do not add routes.} |
|
14
|
|
|
{--controllers= : where to store the controllers.} |
|
15
|
|
|
{--no-controllers : do not generate controllers.} |
|
16
|
|
|
{--no-migration : do not migrate.} |
|
17
|
|
|
{--check-only : only check supplied files for valide relationships.} |
|
18
|
|
|
{--skip-check : skip validity check before processing.} |
|
19
|
|
|
{--force= : override the existing files} |
|
20
|
|
|
{--force-redefine : Force model redefinition.} |
|
21
|
|
|
{--laravel= : Use Laravel style route definitions} |
|
22
|
|
|
'; |
|
23
|
|
|
|
|
24
|
|
|
protected $description = 'Generates multiple resources from a couple of files'; |
|
25
|
|
|
|
|
26
|
|
|
protected $pivotTables = []; |
|
27
|
|
|
protected $morphTables = []; |
|
28
|
|
|
|
|
29
|
|
|
private $checkedErrors = 0; |
|
30
|
|
|
private $checkErrors = []; |
|
31
|
|
|
private $checkInfo = []; |
|
32
|
|
|
|
|
33
|
|
|
public function handle() |
|
34
|
|
|
{ |
|
35
|
|
|
$files = $this->argument('files'); |
|
36
|
|
|
$nodes = []; |
|
37
|
|
|
foreach ($files as $file) { |
|
|
|
|
|
|
38
|
|
|
$nodes = $this->mergeNodes($nodes, $this->readFile($file), $this->option('force-redefine')); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
$this->line(''); |
|
42
|
|
|
$this->info('Bringing models to order...'); |
|
43
|
|
|
|
|
44
|
|
|
$nodes = $this->sortDependencies($nodes); |
|
45
|
|
|
$pivotTables = $this->uniqueArray($this->getTables($nodes, 'pivotTables')); |
|
46
|
|
|
$morphTables = $this->uniqueArray($this->getTables($nodes, 'morphTables')); |
|
47
|
|
|
|
|
48
|
|
|
if (! $this->option('skip-check')) { |
|
49
|
|
|
$this->info('Checking Relationships...'); |
|
50
|
|
|
$keys = array_keys($nodes); |
|
51
|
|
|
foreach ($nodes as $model => $i) { |
|
52
|
|
|
$this->checkRelations($i['belongsTo'], 'belongsTo', $i['filename'], $i['uniquename'], $keys); |
|
53
|
|
|
// $this->checkRelations($i['hasManyThrough'], 'hasManyThrough', $file, $model); |
|
54
|
|
|
} |
|
55
|
|
|
$this->checkPivotRelations($nodes, $pivotTables, 'pivot'); |
|
56
|
|
|
$this->checkPivotRelations($nodes, $morphTables, 'morph'); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
View Code Duplication |
if ($this->checkedErrors > 0) { |
|
|
|
|
|
|
60
|
|
|
$this->line(''); |
|
61
|
|
|
if ($this->option('check-only')) { |
|
62
|
|
|
$this->info('Checking only, we have found ' . $this->checkedErrors . ' errors.'); |
|
63
|
|
|
} |
|
64
|
|
|
$this->printErrors(); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
$proceed = (! $this->option('check-only') && $this->checkedErrors == 0) || $this->option('skip-check'); |
|
68
|
|
View Code Duplication |
if (! $this->option('check-only') && $this->checkedErrors > 0) { |
|
|
|
|
|
|
69
|
|
|
$this->line(''); |
|
70
|
|
|
$proceed = $this->confirm("We have found " . $this->checkedErrors . " errors. Are you sure you want to continue?"); |
|
71
|
|
|
} |
|
72
|
|
|
if ($proceed) { |
|
73
|
|
|
$this->buildResources($nodes); |
|
74
|
|
|
|
|
75
|
|
|
// if (!$this->option('no-migration')) { |
|
76
|
|
|
// $this->call('migrate'); // actually needed for pivot seeders ! |
|
77
|
|
|
// } |
|
78
|
|
|
|
|
79
|
|
|
$this->line(''); |
|
80
|
|
|
$this->buildTables('Pivot-Table', 'wn:pivot-table', 'model1', 'model2', $pivotTables); |
|
81
|
|
|
|
|
82
|
|
|
$this->line(''); |
|
83
|
|
|
$this->buildTables('Morph-Table', 'wn:morph-table', 'model', 'morphable', $morphTables); |
|
84
|
|
|
|
|
85
|
|
|
if (!$this->option('no-migration')) { |
|
86
|
|
|
$this->call('migrate'); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
protected function uniqueArray($array) |
|
92
|
|
|
{ |
|
93
|
|
|
return array_map( |
|
94
|
|
|
'unserialize', |
|
95
|
|
|
array_unique(array_map('serialize', $array)) |
|
96
|
|
|
); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
protected function readFile($file) |
|
100
|
|
|
{ |
|
101
|
|
|
$this->info("Reading file ".$file); |
|
102
|
|
|
|
|
103
|
|
|
$content = $this->fs->get($file); |
|
104
|
|
|
$content = Yaml::parse($content); |
|
105
|
|
|
|
|
106
|
|
|
$nodes = []; |
|
107
|
|
|
|
|
108
|
|
|
foreach ($content as $model => $i){ |
|
109
|
|
|
/* |
|
110
|
|
|
$i['modelname'] = as originally in YAML defined |
|
111
|
|
|
$i['name'] = as originally defined in snake_case |
|
112
|
|
|
$i['uniquename']= for key in singular studly_case |
|
113
|
|
|
*/ |
|
114
|
|
|
$i['filename'] = $file; |
|
115
|
|
|
$i['modelname'] = $model; |
|
116
|
|
|
$model = studly_case(str_singular($model)); |
|
117
|
|
|
$i['uniquename'] = $model; |
|
118
|
|
|
|
|
119
|
|
|
$nodes[] = $this->getResourceParams($i); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
return $nodes; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
protected function mergeNodes($nodes, $toMerge, $forceRedefinition = false) { |
|
126
|
|
|
foreach($toMerge as $node) { |
|
127
|
|
|
$nodes = $this->mergeNode($nodes, $node, $forceRedefinition); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
return $nodes; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
protected function mergeNode($nodes, $toMerge, $forceRedefinition = false) { |
|
134
|
|
|
if (empty($nodes[$toMerge['uniquename']]) || $forceRedefinition) { |
|
135
|
|
|
if (!empty($nodes[$toMerge['uniquename']])) { |
|
136
|
|
|
$this->checkError($toMerge['uniquename'] . ": forced to redefine (in file " . $nodes[$toMerge['uniquename']]['filename'] . ", redefined from file ".$file.")"); |
|
|
|
|
|
|
137
|
|
|
} |
|
138
|
|
|
$nodes[$toMerge['uniquename']] = $toMerge; |
|
139
|
|
|
} else { |
|
140
|
|
|
$this->checkError($toMerge['uniquename'] . ": already defined (in file " . $nodes[$toMerge['uniquename']]['filename'] . ", trying to redefine from file ".$file."; Use --force-redefine to force redefinition)"); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
return $nodes; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
protected function getTables($nodes, $key) { |
|
147
|
|
|
$tables = []; |
|
148
|
|
|
foreach($nodes as $node) { |
|
149
|
|
|
if (!empty($node[$key])) { |
|
150
|
|
|
$tables = array_merge($tables, $node[$key]); |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
return $tables; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
protected function buildResources($nodes) |
|
158
|
|
|
{ |
|
159
|
|
|
$modelIndex = 0; |
|
160
|
|
|
$migrationIdLength = strlen((string)count($nodes)); |
|
161
|
|
|
foreach ($nodes as $i) { |
|
162
|
|
|
$migrationName = 'Create' . ucwords(str_plural($i['name'])); |
|
163
|
|
|
$migrationFile = date('Y_m_d_His') . '-' . str_pad($modelIndex , $migrationIdLength, 0, STR_PAD_LEFT) . '_' . snake_case($migrationName) . '_table'; |
|
164
|
|
|
|
|
165
|
|
|
$this->line(''); |
|
166
|
|
|
$this->info('Building Model ' . $i['uniquename']); |
|
167
|
|
|
|
|
168
|
|
|
$options = [ |
|
169
|
|
|
'name' => $i['name'], |
|
170
|
|
|
'fields' => $i['fields'], |
|
171
|
|
|
'--add' => $i['add'], |
|
172
|
|
|
'--has-many' => $i['hasMany'], |
|
173
|
|
|
'--has-one' => $i['hasOne'], |
|
174
|
|
|
'--belongs-to' => $i['belongsTo'], |
|
175
|
|
|
'--belongs-to-many' => $i['belongsToMany'], |
|
176
|
|
|
'--has-many-through' => $i['hasManyThrough'], |
|
177
|
|
|
'--morph-to' => $i['morphTo'], |
|
178
|
|
|
'--morph-many' => $i['morphMany'], |
|
179
|
|
|
'--morph-to-many' => $i['morphToMany'], |
|
180
|
|
|
'--morphed-by-many' => $i['morphedByMany'], |
|
181
|
|
|
'--no-routes' => $this->option('no-routes'), |
|
182
|
|
|
'--no-controller' => $this->option('no-controllers'), |
|
183
|
|
|
'--force' => $this->option('force'), |
|
184
|
|
|
'--migration-file' => $migrationFile, |
|
185
|
|
|
]; |
|
186
|
|
|
if ($this->option('laravel')) { |
|
187
|
|
|
$options['--laravel'] = true; |
|
188
|
|
|
} |
|
189
|
|
|
if ($this->option('routes')) { |
|
190
|
|
|
$options['--routes'] = $this->option('routes'); |
|
191
|
|
|
} |
|
192
|
|
|
if ($this->option('controllers')) { |
|
193
|
|
|
$options['--controller'] = $this->option('controllers'); |
|
194
|
|
|
} |
|
195
|
|
|
if ($this->option('path')) { |
|
196
|
|
|
$options['--path'] = $this->option('path'); |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
$this->call('wn:resource', $options); |
|
200
|
|
|
$modelIndex++; |
|
201
|
|
|
} |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
protected function buildTables($type, $command, $model1, $model2, $tableAssignment) |
|
205
|
|
|
{ |
|
206
|
|
|
foreach ($tableAssignment as $tables) { |
|
207
|
|
|
$this->info('Building '.$type.' ' . $tables[0] . ' - ' . $tables[1]); |
|
208
|
|
|
$this->call($command, [ |
|
209
|
|
|
$model1 => $tables[0], |
|
210
|
|
|
$model2 => $tables[1], |
|
211
|
|
|
'--force' => $this->option('force') |
|
212
|
|
|
]); |
|
213
|
|
|
|
|
214
|
|
|
// $this->call('wn:pivot-seeder', [ |
|
215
|
|
|
// 'model1' => $tables[0], |
|
216
|
|
|
// 'model2' => $tables[1], |
|
217
|
|
|
// '--force' => $this->option('force') |
|
218
|
|
|
// ]); |
|
219
|
|
|
} |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
protected function getResourceParams($i) |
|
223
|
|
|
{ |
|
224
|
|
|
$modelName = $i['modelname']; |
|
225
|
|
|
|
|
226
|
|
|
$i['filename'] = $i['filename']; |
|
227
|
|
|
$i['name'] = snake_case($modelName); |
|
228
|
|
|
$i['modelname'] = $i['modelname']; |
|
229
|
|
|
$i['uniquename'] = $i['uniquename']; |
|
230
|
|
|
|
|
231
|
|
|
foreach(['hasMany', 'hasOne', 'add', 'belongsTo', 'belongsToMany', 'hasManyThrough', 'morphTo', 'morphMany', 'morphToMany', 'morphedByMany'] as $relation){ |
|
232
|
|
|
if(isset($i[$relation])){ |
|
233
|
|
|
$i[$relation] = $this->convertArray($i[$relation], ' ', ','); |
|
234
|
|
|
} else { |
|
235
|
|
|
$i[$relation] = false; |
|
236
|
|
|
} |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
if($i['belongsToMany']){ |
|
240
|
|
|
$i['pivotTables'] = $this->belongsTo($i['name'], $modelName, $i['belongsToMany']); |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
|
|
if($i['morphToMany']){ |
|
244
|
|
|
$i['morphTables'] = $this->morphToMany($modelName, $i['morphToMany']); |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
if($i['morphedByMany']){ |
|
248
|
|
|
$i['morphTables'] = array_merge($i['morphTables'], $this->morphedByMany($i['name'], $modelName, $i['morphedByMany'])); |
|
249
|
|
|
} |
|
250
|
|
|
|
|
251
|
|
|
$fields = []; |
|
252
|
|
|
foreach($i['fields'] as $name => $value) { |
|
253
|
|
|
$value['name'] = $name; |
|
254
|
|
|
$fields[] = $this->serializeField($value); |
|
255
|
|
|
} |
|
256
|
|
|
$i['fields'] = implode(' ', $fields); |
|
257
|
|
|
|
|
258
|
|
|
return $i; |
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
|
View Code Duplication |
protected function belongsTo($name, $modelName, $belongsTo) |
|
|
|
|
|
|
262
|
|
|
{ |
|
263
|
|
|
$parsedRelations = []; |
|
264
|
|
|
$relations = $this->getArgumentParser('relations')->parse($belongsTo); |
|
265
|
|
|
foreach ($relations as $relation){ |
|
266
|
|
|
if(! $relation['model']){ |
|
267
|
|
|
$table = snake_case($relation['name']); |
|
268
|
|
|
} else { |
|
269
|
|
|
$table = snake_case($this->extractClassName($relation['model'])); |
|
270
|
|
|
} |
|
271
|
|
|
|
|
272
|
|
|
$tables = [ str_singular($table), $name ]; |
|
273
|
|
|
sort($tables); |
|
274
|
|
|
$tables[] = $modelName; |
|
275
|
|
|
$parsedRelations[] = $tables; |
|
276
|
|
|
} |
|
277
|
|
|
|
|
278
|
|
|
return $parsedRelations; |
|
279
|
|
|
} |
|
280
|
|
|
|
|
281
|
|
|
protected function morphToMany($modelName, $morphToMany) |
|
|
|
|
|
|
282
|
|
|
{ |
|
283
|
|
|
$parsedRelations = []; |
|
284
|
|
|
$relations = $this->getArgumentParser('relations-morphMany')->parse(); |
|
|
|
|
|
|
285
|
|
|
foreach ($relations as $relation){ |
|
286
|
|
|
if(! $relation['through']){ |
|
287
|
|
|
$morphable = snake_case($this->extractClassName($relation['model'])); |
|
288
|
|
|
$model = snake_case($relation['name']); |
|
289
|
|
|
} else { |
|
290
|
|
|
$morphable = snake_case($this->extractClassName($relation['through'])); |
|
291
|
|
|
$model = snake_case($this->extractClassName($relation['model'])); |
|
292
|
|
|
} |
|
293
|
|
|
|
|
294
|
|
|
$tables = [ str_singular($model), str_singular($morphable), $modelName ]; |
|
295
|
|
|
$parsedRelations[] = $tables; |
|
296
|
|
|
} |
|
297
|
|
|
|
|
298
|
|
|
return $parsedRelations; |
|
299
|
|
|
} |
|
300
|
|
|
|
|
301
|
|
View Code Duplication |
protected function morphedByMany($name, $modelName, $morphedByMany) |
|
|
|
|
|
|
302
|
|
|
{ |
|
303
|
|
|
$parsedRelations = []; |
|
304
|
|
|
$relations = $this->getArgumentParser('relations-morphMany')->parse($morphedByMany); |
|
305
|
|
|
foreach ($relations as $relation){ |
|
306
|
|
|
$table = ''; |
|
|
|
|
|
|
307
|
|
|
|
|
308
|
|
|
if(! $relation['through']){ |
|
309
|
|
|
$morphable = snake_case($this->extractClassName($relation['model'])); |
|
310
|
|
|
} else { |
|
311
|
|
|
$morphable = snake_case($this->extractClassName($relation['through'])); |
|
312
|
|
|
} |
|
313
|
|
|
|
|
314
|
|
|
$tables = [ str_singular($name), str_singular($morphable), $modelName ]; |
|
315
|
|
|
$parsedRelations[] = $tables; |
|
316
|
|
|
} |
|
317
|
|
|
|
|
318
|
|
|
return $parsedRelations; |
|
319
|
|
|
} |
|
320
|
|
|
|
|
321
|
|
|
protected function serializeField($field) |
|
322
|
|
|
{ |
|
323
|
|
|
$name = $field['name']; |
|
324
|
|
|
$schema = $this->convertArray(str_replace(':', '.', $field['schema']), ' ', ':'); |
|
325
|
|
|
$rules = (isset($field['rules'])) ? $this->convertArray(trim($field['rules']), ' ', '|') : ''; |
|
326
|
|
|
$tags = !empty($field['tags']) ? $this->convertArray($field['tags'], ' ', ',') : ''; |
|
327
|
|
|
|
|
328
|
|
|
$string = "{$name};{$schema};{$rules};{$tags}"; |
|
329
|
|
|
|
|
330
|
|
|
if(isset($field['factory']) && !empty($field['factory'])){ |
|
331
|
|
|
$string .= ';' . $field['factory']; |
|
332
|
|
|
} |
|
333
|
|
|
|
|
334
|
|
|
return $string; |
|
335
|
|
|
} |
|
336
|
|
|
|
|
337
|
|
|
protected function convertArray($list, $old, $new) |
|
338
|
|
|
{ |
|
339
|
|
|
return implode($new, array_filter(explode($old, $list), function($item){ |
|
340
|
|
|
return !empty($item); |
|
341
|
|
|
})); |
|
342
|
|
|
} |
|
343
|
|
|
|
|
344
|
|
|
private function sortDependencies($nodes) { |
|
345
|
|
|
$load_order = array(); |
|
346
|
|
|
$seen = array(); |
|
347
|
|
|
|
|
348
|
|
|
foreach($nodes as $key => $item) { |
|
349
|
|
|
$tmp = $this->getDependencies($nodes, $key, $seen); |
|
350
|
|
|
|
|
351
|
|
|
// if($tmp[2] === false) { |
|
352
|
|
|
$load_order = array_merge($load_order, $tmp[0]); |
|
353
|
|
|
$seen = $tmp[1]; |
|
354
|
|
|
// } |
|
355
|
|
|
} |
|
356
|
|
|
|
|
357
|
|
|
return $load_order; |
|
358
|
|
|
} |
|
359
|
|
|
|
|
360
|
|
|
private function getDependencies($nodes, $key, $seen = array()) { |
|
361
|
|
|
if(array_key_exists($key, $seen) === true) { |
|
362
|
|
|
return array(array(), $seen); |
|
363
|
|
|
} |
|
364
|
|
|
|
|
365
|
|
|
|
|
366
|
|
|
if(!empty($nodes[$key])) { |
|
367
|
|
|
$order = array(); |
|
368
|
|
|
// $failed = array(); |
|
369
|
|
|
|
|
370
|
|
|
if($nodes[$key]['belongsTo']) { |
|
371
|
|
|
$deps = $this->getArgumentParser('relations')->parse($nodes[$key]['belongsTo']); |
|
372
|
|
|
foreach($deps as $dependency) { |
|
373
|
|
|
if(! $dependency['model']){ |
|
374
|
|
|
$dependency['model'] = $dependency['name']; |
|
375
|
|
|
} else if(strpos($dependency['model'], '\\') !== false ){ |
|
376
|
|
|
$dependency['model'] = substr($dependency['model'], strpos($dependency['model'], '\\')+1); // Cut offs first namespace part |
|
377
|
|
|
} |
|
378
|
|
|
$dependency['model'] = studly_case(str_singular($dependency['model'])); |
|
379
|
|
|
if ($dependency['model'] != $key) { |
|
380
|
|
|
$tmp = $this->getDependencies($nodes, $dependency['model'], $seen); |
|
381
|
|
|
|
|
382
|
|
|
$order = array_merge($order, $tmp[0]); |
|
383
|
|
|
$seen = $tmp[1]; |
|
384
|
|
|
} |
|
385
|
|
|
|
|
386
|
|
|
// if($tmp[2] !== false) { |
|
387
|
|
|
// $failed = array_merge($tmp[2], $failed); |
|
388
|
|
|
// } |
|
389
|
|
|
} |
|
390
|
|
|
} |
|
391
|
|
|
$seen[$key] = true; |
|
392
|
|
|
$order[$key] = $nodes[$key]; |
|
393
|
|
|
// $failed = (count($failed) > 0) ? $failed : false; |
|
394
|
|
|
|
|
395
|
|
|
return array($order, $seen);//, $failed |
|
396
|
|
|
} |
|
397
|
|
|
|
|
398
|
|
|
return array(array(), $seen);//, array($item) |
|
399
|
|
|
} |
|
400
|
|
|
|
|
401
|
|
|
protected function checkError($message, $model = "", $file = "") { |
|
402
|
|
|
$this->checkErrors[] = array("message" => $message, "model" => $model, "file" => $file); |
|
403
|
|
|
$this->checkedErrors++; |
|
404
|
|
|
} |
|
405
|
|
|
|
|
406
|
|
|
protected function checkInfo($message, $model = "", $file = "") { |
|
407
|
|
|
$this->checkInfo[] = array("message" => $message, "model" => $model, "file" => $file); |
|
408
|
|
|
} |
|
409
|
|
|
|
|
410
|
|
|
protected function printErrors() { |
|
411
|
|
|
foreach ($this->checkErrors as $error) { |
|
412
|
|
|
$this->error($error['message']); |
|
413
|
|
|
} |
|
414
|
|
|
foreach ($this->checkInfo as $info) { |
|
415
|
|
|
$this->info($info['message']); |
|
416
|
|
|
} |
|
417
|
|
|
} |
|
418
|
|
|
|
|
419
|
|
|
protected function checkRelations($relations, $type, $file, $model, $keys) { |
|
420
|
|
|
if ($relations) { |
|
421
|
|
|
$position = array_search($model, $keys); |
|
422
|
|
|
$relations = $this->getArgumentParser('relations')->parse($relations); |
|
423
|
|
|
foreach($relations as $relation) { |
|
424
|
|
|
switch($type) { |
|
425
|
|
|
case "belongsTo": |
|
426
|
|
|
$rModel = $relation['model'] ? $relation['model'] : $relation['name']; |
|
427
|
|
|
break; |
|
428
|
|
|
default: continue; |
|
429
|
|
|
} |
|
430
|
|
|
|
|
431
|
|
|
$search = array_search(studly_case(str_singular($rModel)), $keys); |
|
|
|
|
|
|
432
|
|
|
if (($search === false || $search > $position) && !class_exists($this->prependNamespace($rModel)) && !class_exists($this->prependNamespace($rModel, 'App'))) { |
|
|
|
|
|
|
433
|
|
|
$this->checkError(studly_case(str_singular($rModel)) . ": undefined (used in " . $type . "-relationship of model " . $model . " in file " . $file . ")"); |
|
434
|
|
|
} else if (class_exists($this->prependNamespace($rModel))) { |
|
435
|
|
|
$this->checkInfo(studly_case(str_singular($rModel)) . ": already defined in Namespace " . $this->getNamespace() . " (used in " . $type . "-relationship of model " . $model . " in file " . $file . ")"); |
|
436
|
|
|
} else if (class_exists($this->prependNamespace($rModel, 'App'))) { |
|
|
|
|
|
|
437
|
|
|
$this->checkInfo(studly_case(str_singular($rModel)) . ": already defined in Namespace App\\ (used in " . $type . "-relationship of model " . $model . " in file " . $file . ")"); |
|
438
|
|
|
} |
|
439
|
|
|
} |
|
440
|
|
|
} |
|
441
|
|
|
} |
|
442
|
|
|
|
|
443
|
|
|
protected function checkPivotRelations($nodes, $relations, $rType) { |
|
444
|
|
|
if ($relations) { |
|
445
|
|
|
foreach($relations as $relation) { |
|
446
|
|
|
$relation['0'] = studly_case(str_singular($relation['0'])); |
|
447
|
|
|
$relation['1'] = studly_case(str_singular($relation['1'])); |
|
448
|
|
|
$relation['2'] = studly_case(str_singular($relation['2'])); |
|
449
|
|
|
|
|
450
|
|
|
if (empty($nodes[$relation['0']]) && !class_exists($this->getNamespace() . '\\' . ucwords(camel_case($relation['0']))) && !class_exists('App\\' . ucwords(camel_case($relation['0'])))) { |
|
451
|
|
|
$this->checkError($relation['0'] . ": undefined (used in " . $rType . "-based relationship of model " . $relation['2'] . " in file " . $nodes[$relation['2']]['filename'] . ")"); |
|
452
|
|
|
} else if (class_exists($this->getNamespace() . '\\' . ucwords(camel_case($relation['0'])))) { |
|
453
|
|
|
$this->checkInfo(studly_case(str_singular($relation['0'])) . ": already defined in Namespace " . $this->getNamespace() . " (used in " . $rType . "-based relationship of model " . $relation['2'] . " in file " . $nodes[$relation['2']]['filename'] . ")"); |
|
454
|
|
View Code Duplication |
} else if (class_exists('App\\' . ucwords(camel_case($relation['0'])))) { |
|
|
|
|
|
|
455
|
|
|
$this->checkInfo(studly_case(str_singular($relation['0'])) . ": already defined in Namespace App\\ (used in " . $rType . "-based relationship of model " . $relation['2'] . " in file " . $nodes[$relation['2']]['filename'] . ")"); |
|
456
|
|
|
} |
|
457
|
|
|
|
|
458
|
|
|
if ($rType == "pivot" && empty($nodes[$relation['1']]) && !class_exists($this->getNamespace() . '\\' . ucwords(camel_case($relation['1']))) && !class_exists('App\\' . ucwords(camel_case($relation['1'])))) { |
|
459
|
|
|
$this->checkError($relation['1'] . ": undefined (used in " . $rType . "-based relationship of model " . $relation['2'] . " in file " . $nodes[$relation['2']]['filename'] . ")"); |
|
460
|
|
|
} else if ($rType == "pivot" && class_exists($this->getNamespace() . '\\' . ucwords(camel_case($relation['1'])))) { |
|
461
|
|
|
$this->checkInfo(studly_case(str_singular($relation['1'])) . ": already defined in Namespace " . $this->getNamespace() . " (used in " . $rType . "-based relationship of model " . $relation['2'] . " in file " . $nodes[$relation['2']]['filename'] . ")"); |
|
462
|
|
View Code Duplication |
} else if ($rType == "pivot" && class_exists('App\\' . ucwords(camel_case($relation['1'])))) { |
|
|
|
|
|
|
463
|
|
|
$this->checkInfo(studly_case(str_singular($relation['1'])) . ": already defined in Namespace App\\ (used in " . $rType . "-based relationship of model " . $relation['2'] . " in file " . $nodes[$relation['2']]['filename'] . ")"); |
|
464
|
|
|
} |
|
465
|
|
|
} |
|
466
|
|
|
} |
|
467
|
|
|
} |
|
468
|
|
|
|
|
469
|
|
|
} |
|
470
|
|
|
|
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.