33namespace Webfox \LaravelBackedEnums ;
44
55use InvalidArgumentException ;
6- use Illuminate \Console \GeneratorCommand ;
7- use Symfony \Component \Console \Attribute \AsCommand ;
6+ use Illuminate \Foundation \Console \EnumMakeCommand ;
87
98
10- #[AsCommand(name: 'make:laravel-backed-enum ' )]
11- class LaravelBackedEnumMakeCommand extends GeneratorCommand
9+ class LaravelBackedEnumMakeCommand extends EnumMakeCommand
1210{
13- protected $ signature = 'make:laravel-backed-enum {name} {enumType} ' ;
14-
1511 protected $ description = 'Create a new laravel backed enum ' ;
1612
17- protected $ type = 'Enum ' ;
18-
1913 protected function getStub (): string
2014 {
21- return $ this ->resolveStubPath ('/stubs/laravel-backed-enum.stub ' );
15+ if ($ this ->option ('string ' ) || $ this ->option ('int ' )) {
16+ return $ this ->resolveStubPath ('/stubs/laravel-backed-enum.stub ' );
17+ }
18+ return parent ::getStub ();
2219 }
2320
2421 protected function resolveStubPath ($ stub ): string
2522 {
2623 return file_exists ($ customPath = $ this ->laravel ->basePath (trim ($ stub , '/ ' )))
2724 ? $ customPath
28- : __DIR__ . $ stub ;
29- }
30-
31- protected function getDefaultNamespace ($ rootNamespace ): string
32- {
33- return $ rootNamespace . '\Enums ' ;
34- }
35-
36- protected function buildClass ($ name ): array |string
37- {
38- $ replace = [
39- '{{ name }} ' => class_basename ($ name ),
40- '{{ enumType }} ' => $ this ->argument ('enumType ' ),
41- ];
42-
43- return str_replace (
44- array_keys ($ replace ),
45- array_values ($ replace ),
46- parent ::buildClass ($ name )
47- );
48- }
49-
50- protected function promptForMissingArgumentsUsing (): array
51- {
52- return [
53- 'enumType ' => [
54- 'What is the type of the enum? ' ,
55- match (strtolower ($ this ->argument ('enumType ' ))) {
56- 'int ' , 'integer ' => 'int ' ,
57- 'str ' , 'string ' => 'string ' ,
58- default => throw new InvalidArgumentException ('Enum type must be either "int" or "string" ' ),
59- },
60- ],
61- ...parent ::promptForMissingArgumentsUsing (),
62- ];
25+ : __DIR__ . "/../ " . $ stub ;
6326 }
6427
6528 protected function getNameInput (): string
@@ -68,6 +31,11 @@ protected function getNameInput(): string
6831 if (!preg_match ('/^[A-Za-z_\x7f-\xff][A-Za-z0-9_\x7f-\xff]*$/ ' , $ name )) {
6932 throw new InvalidArgumentException ('Invalid enum name format ' );
7033 }
34+
35+ if (str_ends_with ($ name , 'Enum ' )) {
36+ return $ name ;
37+ }
38+
7139 return $ name . 'Enum ' ;
7240 }
7341}
0 commit comments