private static function filterDuplicates(array $names, CommandCollection $commands) { $filteredNames = array(); foreach ($names as $nameToFilter) { // Check all existing names for duplicates foreach ($filteredNames as $filteredName) { // $nameToFilter is a duplicate - skip if ($commands->get($nameToFilter) === $commands->get($filteredName)) { continue 2; } } $filteredNames[] = $nameToFilter; } return $filteredNames; }
public function testIterator() { $this->collection->add($ls = new Command(new CommandConfig('ls'))); $this->collection->add($cd = new Command(new CommandConfig('cd'))); $result = iterator_to_array($this->collection); $this->assertSame(array('ls' => $ls, 'cd' => $cd), $result); }
private function validateCommandName(CommandConfig $config) { $name = $config->getName(); if (!$name) { throw CannotAddCommandException::nameEmpty(); } if ($this->commands->contains($name)) { throw CannotAddCommandException::nameExists($name); } }
/** * @param RawArgs $args * @param CommandCollection $namedCommands * @param string[] $argumentsToTest * @param string[] $optionsToTest * * @return ResolveResult */ private function processArguments(RawArgs $args, CommandCollection $namedCommands, array $argumentsToTest, array $optionsToTest) { $currentCommand = null; // Parse the arguments for command names until we fail to find a // matching command foreach ($argumentsToTest as $name) { if (!$namedCommands->contains($name)) { break; } $nextCommand = $namedCommands->get($name); if ($nextCommand->getConfig() instanceof OptionCommandConfig) { break; } $currentCommand = $nextCommand; $namedCommands = $currentCommand->getNamedSubCommands(); } if (!$currentCommand) { return null; } return $this->processOptions($args, $currentCommand, $optionsToTest); }
private function validateSubCommandName(SubCommandConfig $config) { $name = $config->getName(); if (!$name) { throw CannotAddCommandException::nameEmpty(); } if ($this->subCommands->contains($name)) { throw CannotAddCommandException::nameExists($name); } if ($config instanceof OptionCommandConfig) { if ($this->argsFormat->hasOption($name)) { throw CannotAddCommandException::optionExists($name); } if ($shortName = $config->getShortName()) { if ($this->subCommands->contains($shortName)) { throw CannotAddCommandException::nameExists($name); } if ($this->argsFormat->hasOption($shortName)) { throw CannotAddCommandException::optionExists($shortName); } } } }