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