1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Foundation\Generator\Commands; |
4
|
|
|
|
5
|
|
|
use Foundation\Core\Larapi; |
6
|
|
|
use Foundation\Generator\Abstracts\FileGeneratorCommand; |
7
|
|
|
use Foundation\Generator\Events\FactoryGeneratedEvent; |
8
|
|
|
use Symfony\Component\Console\Input\InputOption; |
9
|
|
|
|
10
|
|
|
class FactoryMakeCommand extends FileGeneratorCommand |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* The console command name. |
14
|
|
|
* |
15
|
|
|
* @var string |
16
|
|
|
*/ |
17
|
|
|
protected $name = 'larapi:make:factory'; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* The console command description. |
21
|
|
|
* |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
protected $description = 'Create a new model factory for the specified module.'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* The name of the generated resource. |
28
|
|
|
* |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
protected $generatorName = 'factory'; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* The stub name. |
35
|
|
|
* |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
protected $stub = 'factory.stub'; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* The file path. |
42
|
|
|
* |
43
|
|
|
* @var string |
44
|
|
|
*/ |
45
|
1 |
|
protected $filePath = '/Database/factories'; |
46
|
|
|
|
47
|
|
|
/** |
48
|
1 |
|
* The event that will fire when the file is created. |
49
|
1 |
|
* |
50
|
|
|
* @var string |
51
|
|
|
*/ |
52
|
1 |
|
protected $event = FactoryGeneratedEvent::class; |
53
|
|
|
|
54
|
1 |
|
protected function getClassName(): string |
55
|
|
|
{ |
56
|
|
|
return $this->getModelName() . 'Factory'; |
57
|
1 |
|
} |
58
|
|
|
|
59
|
|
|
protected function stubOptions(): array |
60
|
1 |
|
{ |
61
|
1 |
|
return [ |
62
|
1 |
|
'CLASS' => $this->getClassName(), |
63
|
|
|
'MODEL' => $this->getModelName(), |
64
|
|
|
'MODEL_NAMESPACE' => $this->getModule()->getNamespace() . '\\' . 'Entities' . '\\' . $this->getModelName(), |
65
|
|
|
]; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Get the console command options. |
70
|
|
|
* |
71
|
87 |
|
* @return array |
72
|
|
|
*/ |
73
|
|
|
protected function setOptions(): array |
74
|
87 |
|
{ |
75
|
|
|
return [ |
76
|
|
|
['model', null, InputOption::VALUE_OPTIONAL, 'The Model name for the factory.', null], |
77
|
|
|
]; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
protected function handleModelOption($shortcut, $type, $question, $default) |
|
|
|
|
81
|
|
|
{ |
82
|
|
|
return $this->anticipate('For what model would you like to generate a factory?', Larapi::getModule($this->getModuleName())->getModels()->getAllPhpFileNamesWithoutExtension(), Larapi::getModule($this->getModuleName())->getModels()->getAllPhpFileNamesWithoutExtension()[0] ?? ""); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
protected function getModelName() :string |
86
|
|
|
{ |
87
|
|
|
return $this->getOption('model'); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
protected function getFileName(): string |
91
|
|
|
{ |
92
|
|
|
return $this->getClassName().'.php'; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.