public function testSelect() { $dialog = new DialogHelper(); $helperSet = new HelperSet(array(new FormatterHelper())); $dialog->setHelperSet($helperSet); $heroes = array('Superman', 'Batman', 'Spiderman'); $dialog->setInputStream($this->getInputStream("\n1\n 1 \nFabien\n1\nFabien\n1\n0,2\n 0 , 2 \n\n\n")); $this->assertEquals('2', $dialog->select($this->getOutputStream(), 'What is your favorite superhero?', $heroes, '2')); $this->assertEquals('1', $dialog->select($this->getOutputStream(), 'What is your favorite superhero?', $heroes)); $this->assertEquals('1', $dialog->select($this->getOutputStream(), 'What is your favorite superhero?', $heroes)); $this->assertEquals('1', $dialog->select($output = $this->getOutputStream(), 'What is your favorite superhero?', $heroes, null, false, 'Input "%s" is not a superhero!', false)); rewind($output->getStream()); $this->assertContains('Input "Fabien" is not a superhero!', stream_get_contents($output->getStream())); try { $this->assertEquals('1', $dialog->select($output = $this->getOutputStream(), 'What is your favorite superhero?', $heroes, null, 1)); $this->fail(); } catch (\InvalidArgumentException $e) { $this->assertEquals('Value "Fabien" is invalid', $e->getMessage()); } $this->assertEquals(array('1'), $dialog->select($this->getOutputStream(), 'What is your favorite superhero?', $heroes, null, false, 'Input "%s" is not a superhero!', true)); $this->assertEquals(array('0', '2'), $dialog->select($this->getOutputStream(), 'What is your favorite superhero?', $heroes, null, false, 'Input "%s" is not a superhero!', true)); $this->assertEquals(array('0', '2'), $dialog->select($this->getOutputStream(), 'What is your favorite superhero?', $heroes, null, false, 'Input "%s" is not a superhero!', true)); $this->assertEquals(array('0', '1'), $dialog->select($this->getOutputStream(), 'What is your favorite superhero?', $heroes, '0,1', false, 'Input "%s" is not a superhero!', true)); $this->assertEquals(array('0', '1'), $dialog->select($this->getOutputStream(), 'What is your favorite superhero?', $heroes, ' 0 , 1 ', false, 'Input "%s" is not a superhero!', true)); }
protected function createInstance() { $entities = entity_get_info(); $entityList = array_keys($entities); $entity = $this->dialog->select($this->output, 'What entity? ', $entityList); $entity = $entityList[$entity]; $bundles = field_info_bundles($entity); $bundleList = array_keys($bundles); $bundle = $this->dialog->select($this->output, 'What bundle? ', $bundleList); $bundle = $bundleList[$bundle]; $field_types = FieldBuilder::getFields(); $fieldList = array(); foreach ($field_types as $field => $definition) { $fieldList[$field] = sprintf("%s (%s)", $definition['label'], $field); } $fieldValues = array_keys($field_types); $field = $this->dialog->select($this->output, 'Field To Attach', array_values($fieldList)); $field = $fieldValues[$field]; $widget_types = WidgetBuilder::getTypesForFieldType($field_types[$field]['type']); $widgetList = array_keys($widget_types); $widget = $this->dialog->select($this->output, 'Widget to Use', array_values($widget_types)); $widget = $widgetList[$widget]; $label = $this->dialog->ask($this->output, 'What label for this instance? '); $widgetDefinition = new WidgetBuilder($widget, $label); $builder = new InstanceBuilder($field, $label, $widgetDefinition); $builder->build($entity, $bundle); $this->output->writeln("Created instance of field {$field} with label <info>{$label}</info> on {$entity}:{$bundle}"); $this->mainQuestion(); }
public function testSelectOnErrorOutput() { $dialog = new DialogHelper(); $helperSet = new HelperSet(array(new FormatterHelper())); $dialog->setHelperSet($helperSet); $heroes = array('Superman', 'Batman', 'Spiderman'); $dialog->setInputStream($this->getInputStream("Stdout\n1\n")); $this->assertEquals('1', $dialog->select($output = $this->getConsoleOutput($this->getOutputStream()), 'What is your favorite superhero?', $heroes, null, false, 'Input "%s" is not a superhero!', false)); rewind($output->getErrorOutput()->getStream()); $this->assertContains('Input "Stdout" is not a superhero!', stream_get_contents($output->getErrorOutput()->getStream())); }
/** * Get the the tasks to perform (can be called an infinite number of times * @param OutputInterface $output * @param array $done * @return array */ private function getTasks(OutputInterface $output, array $done) { $options = ['create', 'alter', 'constraints', 'drop']; $selected = $this->dialog->select($output, 'Please select what you want to check (default is create and alter)', $options, '0,1', false, 'Option "%s" is invalid', true); return array_map(function ($i) use($options, $done, $output) { $action = $options[$i]; if (array_key_exists($action, $done)) { $output->writeln(sprintf('<info>Skipping task %s, already done</info>', $action)); } else { return $action; } }, $selected); }
/** * @return array */ private function askPhpVersions() { // see http://docs.travis-ci.com/user/ci-environment/#PHP-versions $availableVersions = array(1 => '5.6', 2 => '5.5', 3 => '5.4', 4 => '5.3', 5 => 'hhvm'); $default = $this->settings->getDefaultValueFor('travis.phpVersions', array()); if (empty($default)) { $defaultChoice = 2; $defaultText = '5.5'; } else { $defaultChoice = implode(',', array_keys($default)); $defaultText = '"' . implode('", "', array_intersect_key($default, $availableVersions)) . '"'; } $done = false; $selected = array(); while (!$done) { $selected = $this->dialog->select($this->output, sprintf('Which versions of php do you want to test this project on (enter the keys comma separated) [%s]? ', $defaultText), $availableVersions, $defaultChoice, false, 'Value "%s" is invalid', true); $selected = array_intersect_key($availableVersions, array_flip($selected)); $done = $this->dialog->askConfirmation($this->output, sprintf('You have chosen the following versions: "%s", is this correct? ', implode('", "', $selected)), true); } return $selected; }
/** * Hacky way to support multiple interactivity modes similar to -v|vv|vvv * There must be a nicer way to do this... I hope * @param InputInterface $input * @param OutputInterface $output * @return $this */ private function setInteraction(InputInterface $input, OutputInterface $output) { $interaction = $input->getOption('interactive'); if ($interaction === null) { //set to 1 if flag is used as-is if ($input->hasParameterOption(['-i', '--interactive'])) { $interaction = self::INTERACTION_LOW; } else { $interaction = self::INTERACTION_NONE; } } else { switch ($interaction) { case '0': $interaction = self::INTERACTION_NONE; break; case '1': $interaction = self::INTERACTION_LOW; break; case 'i': case '2': $interaction = self::INTERACTION_HIGH; break; case 'ii': case '3': $interaction = self::INTERACTION_EXTENDED; break; default: $output->writeln(sprintf('<error>Invalid value for interactive option: %s</error>', $interaction)); $options = [self::INTERACTION_NONE => 'Not interactive', self::INTERACTION_LOW => 'Normal interactivity', self::INTERACTION_HIGH => 'High interaction level (not implemented yet)', self::INTERACTION_EXTENDED => 'Extensive interaction (not implemented yet)']; $interaction = $this->dialog->select($output, '<question>Please select the desired interactivity level (default: Normal)</question>', $options, self::INTERACTION_LOW); break; } } $this->interactive = (int) $interaction; return $this; }
/** * Select instances * * @return array */ protected function promptInstances() { $this->rsyncupdate_conf = "{$this->path}/cli/conf/deploy.xml"; if (!is_readable($this->rsyncupdate_conf)) { throw new Exception("{$this->rsyncupdate_conf} is not readable."); } $dom = new DOMDocument(); $dom->load($this->rsyncupdate_conf); $xpath = new DOMXPath($dom); /** @var DOMNodeList $groups */ $groups = $xpath->query("//group"); $all_instances = array(); /** @var DOMElement $_group */ foreach ($groups as $_group) { $group_name = $_group->getAttribute("name"); if (!isset($all_instances[$group_name])) { $all_instances[$group_name] = array(); } /** @var DOMNodeList $instance_nodes */ $instance_nodes = $xpath->query("instance", $_group); /** @var DOMElement $_instance */ foreach ($instance_nodes as $_instance) { $_path = $_instance->getAttribute("path"); $all_instances[$group_name][] = $_path; $_shortname = $_instance->getAttribute("shortname"); $this->all_instances[$_path] = $_shortname; } } $instances = array("[ALL]"); foreach ($all_instances as $_group => $_instances) { $instances[] = "[{$_group}]"; foreach ($_instances as $_instance) { $instances[] = "[{$_group}] => {$_instance}"; } } $selected = $this->dialog->select($this->output, 'Select instance (or [group] in order to select all of it)', $instances, 0, false, 'Value "%s" is not valid', true); $selected_values = array_map(function ($c) use($instances) { return $instances[$c]; }, $selected); $this->output->writeln('Selected: ' . implode(', ', $selected_values)); $selected = array(); foreach ($selected_values as $_selected) { if (preg_match("/\\[([A-Za-z]+)\\]\$/", $_selected, $matches)) { // All instances if ($matches[1] == "ALL") { $all = iterator_to_array(new RecursiveIteratorIterator(new RecursiveArrayIterator($all_instances)), false); return $all; } // All instances from given GROUP if (in_array($matches[1], array_keys($all_instances))) { foreach ($all_instances[$matches[1]] as $_instance) { $selected[] = $_instance; } } } else { // Single instance if (preg_match("/\\[[A-Za-z]+\\] =\\> (.*)/", $_selected, $path)) { $selected[] = $path[1]; } } } // Remove duplicate entries if GROUP and group instances are selected $selected = array_unique($selected); return $selected; }
/** * @inheritdoc */ public function select($question, $choices, $default = null, $attempts = false, $errorMessage = 'Value "%s" is invalid', $multiselect = false) { return parent::select($this->output, $this->formatQuestion($question, $default), $choices, $default, $attempts, $errorMessage, $multiselect); }
private function promptTask(DialogHelper $helper, IMigrationTask $migrationTask, OutputInterface $output) { $question = "Task: " . $this->getTaskFullName($migrationTask) . "\n"; if ($this->asBeenRun($migrationTask)) { $question .= "(Already runned)"; } return $helper->select($output, $question, array('r' => 'Run', 's' => 'Skip', 'm' => 'Mark As Run', 'a' => 'Skip All Already Run', 'q' => 'Quit')); }
protected function askUrlBinding(DialogHelper $dialog, OutputInterface $output, $title) { $url = $dialog->ask($output, "{$title} URL [empty for none]: "); $url = trim($url); if (!$url) { return array(null, null); } $arrBindings = array('post', 'redirect'); $binding = $dialog->select($output, 'Binding: ', $arrBindings, 'post'); $binding = $arrBindings[$binding]; return array($url, $binding); }