Skip to content

Commit 165ded4

Browse files
authored
Merge 85907ca into 6316d18
2 parents 6316d18 + 85907ca commit 165ded4

File tree

3 files changed

+106
-0
lines changed

3 files changed

+106
-0
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ composer require biiiiiigmonster/laravel-enum
1919

2020
## Usage
2121

22+
Create enum class
23+
```shell
24+
php artisan make:enum TaskStatus
25+
php artisan make:enum Role
26+
```
27+
2228
Apply the trait on your enum
2329
```php
2430
use BiiiiiigMonster\LaravelEnum\Concerns\EnumTraits;

src/Commands/EnumMakeCommand.php

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

src/Commands/stubs/enum.stub

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace {{ namespace }};
4+
5+
use BiiiiiigMonster\LaravelEnum\Concerns\EnumTraits;
6+
7+
enum {{ class }}
8+
{
9+
use EnumTraits;
10+
}

0 commit comments

Comments
 (0)