// trait.stub
<?php
namespace App\Traits;
trait {{ traitName }} {
}
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;
class ArtisanMakeTraitCommand extends Command
{
protected $signature = 'make:trait {name}';
protected $description = 'Create a new trait';
public function handle(): void
{
$name = $this->argument('name');
$name = Str::substr($name, -5) === 'Trait'
? $name
: $name . 'Trait';
$path = app_path('Traits/' . $name . '.php');
if (File::exists($path)) {
$this->error('Trait already exists!');
return;
}
$stub = File::get(base_path('stubs/trait.stub'));
$stub = Str::replace('{{ traitName }}', $name, $stub);
File::put($path, $stub);
$this->info('Trait created successfully.');
}
}