DrushInputAdapter.php

Definition of Drush\Command\DrushInputAdapter.

Namespace

Drush\Command

Classes

Namesort descending Description
DrushInputAdapter Adapter for Symfony Console InputInterface

File

lib/Drush/Command/DrushInputAdapter.php
View source
  1. <?php
  2. /**
  3. * @file
  4. * Definition of Drush\Command\DrushInputAdapter.
  5. */
  6. namespace Drush\Command;
  7. use Symfony\Component\Console\Input\InputInterface;
  8. use Symfony\Component\Console\Input\InputDefinition;
  9. /**
  10. * Adapter for Symfony Console InputInterface
  11. *
  12. * This class can serve as a stand-in wherever an InputInterface
  13. * is needed. It calls through to ordinary Drush procedural functions.
  14. * This object should not be used directly; it exists only in
  15. * the Drush 8.x branch.
  16. *
  17. * We use this class rather than using an ArrayInput for two reasons:
  18. * 1) We do not want to convert our options array back to '--option=value'
  19. * or '--option value' just to have them re-parsed again.
  20. * 2) We do not want Symfony to attempt to validate our options or arguments
  21. * for us.
  22. */
  23. class DrushInputAdapter implements InputInterface {
  24. protected $arguments;
  25. protected $options;
  26. protected $interactive;
  27. public function __construct($arguments, $options, $command = false, $interactive = true)
  28. {
  29. $this->arguments = $arguments;
  30. $this->options = $options;
  31. // If a command name is provided as a parameter, then push
  32. // it onto the front of the arguments list as a service
  33. if ($command) {
  34. $this->arguments = array_merge(
  35. [ 'command' => $command ],
  36. $this->arguments
  37. );
  38. }
  39. // Is it interactive, or is it not interactive?
  40. // Call drush_get_option() here if value not passed in?
  41. $this->interactive = $interactive;
  42. }
  43. /**
  44. * {@inheritdoc}
  45. */
  46. public function getFirstArgument()
  47. {
  48. return reset($arguments);
  49. }
  50. /**
  51. * {@inheritdoc}
  52. */
  53. public function hasParameterOption($values)
  54. {
  55. $values = (array) $values;
  56. foreach ($values as $value) {
  57. if (array_key_exists($value, $this->options)) {
  58. return true;
  59. }
  60. }
  61. return false;
  62. }
  63. /**
  64. * {@inheritdoc}
  65. */
  66. public function getParameterOption($values, $default = false)
  67. {
  68. $values = (array) $values;
  69. foreach ($values as $value) {
  70. if (array_key_exists($value, $this->options)) {
  71. return $this->getOption($value);
  72. }
  73. }
  74. return $default;
  75. }
  76. /**
  77. * {@inheritdoc}
  78. */
  79. public function bind(InputDefinition $definition)
  80. {
  81. // no-op: this class exists to avoid validation
  82. }
  83. /**
  84. * {@inheritdoc}
  85. */
  86. public function validate()
  87. {
  88. // no-op: this class exists to avoid validation
  89. }
  90. /**
  91. * {@inheritdoc}
  92. */
  93. public function getArguments()
  94. {
  95. return $this->arguments;
  96. }
  97. /**
  98. * {@inheritdoc}
  99. */
  100. public function getArgument($name)
  101. {
  102. // TODO: better to throw if an argument that does not exist is requested?
  103. return isset($this->arguments[$name]) ? $this->arguments[$name] : '';
  104. }
  105. /**
  106. * {@inheritdoc}
  107. */
  108. public function setArgument($name, $value)
  109. {
  110. $this->arguments[$name] = $value;
  111. }
  112. /**
  113. * {@inheritdoc}
  114. */
  115. public function hasArgument($name)
  116. {
  117. return isset($this->arguments[$name]);
  118. }
  119. /**
  120. * {@inheritdoc}
  121. */
  122. public function getOptions()
  123. {
  124. return $this->options;
  125. }
  126. /**
  127. * {@inheritdoc}
  128. */
  129. public function getOption($name)
  130. {
  131. return $this->options[$name];
  132. }
  133. /**
  134. * {@inheritdoc}
  135. */
  136. public function setOption($name, $value)
  137. {
  138. $this->options[$name] = $value;
  139. }
  140. /**
  141. * {@inheritdoc}
  142. */
  143. public function hasOption($name)
  144. {
  145. return isset($this->options[$name]);
  146. }
  147. /**
  148. * {@inheritdoc}
  149. */
  150. public function isInteractive()
  151. {
  152. return $this->interactive;
  153. }
  154. /**
  155. * {@inheritdoc}
  156. */
  157. public function setInteractive($interactive)
  158. {
  159. $this->interactive = $interactive;
  160. }
  161. }