Skip to content

Commit 299be5a

Browse files
committed
Update to override og make enum command still need to update readme
1 parent bf3487b commit 299be5a

File tree

10 files changed

+131
-73
lines changed

10 files changed

+131
-73
lines changed

.github/workflows/run-tests.yml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,20 @@ name: run-tests
22

33
on:
44
push:
5-
branches: [main]
5+
branches: [ main ]
66
pull_request:
7-
branches: [main]
7+
branches: [ main ]
88

99
jobs:
1010
test:
1111
runs-on: ${{ matrix.os }}
1212
strategy:
1313
fail-fast: true
1414
matrix:
15-
os: [ubuntu-latest]
16-
php: [8.2]
17-
laravel: [11.*]
18-
stability: [prefer-lowest, prefer-stable]
15+
os: [ ubuntu-latest ]
16+
php: [ 8.2 ]
17+
laravel: [ 10.*,11.* ]
18+
stability: [ prefer-lowest, prefer-stable ]
1919

2020
name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.stability }} - ${{ matrix.os }}
2121

composer.json

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,21 @@
1717
],
1818
"require": {
1919
"php": "^8.1",
20-
"spatie/laravel-package-tools": "^1.14.0",
21-
"illuminate/contracts": "^10.0|^11.0"
20+
"spatie/laravel-package-tools": "^1.18",
21+
"illuminate/contracts": "^11.0",
22+
"illuminate/console": "^11.0"
2223
},
2324
"require-dev": {
24-
"laravel/pint": "^1.0",
25-
"nunomaduro/collision": "^6.1",
26-
"larastan/larastan": "^v2.9.2",
27-
"orchestra/testbench": "^8.0",
28-
"pestphp/pest": "^1.21",
29-
"pestphp/pest-plugin-laravel": "^1.1",
30-
"phpstan/extension-installer": "^1.1",
31-
"phpstan/phpstan-deprecation-rules": "^1.0",
32-
"phpstan/phpstan-phpunit": "^1.0",
33-
"phpunit/phpunit": "^9.6"
25+
"laravel/pint": "^1.20",
26+
"nunomaduro/collision": "^8.5",
27+
"larastan/larastan": "^3.0",
28+
"orchestra/testbench": "^9.9",
29+
"pestphp/pest": "^3.7",
30+
"pestphp/pest-plugin-laravel": "^3.0",
31+
"phpstan/extension-installer": "^1.4",
32+
"phpstan/phpstan-deprecation-rules": "^2.0",
33+
"phpstan/phpstan-phpunit": "^2.0",
34+
"phpunit/phpunit": "^11.5"
3435
},
3536
"autoload": {
3637
"psr-4": {
@@ -83,4 +84,4 @@
8384
},
8485
"minimum-stability": "dev",
8586
"prefer-stable": true
86-
}
87+
}

src/LaravelBackedEnumMakeCommand.php

Lines changed: 12 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -3,63 +3,26 @@
33
namespace Webfox\LaravelBackedEnums;
44

55
use 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
}

src/LaravelBackedEnumsServiceProvider.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,12 @@ public function configurePackage(Package $package): void
1818
->name('laravel-backed-enums')
1919
->hasConfigFile();
2020

21-
$package->hasCommands([
22-
LaravelBackedEnumMakeCommand::class,
23-
]);
21+
22+
/** @noinspection PhpFullyQualifiedNameUsageInspection */
23+
if (class_exists(\Illuminate\Foundation\Console\EnumMakeCommand::class)) {
24+
$package->hasConsoleCommand(
25+
LaravelBackedEnumMakeCommand::class,
26+
);
27+
}
2428
}
2529
}

src/stubs/laravel-backed-enum.stub renamed to stubs/laravel-backed-enum.stub

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace {{ namespace }};
55
use Webfox\LaravelBackedEnums\BackedEnum;
66
use Webfox\LaravelBackedEnums\IsBackedEnum;
77

8-
enum {{ name }}: {{ enumType }} implements BackedEnum
8+
enum {{ class }}: {{ type }} implements BackedEnum
99
{
1010
use IsBackedEnum;
1111

@@ -14,5 +14,4 @@ enum {{ name }}: {{ enumType }} implements BackedEnum
1414
* e.g. case Standard = 'standard';
1515
*/
1616

17-
1817
}

tests/Fixtures/ExpectedIntEnum.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace App;
4+
5+
use Webfox\LaravelBackedEnums\BackedEnum;
6+
use Webfox\LaravelBackedEnums\IsBackedEnum;
7+
8+
enum IntEnum: int implements BackedEnum
9+
{
10+
use IsBackedEnum;
11+
12+
/**
13+
* Add your Enums below using.
14+
* e.g. case Standard = 'standard';
15+
*/
16+
17+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace App;
4+
5+
enum PureEnum
6+
{
7+
//
8+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace App;
4+
5+
use Webfox\LaravelBackedEnums\BackedEnum;
6+
use Webfox\LaravelBackedEnums\IsBackedEnum;
7+
8+
enum StringEnum: string implements BackedEnum
9+
{
10+
use IsBackedEnum;
11+
12+
/**
13+
* Add your Enums below using.
14+
* e.g. case Standard = 'standard';
15+
*/
16+
17+
}

tests/MakeEnumCommandTest.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
use Illuminate\Support\Facades\File;
4+
use function Pest\Laravel\artisan;
5+
6+
it('can create an enum', function () {
7+
artisan('make:enum TestEnum -s')
8+
->execute();
9+
expect(base_path('app/TestEnum.php'))->toBeFile();
10+
});
11+
12+
it('can make pure enum', function () {
13+
artisan('make:enum PureEnum')
14+
->execute();
15+
16+
expect(base_path('app/PureEnum.php'))->toBeFile();
17+
18+
$expectedContents = File::get(__DIR__ . '/Fixtures/ExpectedPureEnum.php');
19+
$actualContents = File::get(base_path('app/PureEnum.php'));
20+
21+
expect($actualContents)->toEqual($expectedContents);
22+
});
23+
24+
it('can make string enum', function () {
25+
26+
artisan('make:enum StringEnum --string --force')
27+
->execute();
28+
29+
30+
expect(base_path('app/StringEnum.php'))->toBeFile();
31+
32+
$expectedContents = File::get(__DIR__ . '/Fixtures/ExpectedStringEnum.php');
33+
$actualContents = File::get(base_path('app/StringEnum.php'));
34+
35+
expect($actualContents)->toEqual($expectedContents);
36+
});
37+
38+
it('can make int enum', function () {
39+
40+
artisan('make:enum IntEnum --int --force')
41+
->execute();
42+
43+
expect(base_path('app/IntEnum.php'))->toBeFile();
44+
45+
$expectedContents = File::get(__DIR__ . '/Fixtures/ExpectedIntEnum.php');
46+
$actualContents = File::get(base_path('app/IntEnum.php'));
47+
48+
expect($actualContents)->toEqual($expectedContents);
49+
});

tests/TestCase.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,4 @@ protected function getPackageProviders($app)
1818
LaravelBackedEnumsServiceProvider::class,
1919
];
2020
}
21-
22-
public static function applicationBasePath(): string
23-
{
24-
return __DIR__.'/../workbench';
25-
}
2621
}

0 commit comments

Comments
 (0)