1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\Enum\Laravel\Commands; |
4
|
|
|
|
5
|
|
|
use Illuminate\Console\GeneratorCommand; |
6
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
7
|
|
|
use Symfony\Component\Console\Input\InputOption; |
8
|
|
|
|
9
|
|
|
class MakeEnum extends GeneratorCommand |
10
|
|
|
{ |
11
|
|
|
protected $name = 'make:enum'; |
12
|
|
|
|
13
|
|
|
protected $description = 'Create a new enum'; |
14
|
|
|
|
15
|
36 |
|
public function handle() |
16
|
|
|
{ |
17
|
36 |
|
$this->type = $this->getNameInput(); |
18
|
|
|
|
19
|
|
|
return parent::handle(); |
20
|
36 |
|
} |
21
|
|
|
|
22
|
36 |
|
protected function getStub() |
23
|
|
|
{ |
24
|
|
|
return __DIR__.'/../../stubs/enum.php.stub'; |
25
|
36 |
|
} |
26
|
|
|
|
27
|
36 |
|
protected function getDefaultNamespace($rootNamespace) |
28
|
|
|
{ |
29
|
36 |
|
return $rootNamespace.'\Enums'; |
30
|
36 |
|
} |
31
|
|
|
|
32
|
36 |
|
protected function replaceClass($stub, $name) |
33
|
|
|
{ |
34
|
|
|
$stub = parent::replaceClass($stub, $name); |
35
|
36 |
|
|
36
|
|
|
$stub = str_replace('/** DummyDocBlock */', $this->getDocBlock(), $stub); |
37
|
36 |
|
|
38
|
|
|
return $stub; |
39
|
36 |
|
} |
40
|
32 |
|
|
41
|
|
|
protected function getDocBlock(): string |
42
|
32 |
|
{ |
43
|
32 |
|
$methods = $this->option('method'); |
44
|
32 |
|
|
45
|
|
|
if (! empty($methods)) { |
46
|
|
|
$docBlock = PHP_EOL.'/**'; |
47
|
36 |
|
$docBlock .= implode('', array_map(function ($method) { |
48
|
|
|
return PHP_EOL.' * @method static self '.$method.'()'; |
49
|
|
|
}, $methods)); |
|
|
|
|
50
|
36 |
|
$docBlock .= PHP_EOL.' */'; |
51
|
|
|
} |
52
|
36 |
|
|
53
|
|
|
return $docBlock ?? ''; |
54
|
36 |
|
} |
55
|
16 |
|
|
56
|
16 |
|
protected function getArguments() |
57
|
|
|
{ |
58
|
16 |
|
return [ |
59
|
16 |
|
['name', InputArgument::REQUIRED, 'The name of the enum'], |
60
|
|
|
]; |
61
|
|
|
} |
62
|
16 |
|
|
63
|
|
|
protected function getOptions() |
64
|
|
|
{ |
65
|
36 |
|
return [ |
66
|
|
|
['method', 'm', InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'The method name that should be added to the enum'], |
67
|
|
|
]; |
68
|
32 |
|
} |
69
|
|
|
} |
70
|
|
|
|