There are two different ways of creating a command configuration: * Call {@link create()} or {@link ApplicationConfig::beginCommand()} and use the fluent interface: php $config = CommandConfig::create() ->setName('server') ->setDescription('List and manage servers') ->beginSubCommand('add') ->setDescription('Add a new server') ->addArgument('host', Argument::REQUIRED) ->addOption('port', 'p', Option::VALUE_OPTIONAL, null, 80) ->end() ... ; * Extend the class and implement the {@link configure()} method: php class ServerCommandConfig extends CommandConfig { protected function configure() { $this ->setName('server') ->setDescription('List and manage servers') ->beginSubCommand('add') ->setDescription('Add a new server') ->addArgument('host', Argument::REQUIRED) ->addOption('port', 'p', Option::VALUE_OPTIONAL, null, 80) ->end() ... ; } } You can choose between two different ways of executing a command: * You can register a callback with {@link setCallback()}. The callback receives the input, the standard output and the error output as arguments: php $config->setCallback( function (InputInterface $input, OutputInterface $output, OutputInterface $errorOutput) { ... } ); * You can implement a custom command handler and return the handler from {@link getHandler()}. Since the command handler is separated, it can be easily tested: php class ServerConfig extends CommandConfig { public function getHandler() { return new ServerHandler(); } }
Since: 1.0
Author: Bernhard Schussek (bschussek@gmail.com)
Inheritance: extends Config
 public function testLenientArgsParsingDefaultsToParentValue()
 {
     $this->parentConfig->enableLenientArgsParsing();
     $this->assertTrue($this->config->isLenientArgsParsingEnabled());
     $this->parentConfig->disableLenientArgsParsing();
     $this->assertFalse($this->config->isLenientArgsParsingEnabled());
 }
示例#2
0
 public function testCreateDisabled()
 {
     $config = CommandConfig::create()->setName('command')->disable();
     $applicationConfig = new ApplicationConfig();
     $application = new ConsoleApplication($applicationConfig);
     $applicationAdapter = new ApplicationAdapter($application);
     $command = new Command($config, $application);
     $adapter = new CommandAdapter($command, $applicationAdapter);
     $this->assertFalse($adapter->isEnabled());
 }
示例#3
0
 public function testBuildAnonymousArgsFormat()
 {
     $baseFormat = new ArgsFormat();
     $this->config->setName('command');
     $this->config->setAliases(array('alias1', 'alias2'));
     $this->config->addOption('option');
     $this->config->addArgument('argument');
     $this->config->markAnonymous();
     $expected = ArgsFormat::build($baseFormat)->addArgument(new Argument('argument'))->addOption(new Option('option'))->getFormat();
     $this->assertEquals($expected, $this->config->buildArgsFormat($baseFormat));
 }
示例#4
0
 private function doHandle(Args $args, IO $io)
 {
     if ($this->dispatcher && $this->dispatcher->hasListeners(ConsoleEvents::PRE_HANDLE)) {
         $event = new PreHandleEvent($args, $io, $this);
         $this->dispatcher->dispatch(ConsoleEvents::PRE_HANDLE, $event);
         if ($event->isHandled()) {
             return $event->getStatusCode();
         }
     }
     $commandHandler = $this->config->getHandler();
     $handlerMethod = $this->config->getHandlerMethod();
     return $commandHandler->{$handlerMethod}($args, $io, $this);
 }
示例#5
0
 private function validateCommandName(CommandConfig $config)
 {
     $name = $config->getName();
     if (!$name) {
         throw CannotAddCommandException::nameEmpty();
     }
     if ($this->commands->contains($name)) {
         throw CannotAddCommandException::nameExists($name);
     }
 }
 public function testGetAliases()
 {
     $ls = new Command(CommandConfig::create('ls')->addAlias('ls-alias'));
     $cd = new Command(CommandConfig::create('cd')->addAlias('cd-alias'));
     $this->collection->add($ls);
     $this->collection->add($cd);
     $this->assertSame(array('cd-alias' => 'cd', 'ls-alias' => 'ls'), $this->collection->getAliases());
 }
 public function testHasCommandConfig()
 {
     $this->config->addCommandConfig($config = new CommandConfig());
     $this->assertFalse($this->config->hasCommandConfig('command'));
     $this->assertFalse($this->config->hasCommandConfig('foobar'));
     $config->setName('command');
     $this->assertTrue($this->config->hasCommandConfig('command'));
     $this->assertFalse($this->config->hasCommandConfig('foobar'));
 }
示例#8
0
 public function testRunWithLenientArgsParsing()
 {
     $rawArgs = $this->getMock('Webmozart\\Console\\Api\\Args\\RawArgs');
     $parsedArgs = new Args(new ArgsFormat());
     $io = $this->getMockBuilder('Webmozart\\Console\\Api\\IO\\IO')->disableOriginalConstructor()->getMock();
     $parser = $this->getMock('Webmozart\\Console\\Api\\Args\\ArgsParser');
     $handler = $this->getMock('stdClass', array('handle'));
     $config = new CommandConfig('command');
     $config->setArgsParser($parser);
     $config->setHandler($handler);
     $config->enableLenientArgsParsing();
     $command = new Command($config);
     $format = $command->getArgsFormat();
     $parser->expects($this->once())->method('parseArgs')->with($this->identicalTo($rawArgs), $this->identicalTo($format), true)->willReturn($parsedArgs);
     $handler->expects($this->once())->method('handle')->with($parsedArgs, $io, $command)->willReturn(123);
     $this->assertSame(123, $command->run($rawArgs, $io));
 }
 /**
  * @dataProvider getInputOutput
  */
 public function testFindSimilarNames($input, array $suggestions)
 {
     $commands = new CommandCollection(array(new Command(CommandConfig::create('package')->addAlias('package-alias')), new Command(CommandConfig::create('pack')->addAlias('pack-alias')), new Command(CommandConfig::create('pack'))));
     $this->assertSame($suggestions, SimilarCommandName::find($input, $commands));
 }
 public function testHasDefaultCommands()
 {
     $config = new CommandConfig('command');
     $config->markDefault();
     $this->config->addCommandConfig($config);
     $application = new ConsoleApplication($this->config);
     $this->assertTrue($application->hasDefaultCommands());
 }