Skip to content

Commit 1d58927

Browse files
authored
Merge pull request #1 from DeSmart/enum-generation-command
Enum class generation command
2 parents 11e6f62 + af4c8be commit 1d58927

File tree

6 files changed

+171
-1
lines changed

6 files changed

+171
-1
lines changed

CHANGELOG.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
All notable changes to `laravel-enum` will be documented in this file:
44

5+
## 1.1.0 - 2021-03-17
6+
7+
Added:
8+
* added Artisan `make:enum` command for enumeration classes generation
9+
510
## 1.0.0 - 2021-03-08
611

7-
- initial release
12+
Added:
13+
* initial release

README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,34 @@ dump($hero->character);
6464
// -value: "evil"
6565
// }
6666
```
67+
68+
### Enumeration class generation
69+
70+
Package provides `make:enum` Artisan command for enumeration classes auto-generation. To generate new enum class, run:
71+
```php
72+
php artisan make:enum Character --cases='good,evil,sometimes_good_sometimes_evil'
73+
```
74+
> `--cases` (or `-c`) option allows defining available enum cases. Command can be run without that option specified.
75+
76+
Above command will create a new class inside `Enums` directory:
77+
```php
78+
namespace App\Enums;
79+
80+
use DeSmart\Laravel\Enumeration\Enumeration;
81+
82+
/**
83+
* @method static Character good()
84+
* @method static Character evil()
85+
* @method static Character sometimesGoodSometimesEvil()
86+
*/
87+
class Character extends Enumeration
88+
{
89+
const GOOD = 'good';
90+
const EVIL = 'evil';
91+
const SOMETIMES_GOOD_SOMETIMES_EVIL = 'sometimes_good_sometimes_evil';
92+
}
93+
```
94+
6795
## Changelog
6896

6997
Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.

composer.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@
3737
"preferred-install": "dist",
3838
"sort-packages": true
3939
},
40+
"extra": {
41+
"laravel": {
42+
"providers": [
43+
"DeSmart\\Laravel\\Enumeration\\EnumerationServiceProvider"
44+
]
45+
}
46+
},
4047
"minimum-stability": "dev",
4148
"prefer-stable": true
4249
}

src/Console/MakeEnumCommand.php

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace DeSmart\Laravel\Enumeration\Console;
6+
7+
use Illuminate\Console\GeneratorCommand;
8+
use Illuminate\Support\Str;
9+
use Symfony\Component\Console\Input\InputOption;
10+
use function DeSmart\Enum\Helpers\toConstName;
11+
12+
class MakeEnumCommand extends GeneratorCommand
13+
{
14+
/**
15+
* @var string
16+
*/
17+
protected $name = 'make:enum';
18+
19+
/**
20+
* @var string
21+
*/
22+
protected $description = 'Create a new Enumeration class';
23+
24+
/**
25+
* @var string
26+
*/
27+
protected $type = 'Enum';
28+
29+
/**
30+
* @param string $stub
31+
* @param string $name
32+
* @return string
33+
*/
34+
protected function replaceClass($stub, $name): string
35+
{
36+
$class = str_replace($this->getNamespace($name) . '\\', '', $name);
37+
38+
$staticMethods = "\n";
39+
$constants = '';
40+
41+
if ($this->option('cases')) {
42+
$cases = array_map('trim', explode(',', $this->option('cases')));
43+
44+
$staticMethods .= $this->generateStaticMethods($cases, $class);
45+
$constants .= $this->generateConstants($cases);
46+
}
47+
48+
$constants .= '}';
49+
50+
return str_replace(
51+
['{{ staticMethods }}', '{{ constants }}', '{{ class }}'],
52+
[$staticMethods, $constants, $class],
53+
$stub
54+
);
55+
}
56+
57+
protected function generateStaticMethods(array $cases, string $class): string
58+
{
59+
$staticMethods = "/**\n";
60+
61+
foreach ($cases as $case) {
62+
$method = Str::camel(Str::lower($case));
63+
$staticMethods .= " * @method static $class $method()\n";
64+
}
65+
66+
$staticMethods .= " */\n";
67+
68+
return $staticMethods;
69+
}
70+
71+
protected function generateConstants(array $cases): string
72+
{
73+
$constants = '';
74+
75+
foreach ($cases as $case) {
76+
$constant = toConstName($case);
77+
$constants .= "\tconst $constant = '$case';\n";
78+
}
79+
80+
return $constants;
81+
}
82+
83+
protected function getStub(): string
84+
{
85+
return __DIR__ . '/stubs/enum.stub';
86+
}
87+
88+
/**
89+
* @param string $rootNamespace
90+
* @return string
91+
*/
92+
protected function getDefaultNamespace($rootNamespace): string
93+
{
94+
return $rootNamespace . '\\Enums';
95+
}
96+
97+
protected function getOptions(): array
98+
{
99+
return [
100+
['cases', '-c', InputOption::VALUE_OPTIONAL, 'Set enum cases'],
101+
];
102+
}
103+
}

src/Console/stubs/enum.stub

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace {{ namespace }};
4+
5+
use DeSmart\Laravel\Enumeration\Enumeration;
6+
{{ staticMethods }}class {{ class }} extends Enumeration
7+
{
8+
{{ constants }}

src/EnumerationServiceProvider.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace DeSmart\Laravel\Enumeration;
6+
7+
use DeSmart\Laravel\Enumeration\Console\MakeEnumCommand;
8+
use Illuminate\Support\ServiceProvider;
9+
10+
class EnumerationServiceProvider extends ServiceProvider
11+
{
12+
public function boot(): void
13+
{
14+
if ($this->app->runningInConsole()) {
15+
$this->commands(MakeEnumCommand::class);
16+
}
17+
}
18+
}

0 commit comments

Comments
 (0)