1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167:
<?php
namespace Webmozart\Console\UI\Help;
use Webmozart\Console\Api\Application\Application;
use Webmozart\Console\Api\Args\Format\ArgsFormat;
use Webmozart\Console\Api\Args\Format\Argument;
use Webmozart\Console\Api\Command\Command;
use Webmozart\Console\Api\Command\CommandCollection;
use Webmozart\Console\UI\Component\EmptyLine;
use Webmozart\Console\UI\Component\LabeledParagraph;
use Webmozart\Console\UI\Component\NameVersion;
use Webmozart\Console\UI\Component\Paragraph;
use Webmozart\Console\UI\Layout\BlockLayout;
class ApplicationHelp extends AbstractHelp
{
private $application;
public function __construct(Application $application)
{
$this->application = $application;
}
protected function renderHelp(BlockLayout $layout)
{
$help = $this->application->getConfig()->getHelp();
$commands = $this->application->getNamedCommands();
$globalArgsFormat = $this->application->getGlobalArgsFormat();
$argsFormat = ArgsFormat::build()
->addArgument(new Argument('command', Argument::REQUIRED, 'The command to execute'))
->addArgument(new Argument('arg', Argument::MULTI_VALUED, 'The arguments of the command'))
->addOptions($globalArgsFormat->getOptions())
->getFormat();
$this->renderName($layout, $this->application);
$this->renderUsage($layout, $this->application, $argsFormat);
$this->renderArguments($layout, $argsFormat->getArguments());
if ($argsFormat->hasOptions()) {
$this->renderGlobalOptions($layout, $argsFormat->getOptions());
}
if (!$commands->isEmpty()) {
$this->renderCommands($layout, $commands);
}
if ($help) {
$this->renderDescription($layout, $help);
}
}
protected function renderName(BlockLayout $layout, Application $application)
{
$layout->add(new NameVersion($application->getConfig()));
$layout->add(new EmptyLine());
}
protected function renderUsage(BlockLayout $layout, Application $application, ArgsFormat $argsFormat)
{
$appName = $application->getConfig()->getName();
$layout->add(new Paragraph('<b>USAGE</b>'));
$layout->beginBlock();
$this->renderSynopsis($layout, $argsFormat, $appName);
$layout->endBlock();
$layout->add(new EmptyLine());
}
protected function renderCommands(BlockLayout $layout, CommandCollection $commands)
{
$layout->add(new Paragraph('<b>AVAILABLE COMMANDS</b>'));
$layout->beginBlock();
$commands = $commands->toArray();
ksort($commands);
foreach ($commands as $command) {
$this->renderCommand($layout, $command);
}
$layout->endBlock();
$layout->add(new EmptyLine());
}
protected function renderCommand(BlockLayout $layout, Command $command)
{
$description = $command->getConfig()->getDescription();
$name = '<c1>'.$command->getName().'</c1>';
$layout->add(new LabeledParagraph($name, $description));
}
protected function renderDescription(BlockLayout $layout, $help)
{
$layout
->add(new Paragraph('<b>DESCRIPTION</b>'))
->beginBlock()
->add(new Paragraph($help))
->endBlock()
->add(new EmptyLine())
;
}
}