Last active
August 14, 2023 18:46
-
-
Save utsavsomaiya/7fe9a8b3b337966d8df32d67646d6bc6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types=1); | |
namespace App\Console\Commands; | |
use Illuminate\Console\GeneratorCommand; | |
use Illuminate\Support\Str; | |
use Symfony\Component\Console\Attribute\AsCommand; | |
#[AsCommand(name: 'make:enum')] | |
class EnumMakeCommand extends GeneratorCommand | |
{ | |
/** | |
* The name and signature of the console command. | |
* | |
* @var string | |
*/ | |
protected $signature = 'make:enum {name}'; | |
/** | |
* The console command description. | |
* | |
* @var string | |
*/ | |
protected $description = 'Create a new enum'; | |
/** | |
* The type of class being generated. | |
* | |
* @var string | |
*/ | |
protected $type = 'Enum'; | |
/** | |
* Get the stub file for the generator. | |
* | |
* @return string | |
*/ | |
protected function getStub() | |
{ | |
return $this->resolveStubPath('/stubs/enum.stub'); | |
} | |
/** | |
* Resolve the fully-qualified path to the stub. | |
* | |
* @param string $stub | |
* @return string | |
*/ | |
protected function resolveStubPath($stub) | |
{ | |
return file_exists($customPath = $this->laravel->basePath(trim($stub, '/'))) | |
? $customPath | |
: __DIR__.$stub; | |
} | |
/** | |
* Replace the class name for the given stub. | |
* | |
* @param string $name | |
* @return string | |
*/ | |
protected function buildClass($name) | |
{ | |
return str_replace('{{ enum }}', Str::of($name)->classBasename()->value(), parent::buildClass($name)); | |
} | |
/** | |
* Get the default namespace for the class. | |
* | |
* @param string $rootNamespace | |
* @return string | |
*/ | |
protected function getDefaultNamespace($rootNamespace) | |
{ | |
return $rootNamespace.'\Enums'; | |
} | |
/** | |
* Prompt for missing input arguments using the returned questions. | |
* | |
* @return array | |
*/ | |
protected function promptForMissingArgumentsUsing() | |
{ | |
return [ | |
'name' => [ | |
'What should the ' . strtolower($this->type) . ' be named?', | |
'E.g. JsonEnum', | |
fn (string $value) => match (true) { | |
! Str::of($value)->endsWith('Enum') => 'The name must have suffix "Enum".', | |
} | |
], | |
]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment