Ejemplo n.º 1
0
 /**
  * @param InputOption $option
  *
  * @return \DOMDocument
  */
 private function getInputOptionDocument(InputOption $option)
 {
     $dom = new \DOMDocument('1.0', 'UTF-8');
     $dom->appendChild($objectXML = $dom->createElement('option'));
     $objectXML->setAttribute('name', '--' . $option->getName());
     $pos = strpos($option->getShortcut(), '|');
     if (false !== $pos) {
         $objectXML->setAttribute('shortcut', '-' . substr($option->getShortcut(), 0, $pos));
         $objectXML->setAttribute('shortcuts', '-' . implode('|-', explode('|', $option->getShortcut())));
     } else {
         $objectXML->setAttribute('shortcut', $option->getShortcut() ? '-' . $option->getShortcut() : '');
     }
     $objectXML->setAttribute('accept_value', $option->acceptValue() ? 1 : 0);
     $objectXML->setAttribute('is_value_required', $option->isValueRequired() ? 1 : 0);
     $objectXML->setAttribute('is_multiple', $option->isArray() ? 1 : 0);
     $objectXML->appendChild($descriptionXML = $dom->createElement('description'));
     $descriptionXML->appendChild($dom->createTextNode($option->getDescription()));
     if ($option->acceptValue()) {
         $defaults = is_array($option->getDefault()) ? $option->getDefault() : (is_bool($option->getDefault()) ? array(var_export($option->getDefault(), true)) : ($option->getDefault() ? array($option->getDefault()) : array()));
         $objectXML->appendChild($defaultsXML = $dom->createElement('defaults'));
         if (!empty($defaults)) {
             foreach ($defaults as $default) {
                 $defaultsXML->appendChild($defaultXML = $dom->createElement('default'));
                 $defaultXML->appendChild($dom->createTextNode($default));
             }
         }
     }
     return $dom;
 }
Ejemplo n.º 2
0
 /**
  * Adds an InputOption object.
  *
  * @param InputOption $option An InputOption object
  *
  * @throws \LogicException When option given already exist
  *
  * @api
  */
 public function addOption(InputOption $option)
 {
     if (isset($this->options[$option->getName()]) && !$option->equals($this->options[$option->getName()])) {
         throw new \LogicException(sprintf('An option named "%s" already exists.', $option->getName()));
     }
     if ($option->getShortcut()) {
         foreach (explode('|', $option->getShortcut()) as $shortcut) {
             if (isset($this->shortcuts[$shortcut]) && !$option->equals($this->options[$this->shortcuts[$shortcut]])) {
                 throw new \LogicException(sprintf('An option with shortcut "%s" already exists.', $shortcut));
             }
         }
     }
     $this->options[$option->getName()] = $option;
     if ($option->getShortcut()) {
         foreach (explode('|', $option->getShortcut()) as $shortcut) {
             $this->shortcuts[$shortcut] = $option->getName();
         }
     }
 }
Ejemplo n.º 3
0
 /**
  * @param InputOption $option
  *
  * @return array
  */
 private function getInputOptionData(InputOption $option)
 {
     return array('name' => '--' . $option->getName(), 'shortcut' => $option->getShortcut() ? '-' . implode('|-', explode('|', $option->getShortcut())) : '', 'accept_value' => $option->acceptValue(), 'is_value_required' => $option->isValueRequired(), 'is_multiple' => $option->isArray(), 'description' => $option->getDescription(), 'default' => $option->getDefault());
 }
Ejemplo n.º 4
0
 public function testEquals()
 {
     $option = new InputOption('foo', 'f', null, 'Some description');
     $option2 = new InputOption('foo', 'f', null, 'Alternative description');
     $this->assertTrue($option->equals($option2));
     $option = new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL, 'Some description');
     $option2 = new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL, 'Some description', true);
     $this->assertFalse($option->equals($option2));
     $option = new InputOption('foo', 'f', null, 'Some description');
     $option2 = new InputOption('bar', 'f', null, 'Some description');
     $this->assertFalse($option->equals($option2));
     $option = new InputOption('foo', 'f', null, 'Some description');
     $option2 = new InputOption('foo', '', null, 'Some description');
     $this->assertFalse($option->equals($option2));
     $option = new InputOption('foo', 'f', null, 'Some description');
     $option2 = new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL, 'Some description');
     $this->assertFalse($option->equals($option2));
 }
Ejemplo n.º 5
0
 /**
  * {@inheritdoc}
  */
 protected function describeInputOption(InputOption $option, array $options = array())
 {
     $this->write('**' . $option->getName() . ':**' . "\n\n" . '* Name: `--' . $option->getName() . '`' . "\n" . '* Shortcut: ' . ($option->getShortcut() ? '`-' . implode('|-', explode('|', $option->getShortcut())) . '`' : '<none>') . "\n" . '* Accept value: ' . ($option->acceptValue() ? 'yes' : 'no') . "\n" . '* Is value required: ' . ($option->isValueRequired() ? 'yes' : 'no') . "\n" . '* Is multiple: ' . ($option->isArray() ? 'yes' : 'no') . "\n" . '* Description: ' . ($option->getDescription() ?: '<none>') . "\n" . '* Default: `' . str_replace("\n", '', var_export($option->getDefault(), true)) . '`');
 }
Ejemplo n.º 6
0
 /**
  * {@inheritdoc}
  */
 protected function describeInputOption(InputOption $option, array $options = array())
 {
     if ($option->acceptValue() && null !== $option->getDefault() && (!is_array($option->getDefault()) || count($option->getDefault()))) {
         $default = sprintf('<comment> (default: %s)</comment>', $this->formatDefaultValue($option->getDefault()));
     } else {
         $default = '';
     }
     $nameWidth = isset($options['name_width']) ? $options['name_width'] : strlen($option->getName());
     $nameWithShortcutWidth = $nameWidth - strlen($option->getName()) - 2;
     $this->writeText(sprintf(" <info>%s</info> %-{$nameWithShortcutWidth}s%s%s%s", '--' . $option->getName(), $option->getShortcut() ? sprintf('(-%s) ', $option->getShortcut()) : '', str_replace("\n", "\n" . str_repeat(' ', $nameWidth + 2), $option->getDescription()), $default, $option->isArray() ? '<comment> (multiple values allowed)</comment>' : ''), $options);
 }