1. Go to this page and download the library: Download zenstruck/console-extra library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?phprequire_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
zenstruck / console-extra example snippets
#[AsCommand('create:user', 'Creates a user in the database.')]finalclassCreateUserCommandextendsInvokableServiceCommand{
useRunsCommands, RunsProcesses;
publicfunction__invoke(
IO $io,
UserManager $userManager,
#[Argument]
string $email,
#[Argument]
string $password,
#[Option(name: 'role', shortcut: 'r', suggestions: UserMananger::ROLES)]
array $roles,
): void{
$userManager->createUser($email, $password, $roles);
$this->runCommand('another:command');
$this->runProcess('/some/script');
$io->success('Created user.');
}
}
useZenstruck\Console\IO;
$io = new IO($input, $output);
$io->getOption('role'); // InputInterface
$io->writeln('a line'); // OutputInterface
$io->success('Created.'); // StyleInterface// additional methods
$io->input(); // get the "wrapped" input
$io->output(); // get the "wrapped" output
useSymfony\Component\Console\Command\Command;
useSymfony\Component\Console\Input\InputArgument;
useSymfony\Component\Console\Input\InputOption;
useZenstruck\Console\InvokableCommand;
useZenstruck\Console\IO;
classMyCommandextendsInvokableCommand{
// $username/$roles are the argument/option defined belowpublicfunction__invoke(IO $io, string $username, array $roles){
$io->success('created.');
// even if you don't inject IO, it's available as a method:$this->io(); // IO
}
publicfunctionconfigure(): void{
$this
->addArgument('username', InputArgument::REQUIRED)
->addOption('roles', mode: InputOption::VALUE_IS_ARRAY)
;
}
}
useSymfony\Component\Console\Command\Command;
useSymfony\Component\Console\Input\InputArgument;
useZenstruck\Console\Attribute\Argument;
useZenstruck\Console\Attribute\Option;
useZenstruck\Console\InvokableCommand;
#[Argument('arg1', description: 'Argument 1 description', mode: InputArgument::REQUIRED)]#[Argument('arg2', description: 'Argument 1 description')]#[Argument('arg3', suggestions: ['suggestion1', 'suggestion2'])] // for auto-completion#[Argument('arg4', suggestions: 'suggestionsForArg4')] // use a method on the command to get suggestions#[Option('option1', description: 'Option 1 description')]#[Option('option2', suggestions: ['suggestion1', 'suggestion2'])] // for auto-completion#[Option('option3', suggestions: 'suggestionsForOption3')] // use a method on the command to get suggestionsclassMyCommandextendsInvokableCommand{
// ...privatefunctionsuggestionsForArg4(): array{
return ['suggestion3', 'suggestion4'];
}
privatefunctionsuggestionsForOption3(): array{
return ['suggestion3', 'suggestion4'];
}
}
useSymfony\Component\Console\Attribute\AsCommand;
useSymfony\Component\Console\Command\Command;
useZenstruck\Console\Attribute\Argument;
useZenstruck\Console\Attribute\Option;
useZenstruck\Console\InvokableCommand;
#[AsCommand('my:command')]classMyCommandextendsInvokableCommand{
publicfunction__invoke(
#[Argument]
string $username, // defined as a n = false, // defined as a "value-less" option (--super-admin)
#[Option]
?bool $force = null, // defined as a "negatable" option (--force/--no-force)
#[Option]
?string $name = null, // defined as an option that
useZenstruck\Console\CommandRunner;
/** @var \Symfony\Component\Console\Command\Command $command */
CommandRunner::for($command)->run(); // int (the status after running the command)// pass arguments
CommandRunner::for($command, 'arg --opt')->run(); // int