[
'mode' => 'argument',
'required' => true,
'description' => 'Command name (e.g., hello:world)',
],
'--force' => [
'mode' => 'flag',
'description' => 'Overwrite if the target file already exists.',
],
];
public function handle(Input $input, Output $output): int
{
$name = (string) ($input->getArgument('name') ?? '');
$force = (bool) $input->getOption('force');
$name = trim($name);
if ($name === '') {
$output->writeln('Command name is required.');
return 1;
}
// Derive PascalCase filename from name, splitting on non-alphanumeric boundaries and colons/underscores/dashes
$parts = preg_split('/[^a-zA-Z0-9]+/', $name) ?: [];
$classStem = '';
foreach ($parts as $p) {
if ($p === '') { continue; }
$classStem .= ucfirst(strtolower($p));
}
if ($classStem === '') {
$output->writeln('Unable to derive a valid filename from the provided name.');
return 1;
}
$root = getcwd();
$dir = $root . '/console/commands';
$file = $dir . '/' . $classStem . '.php';
if (!is_dir($dir)) {
@mkdir($dir, 0777, true);
}
if (file_exists($file) && !$force) {
$output->writeln('Command already exists: console/commands/' . basename($file));
$output->writeln('Use --force to overwrite.');
return 1;
}
$template = <<<'PHP'
writeln('Command __COMMAND__ executed.');
return 0;
}
};
PHP;
$contents = str_replace('__COMMAND__', $name, $template);
@file_put_contents($file, rtrim($contents) . "\n");
$output->writeln('created console/commands/' . basename($file));
return 0;
}
};