/** * 注册一个helper * * @param string $name * @param HelperInterface $helper */ function registerHelper($name, HelperInterface $helper) { $this->console->getHelperRegistry()->register($name, $helper); }
/** * 创建helper对象 * * @param string $className * @return HelperInterface */ protected function create($className) { return new $className($this->console->getIo()); }
<?php use App\AppKernel; use Slince\Console\Console; include __DIR__ . '/../config/bootstrap.php'; $kernel = new AppKernel(); $console = new Console(); $console->addCommands([new \Slince\Application\Command\ClearCommand($kernel)]); $console->run();
use Slince\Console\Command; use Slince\Console\Console; use Slince\Console\Context\Io; use Slince\Console\Context\Argv; use Slince\Console\Context\Option; use Slince\Console\Question\Question; use Slince\Console\Question\ConfirmQuestion; use Slince\Console\Question\ChoiceQuestion; include __DIR__ . '/../vendor/autoload.php'; class SayHalloCommand extends Command { protected $name = 'hello'; function configure() { $this->setDescription('Say hello to someone'); $this->addArgument('name2', Option::VALUE_REQUIRED); $this->addArgument('age', Option::VALUE_OPTIONAL); $this->addOption('yell', Option::VALUE_REQUIRED, 'if set will output uppercase'); } function execute(Io $io, Argv $argv) { $question = new ChoiceQuestion('Whats your favorite sports', ['baskerball', 'foot ball', 'ping pang'], 0); $question->setMaxAttempts(3); $question->setMultiSelect(true); $answer = $this->getHelper('Question')->ask($question); $io->write(sprintf("You like %s", implode(', ', $answer))); } } $console = new Console(); $console->addCommand(new SayHalloCommand()); $console->run();