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.
<?php
require_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.')]
final class CreateUserCommand extends InvokableServiceCommand
{
use RunsCommands, RunsProcesses;
public function __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.');
}
}
use Zenstruck\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
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Zenstruck\Console\InvokableCommand;
use Zenstruck\Console\IO;
class MyCommand extends InvokableCommand
{
// $username/$roles are the argument/option defined below
public function __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
}
public function configure(): void
{
$this
->addArgument('username', InputArgument::REQUIRED)
->addOption('roles', mode: InputOption::VALUE_IS_ARRAY)
;
}
}
public function __invoke(IO $io, InputInterface $input, OutputInterface $output)
public function __invoke(IO $io): int
{
return $success ? 0 : 1;
}
use App\Service\UserManager;
use Psr\Log\LoggerInterface;
use Zenstruck\Console\InvokableServiceCommand;
use Zenstruck\Console\IO;
class CreateUserCommand extends InvokableServiceCommand
{
public function __invoke(IO $io, UserManager $userManager, LoggerInterface $logger): void
{
// access container parameters
$environment = $this->parameter('kernel.environment');
// ...
}
}
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\DependencyInjection\Attribute\Target;
use Symfony\Component\DependencyInjection\Attribute\TaggedIterator;
use Zenstruck\Console\InvokableServiceCommand;
class SomeCommand extends InvokableServiceCommand
{
public function __invoke(
#[Autowire('@some.service.id')]
SomeService $service,
#[Autowire('%kernel.environment%')]
string $environment,
#[Target('githubApi')]
HttpClientInterface $httpClient,
#[TaggedIterator('app.handler')]
iterable $handlers,
): void {
// ...
}
}
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Zenstruck\Console\Attribute\Argument;
use Zenstruck\Console\Attribute\Option;
use Zenstruck\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 suggestions
class MyCommand extends InvokableCommand
{
// ...
private function suggestionsForArg4(): array
{
return ['suggestion3', 'suggestion4'];
}
private function suggestionsForOption3(): array
{
return ['suggestion3', 'suggestion4'];
}
}
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Zenstruck\Console\Attribute\Argument;
use Zenstruck\Console\Attribute\Option;
use Zenstruck\Console\InvokableCommand;
#[AsCommand('my:command')]
class MyCommand extends InvokableCommand
{
public function __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
use Zenstruck\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
use Zenstruck\Console\CommandRunner;
/** @var \Symfony\Component\Console\Application $application */
CommandRunner::from($application, 'my:command')->run();
// pass arguments/options
CommandRunner::from($application, 'my:command arg --opt')->run(); // int
use Zenstruck\Console\CommandRunner;
/** @var \Symfony\Component\Console\Application $application */
$output = new \Symfony\Component\Console\Output\BufferedOutput();
CommandRunner::from($application, 'my:command')
->withOutput($output) // any OutputInterface
->run()
;
$output->fetch(); // string (the output)
use Symfony\Component\Console\Command;
use Zenstruck\Console\InvokableCommand;
use Zenstruck\Console\RunsCommands;
class MyCommand extends InvokableCommand
{
use RunsCommands;
public function __invoke(): void
{
$this->runCommand('another:command'); // int (sub-command's run status)
// pass arguments/options
$this->runCommand('another:command arg --opt');
// pass inputs for interactive commands
$this->runCommand('another:command', [
'foo', // input 1
'', // input 2 (<enter>)
'y', // input 3
])
}
}
use Symfony\Component\Console\Command;
use Symfony\Component\Process\Process;
use Zenstruck\Console\InvokableCommand;
use Zenstruck\Console\RunsProcesses;
class MyCommand extends InvokableCommand
{
use RunsProcesses;
public function __invoke(): void
{
$this->runProcess('/some/script');
// construct with array
$this->runProcess(['/some/script', 'arg1', 'arg1']);
// for full control, pass a Process itself
$this->runProcess(
Process::fromShellCommandline('/some/script')
->setTimeout(900)
->setWorkingDirectory('/')
);
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.