|
1
|
|
|
<?php namespace Wn\Generators\Commands; |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
class FactoryCommand extends BaseCommand { |
|
5
|
|
|
|
|
6
|
|
|
protected $signature = 'wn:factory |
|
7
|
|
|
{model : full qualified name of the model.} |
|
8
|
|
|
{--fields= : the fields to generate.} |
|
9
|
|
|
{--file= : the factories file.} |
|
10
|
|
|
{--parsed : tells the command that arguments have been already parsed. To use when calling the command from an other command and passing the parsed arguments and options} |
|
11
|
|
|
{--force= : override the existing files} |
|
12
|
|
|
'; |
|
13
|
|
|
|
|
14
|
|
|
protected $description = 'Generates a model factory'; |
|
15
|
|
|
|
|
16
|
|
|
public function handle() |
|
17
|
|
|
{ |
|
18
|
|
|
$model = $this->argument('model'); |
|
19
|
|
|
|
|
20
|
|
|
$file = $this->getFile(); |
|
21
|
|
|
|
|
22
|
|
|
$content = $this->fs->get($file); |
|
|
|
|
|
|
23
|
|
|
|
|
24
|
|
|
$content .= $this->getTemplate('factory') |
|
25
|
|
|
->with([ |
|
26
|
|
|
'model' => $model, |
|
27
|
|
|
'factory_fields' => $this->getFieldsContent() |
|
28
|
|
|
]) |
|
29
|
|
|
->get(); |
|
30
|
|
|
|
|
31
|
|
|
$this->save($content, $file, "{$model} factory", true); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
protected function getFile() |
|
35
|
|
|
{ |
|
36
|
|
|
$file = $this->option('file'); |
|
37
|
|
|
if(! $file){ |
|
38
|
|
|
$file = './database/factories/ModelFactory.php'; |
|
39
|
|
|
} |
|
40
|
|
|
return $file; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
protected function getFieldsContent() |
|
44
|
|
|
{ |
|
45
|
|
|
$content = []; |
|
46
|
|
|
|
|
47
|
|
|
$fields = $this->option('fields'); |
|
48
|
|
|
|
|
49
|
|
|
if($fields){ |
|
50
|
|
|
if(! $this->option('parsed')){ |
|
51
|
|
|
$fields = $this->getArgumentParser('factory-fields')->parse($fields); |
|
52
|
|
|
} |
|
53
|
|
|
$template = $this->getTemplate('factory/field'); |
|
54
|
|
|
foreach($fields as $field){ |
|
55
|
|
|
$content[] = $template->with($field)->get(); |
|
56
|
|
|
} |
|
57
|
|
|
$content = implode(PHP_EOL, $content); |
|
58
|
|
|
} else { |
|
59
|
|
|
$content = " // Fields here"; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
return $content; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
} |
|
66
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.