1. Go to this page and download the library: Download webmozart/console 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/ */
webmozart / console example snippets
use Webmozart\Console\Config\DefaultApplicationConfig;
class GitApplicationConfig extends DefaultApplicationConfig
{
protected function configure()
{
parent::configure();
$this
->setName('git')
->setVersion('1.0.0')
// ...
;
}
}
#!/usr/bin/env php
use Webmozart\Console\ConsoleApplication;
if (file_exists($autoload = __DIR__.'/../../../autoload.php')) {
class GitApplicationConfig extends DefaultApplicationConfig
{
protected function configure()
{
$this
// ...
->beginCommand('log')
->setDescription('Show the latest commits')
->setHandler(new LogCommandHandler())
->end()
;
}
}
use Webmozart\Console\Api\Args\Args;
use Webmozart\Console\Api\Command\Command;
use Webmozart\Console\Api\IO\IO;
class LogCommandHandler
{
public function handle(Args $args, IO $io, Command $command)
{
// Simulate the retrieval of the commits
$commits = array(
'commit1',
'commit2',
'commit3',
);
foreach ($commits as $commit) {
$io->writeLine($commit);
}
return 0;
}
}
use Webmozart\Console\Api\Args\Format\Argument;
class GitApplicationConfig extends DefaultApplicationConfig
{
protected function configure()
{
$this
// ...
->beginCommand('log')
// ...
->addArgument('branch', Argument::OPTIONAL, 'The branch to display', 'master')
->end()
;
}
}
class LogCommandHandler
{
public function handle(Args $args, IO $io)
{
$io->writeLine('Branch: '.$args->getArgument('branch'));
$io->writeLine('--');
// ...
}
}
use Webmozart\Console\Api\Args\Format\Option;
class GitApplicationConfig extends DefaultApplicationConfig
{
protected function configure()
{
$this
// ...
->beginCommand('log')
// ...
->addOption('max', null, Option::REQUIRED_VALUE, 'The maximum number of commits', 25)
->end()
;
}
}
class LogCommandHandler
{
public function handle(Args $args, IO $io)
{
// ...
$io->writeLine('Limit: '.$args->getOption('max').' commits');
// ...
}
}
use Webmozart\Console\Api\Args\Format\Option;
class GitApplicationConfig extends DefaultApplicationConfig
{
protected function configure()
{
$this
// ...
->beginCommand('log')
// ...
->addOption('max', 'm', Option::REQUIRED_VALUE, 'The maximum number of commits', 25)
->end()
;
}
}
class LogCommandHandler
{
private $repository;
public function __construct(CommitRepository $repository)
{
$this->repository = $repository;
}
public function handle(Args $args, IO $io)
{
$commits = $this->repository->findByBranch($args->getArgument('branch'));
// ...
}
}
use Webmozart\Console\Api\Formatter\Style;
class GitApplicationConfig extends DefaultApplicationConfig
{
protected function configure()
{
$this
// ...
->addStyle(Style::tag('c3')->fgMagenta())
;
}
}
use Webmozart\Console\UI\Component\Table;
$table = new Table();
$table->setHeaderRow(array('Remote Name', 'Remote URL'));
foreach ($remotes as $remote) {
$table->addRow(array($remote->getName(), $remote->getUrl()));
}
$table->render($io);
use Webmozart\Console\UI\Component\Table;
use Webmozart\Console\UI\Component\TableStyle;
$table = new Table(TableStyle::solidBorder());
use Webmozart\Console\Adapter\ArgsInput;
use Webmozart\Console\Adapter\IOOutput;
$colors = $helper->ask(new ArgsInput($args->getRawArgs(), $args), new IOOutput($io), $question);
use Webmozart\Console\Adapter\ArgsInput;
use Webmozart\Console\Adapter\IOOutput;
trait QuestionTrait
{
protected function ask(Args $args, IO $io, Question $question)
{
// You could cache these instances, but you probably won't need it
$helper = new QuestionHelper();
return $helper->ask(new ArgsInput($args->getRawArgs(), $args), new IOOutput($io), $question);
}
}
class MyCommandHandler
{
use QuestionTrait;
public function handle(Args $args, IO $io)
{
$question = new ChoiceQuestion(
'Please select your favorite colors (defaults to red and blue)',
array('red', 'blue', 'yellow'),
'0,1'
);
$question->setMultiselect(true);
$colors = $this->ask($args, $io, $question);
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.