Completion.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. namespace Stecman\Component\Symfony\Console\BashCompletion;
  3. use Stecman\Component\Symfony\Console\BashCompletion\Completion\CompletionInterface;
  4. class Completion implements CompletionInterface
  5. {
  6. /**
  7. * The type of input (option/argument) the completion should be run for
  8. *
  9. * @see CompletionInterface::ALL_TYPES
  10. * @var string
  11. */
  12. protected $type;
  13. /**
  14. * The command name the completion should be run for
  15. *
  16. * @see CompletionInterface::ALL_COMMANDS
  17. * @var string|null
  18. */
  19. protected $commandName;
  20. /**
  21. * The option/argument name the completion should be run for
  22. *
  23. * @var string
  24. */
  25. protected $targetName;
  26. /**
  27. * Array of values to return, or a callback to generate completion results with
  28. * The callback can be in any form accepted by call_user_func.
  29. *
  30. * @var callable|array
  31. */
  32. protected $completion;
  33. /**
  34. * Create a Completion with the command name set to CompletionInterface::ALL_COMMANDS
  35. *
  36. * @deprecated - This will be removed in 1.0.0 as it is redundant and isn't any more concise than what it implements.
  37. *
  38. * @param string $targetName
  39. * @param string $type
  40. * @param array|callable $completion
  41. * @return Completion
  42. */
  43. public static function makeGlobalHandler($targetName, $type, $completion)
  44. {
  45. return new Completion(CompletionInterface::ALL_COMMANDS, $targetName, $type, $completion);
  46. }
  47. /**
  48. * @param string $commandName
  49. * @param string $targetName
  50. * @param string $type
  51. * @param array|callable $completion
  52. */
  53. public function __construct($commandName, $targetName, $type, $completion)
  54. {
  55. $this->commandName = $commandName;
  56. $this->targetName = $targetName;
  57. $this->type = $type;
  58. $this->completion = $completion;
  59. }
  60. /**
  61. * Return the stored completion, or the results returned from the completion callback
  62. *
  63. * @return array
  64. */
  65. public function run()
  66. {
  67. if ($this->isCallable()) {
  68. return call_user_func($this->completion);
  69. }
  70. return $this->completion;
  71. }
  72. /**
  73. * Get type of input (option/argument) the completion should be run for
  74. *
  75. * @see CompletionInterface::ALL_TYPES
  76. * @return string|null
  77. */
  78. public function getType()
  79. {
  80. return $this->type;
  81. }
  82. /**
  83. * Set type of input (option/argument) the completion should be run for
  84. *
  85. * @see CompletionInterface::ALL_TYPES
  86. * @param string|null $type
  87. */
  88. public function setType($type)
  89. {
  90. $this->type = $type;
  91. }
  92. /**
  93. * Get the command name the completion should be run for
  94. *
  95. * @see CompletionInterface::ALL_COMMANDS
  96. * @return string|null
  97. */
  98. public function getCommandName()
  99. {
  100. return $this->commandName;
  101. }
  102. /**
  103. * Set the command name the completion should be run for
  104. *
  105. * @see CompletionInterface::ALL_COMMANDS
  106. * @param string|null $commandName
  107. */
  108. public function setCommandName($commandName)
  109. {
  110. $this->commandName = $commandName;
  111. }
  112. /**
  113. * Set the option/argument name the completion should be run for
  114. *
  115. * @see setType()
  116. * @return string
  117. */
  118. public function getTargetName()
  119. {
  120. return $this->targetName;
  121. }
  122. /**
  123. * Get the option/argument name the completion should be run for
  124. *
  125. * @see getType()
  126. * @param string $targetName
  127. */
  128. public function setTargetName($targetName)
  129. {
  130. $this->targetName = $targetName;
  131. }
  132. /**
  133. * Return the array or callback configured for for the Completion
  134. *
  135. * @return array|callable
  136. */
  137. public function getCompletion()
  138. {
  139. return $this->completion;
  140. }
  141. /**
  142. * Set the array or callback to return/run when Completion is run
  143. *
  144. * @see run()
  145. * @param array|callable $completion
  146. */
  147. public function setCompletion($completion)
  148. {
  149. $this->completion = $completion;
  150. }
  151. /**
  152. * Check if the configured completion value is a callback function
  153. *
  154. * @return bool
  155. */
  156. public function isCallable()
  157. {
  158. return is_callable($this->completion);
  159. }
  160. }