1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BiiiiiigMonster\LaravelEnum\Commands; |
4
|
|
|
|
5
|
|
|
use Illuminate\Console\GeneratorCommand; |
6
|
|
|
use Illuminate\Contracts\Filesystem\FileNotFoundException; |
7
|
|
|
use Symfony\Component\Console\Attribute\AsCommand; |
8
|
|
|
use Symfony\Component\Console\Input\InputOption; |
9
|
|
|
|
10
|
|
|
#[AsCommand(name: 'make:enum')] |
11
|
|
|
class EnumMakeCommand extends GeneratorCommand |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* The console command name. |
15
|
|
|
* |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
protected $name = 'make:enum'; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* The console command description. |
22
|
|
|
* |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
protected $description = 'Create a new custom enum class'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* The type of class being generated. |
29
|
|
|
* |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
protected $type = 'Enum'; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Get the stub file for the generator. |
36
|
|
|
* |
37
|
|
|
* @return string |
38
|
|
|
*/ |
39
|
|
|
protected function getStub() |
40
|
|
|
{ |
41
|
|
|
$stub = $this->option('local') |
42
|
|
|
? '/stubs/enum.local.stub' |
43
|
|
|
: '/stubs/enum.stub'; |
44
|
|
|
|
45
|
|
|
return file_exists($customPath = $this->laravel->basePath(trim($stub, '/'))) |
46
|
|
|
? $customPath |
47
|
|
|
: __DIR__.$stub; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Get the default namespace for the class. |
52
|
|
|
* |
53
|
|
|
* @param string $rootNamespace |
54
|
|
|
* @return string |
55
|
|
|
*/ |
56
|
|
|
protected function getDefaultNamespace($rootNamespace) |
57
|
|
|
{ |
58
|
|
|
return $rootNamespace.'\Enums'; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Build the class with the given name and data type. |
63
|
|
|
* |
64
|
|
|
* @param string $name |
65
|
|
|
* @return string |
66
|
|
|
* |
67
|
|
|
* @throws FileNotFoundException |
68
|
|
|
*/ |
69
|
|
|
protected function buildClass($name) |
70
|
|
|
{ |
71
|
|
|
return str_replace( |
72
|
|
|
['{{ type }}'], |
73
|
|
|
match ($this->option('type')) { |
74
|
|
|
'string' => ': string', |
75
|
|
|
'int', 'integer' => ': int', |
76
|
|
|
default => '' |
77
|
|
|
}, |
78
|
|
|
parent::buildClass($name) |
79
|
|
|
); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Get the console command arguments. |
84
|
|
|
* |
85
|
|
|
* @return array |
86
|
|
|
*/ |
87
|
|
|
protected function getOptions() |
88
|
|
|
{ |
89
|
|
|
return [ |
90
|
|
|
['force', 'f', InputOption::VALUE_NONE, 'Create the class even if the enum already exists'], |
91
|
|
|
['type', 't', InputOption::VALUE_OPTIONAL, 'Indicates that enum data type'], |
92
|
|
|
['local', 'l', InputOption::VALUE_NONE, 'Generate a localizable enum'], |
93
|
|
|
]; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|