Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 98 additions & 0 deletions src/Illuminate/Foundation/Console/EnumMakeCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php

namespace Illuminate\Foundation\Console;

use Illuminate\Console\Concerns\CreatesMatchingTest;
use Illuminate\Console\GeneratorCommand;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Input\{InputInterface, InputOption};

#[AsCommand(name: 'make:enum')]
class EnumMakeCommand extends GeneratorCommand
{
use CreatesMatchingTest;

/**
* The console command name.
*
* @var string
*/
protected $name = 'make:enum';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Create a new enum class';

/**
* The type of class being generated.
*
* @var string
*/
protected $type = 'Enum';

/**
* Execute the console command.
*
* @return void
*/
public function handle()
{
if (parent::handle() === false && !$this->option('force')) {
return false;
}

if ($this->option('test')) {
$this->createTest();
}
}

/**
* Create a model factory for the model.
*
* @return void
*/
protected function createTest()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Enum will not need tests.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not? I think testing enums are fairly commonplace.

{
$this->call('make:test', [
'name' => $this->qualifyClass($this->getNameInput()) . 'Test',
'--unit' => true,
]);
}

/**
* 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;
}

/**
* Get the default namespace for the class.
*
* @param string $rootNamespace
* @return string
*/
protected function getDefaultNamespace($rootNamespace)
{
return is_dir(app_path('Enums')) ? $rootNamespace . '\\Enums' : $rootNamespace;
}
}
9 changes: 9 additions & 0 deletions src/Illuminate/Foundation/Console/stubs/enum.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace {{ namespace }};

enum {{ class }}: string
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why only a string and not an int?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the int|string suggestion?

{

}

14 changes: 14 additions & 0 deletions src/Illuminate/Foundation/Providers/ArtisanServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
use Illuminate\Foundation\Console\EventGenerateCommand;
use Illuminate\Foundation\Console\EventListCommand;
use Illuminate\Foundation\Console\EventMakeCommand;
use Illuminate\Foundation\Console\EnumMakeCommand;
use Illuminate\Foundation\Console\ExceptionMakeCommand;
use Illuminate\Foundation\Console\JobMakeCommand;
use Illuminate\Foundation\Console\KeyGenerateCommand;
Expand Down Expand Up @@ -125,6 +126,7 @@ class ArtisanServiceProvider extends ServiceProvider implements DeferrableProvid
'Environment' => EnvironmentCommand::class,
'EnvironmentDecrypt' => EnvironmentDecryptCommand::class,
'EnvironmentEncrypt' => EnvironmentEncryptCommand::class,
'EnumMake' => EnumMakeCommand::class,
'EventCache' => EventCacheCommand::class,
'EventClear' => EventClearCommand::class,
'EventList' => EventListCommand::class,
Expand Down Expand Up @@ -394,6 +396,18 @@ protected function registerEventMakeCommand()
});
}

/**
* Register the command.
*
* @return void
*/
protected function registerEnumMakeCommand()
{
$this->app->singleton(EnumMakeCommand::class, function ($app) {
return new EnumMakeCommand($app['files']);
});
}

/**
* Register the command.
*
Expand Down