1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BeyondCode\LaravelPackageTools\Commands; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Str; |
6
|
|
|
use Symfony\Component\Console\Input\ArrayInput; |
7
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
8
|
|
|
use Symfony\Component\Console\Input\InputDefinition; |
9
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
10
|
|
|
use Symfony\Component\Console\Input\InputOption; |
11
|
|
|
use Symfony\Component\Console\Output\BufferedOutput; |
12
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
13
|
|
|
|
14
|
|
|
class MakeModel extends GeneratorCommand |
15
|
|
|
{ |
16
|
|
|
protected $type = 'Model'; |
17
|
|
|
|
18
|
|
|
public function __invoke(InputInterface $input, OutputInterface $output) |
19
|
|
|
{ |
20
|
|
|
parent::__invoke($input, $output); |
21
|
|
|
|
22
|
|
|
if($this->input->getOption('migration')) { |
23
|
|
|
$this->createMigration(); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
if($this->input->getOption('factory')) { |
27
|
|
|
$this->createFactory(); |
28
|
|
|
} |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
protected function getStub() |
32
|
|
|
{ |
33
|
|
|
return __DIR__.'/stubs/model.stub'; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
protected function getDefaultNamespace($rootNamespace): string |
37
|
|
|
{ |
38
|
|
|
return $rootNamespace.'\Models'; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
private function createMigration() |
42
|
|
|
{ |
43
|
|
|
$table = Str::snake(Str::plural(class_basename($this->getNameInput()))); |
44
|
|
|
|
45
|
|
|
$this->runCommand("create_{$table}_table", ['--create' => "{$table}"], new MakeMigration); |
46
|
|
|
|
47
|
|
|
$this->info(PHP_EOL . "Created migration: create_{$table}_table"); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
protected function createFactory() |
51
|
|
|
{ |
52
|
|
|
$model = $this->getNameInput(); |
53
|
|
|
|
54
|
|
|
$this->runCommand("{$model}Factory", ['--model' => "{$model}"], new MakeFactory); |
55
|
|
|
|
56
|
|
|
$this->info(PHP_EOL . "Factory created successfully."); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
protected function runCommand($name, $options, GeneratorCommand $command) |
60
|
|
|
{ |
61
|
|
|
$input = new ArrayInput(array_merge([ |
62
|
|
|
'name' => $name, |
63
|
|
|
'--force' => false, |
64
|
|
|
], $options), |
65
|
|
|
new InputDefinition([ |
66
|
|
|
new InputArgument('name'), |
67
|
|
|
new InputOption('create'), |
68
|
|
|
new InputOption('model'), |
69
|
|
|
new InputOption('force'), |
70
|
|
|
])); |
71
|
|
|
|
72
|
|
|
$output = new BufferedOutput(); |
73
|
|
|
|
74
|
|
|
$command->outputPath = ($this->outputPath); |
75
|
|
|
|
76
|
|
|
$command->__invoke($input, $output); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|