zachleigh /
artisanize
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Artisanize; |
||
| 4 | |||
| 5 | use Artisanize\Input\Input; |
||
| 6 | use Artisanize\Output\SymfonyOutput; |
||
| 7 | use Symfony\Component\Console\Question\Question; |
||
| 8 | use Symfony\Component\Console\Input\InputInterface; |
||
| 9 | use Symfony\Component\Console\Output\OutputInterface; |
||
| 10 | use Symfony\Component\Console\Question\ChoiceQuestion; |
||
| 11 | use Symfony\Component\Console\Question\ConfirmationQuestion; |
||
| 12 | use Symfony\Component\Console\Command\Command as SymfonyCommand; |
||
| 13 | |||
| 14 | abstract class Command extends SymfonyCommand |
||
| 15 | { |
||
| 16 | /** |
||
| 17 | * Symfony console output. |
||
| 18 | * |
||
| 19 | * @var SymfonyOutput |
||
| 20 | */ |
||
| 21 | protected $output; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Symfony input implementation. |
||
| 25 | * |
||
| 26 | * @var InputInterface |
||
| 27 | */ |
||
| 28 | protected $input; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * The command description. |
||
| 32 | * |
||
| 33 | * @var string |
||
| 34 | */ |
||
| 35 | protected $description; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Command signature. |
||
| 39 | * |
||
| 40 | * @var null|string |
||
| 41 | */ |
||
| 42 | protected $signature = null; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Configure the command if signature is set. |
||
| 46 | */ |
||
| 47 | protected function configure() |
||
| 48 | { |
||
| 49 | if (!is_null($this->signature)) { |
||
| 50 | $parser = new SignatureParser($this); |
||
| 51 | |||
| 52 | $parser->parse($this->signature); |
||
| 53 | |||
| 54 | $this->setDescription($this->description); |
||
| 55 | } |
||
| 56 | } |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Execute the command. |
||
| 60 | * |
||
| 61 | * @param InputInterface $input |
||
| 62 | * @param OutputInterface $output |
||
| 63 | */ |
||
| 64 | protected function execute(InputInterface $input, OutputInterface $output) |
||
| 65 | { |
||
| 66 | $this->output = new SymfonyOutput($output); |
||
| 67 | |||
| 68 | $this->input = $input; |
||
| 69 | |||
| 70 | if (method_exists($this, 'handle')) { |
||
| 71 | $this->handle(); |
||
| 72 | } |
||
| 73 | } |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Return the output implementation. |
||
| 77 | * |
||
| 78 | * @return SymfonyOutput |
||
| 79 | */ |
||
| 80 | protected function getOutput() |
||
| 81 | { |
||
| 82 | return $this->output; |
||
| 83 | } |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Get root symfony output implementation. |
||
| 87 | * |
||
| 88 | * @return OutputInterface |
||
| 89 | */ |
||
| 90 | protected function getOutputInterface() |
||
| 91 | { |
||
| 92 | return $this->getOutput()->getOutput(); |
||
| 93 | } |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Get the value of a command argument. |
||
| 97 | * |
||
| 98 | * @param string $key |
||
| 99 | * |
||
| 100 | * @return string|array |
||
| 101 | */ |
||
| 102 | protected function argument($key = null) |
||
| 103 | { |
||
| 104 | if (is_null($key)) { |
||
| 105 | return $this->input->getArguments(); |
||
| 106 | } |
||
| 107 | |||
| 108 | return $this->input->getArgument($key); |
||
| 109 | } |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Determine if the given argument is present. |
||
| 113 | * |
||
| 114 | * @param string|int $name |
||
| 115 | * |
||
| 116 | * @return bool |
||
| 117 | */ |
||
| 118 | protected function hasArgument($name) |
||
| 119 | { |
||
| 120 | return $this->input->hasArgument($name); |
||
| 121 | } |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Get the value of a command option. |
||
| 125 | * |
||
| 126 | * @param string $key |
||
| 127 | * |
||
| 128 | * @return string|array |
||
| 129 | */ |
||
| 130 | protected function option($key = null) |
||
| 131 | { |
||
| 132 | if (is_null($key)) { |
||
| 133 | return $this->input->getOptions(); |
||
| 134 | } |
||
| 135 | |||
| 136 | return $this->input->getOption($key); |
||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 137 | } |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Determine if the given option is present. |
||
| 141 | * |
||
| 142 | * @param string $name |
||
| 143 | * |
||
| 144 | * @return bool |
||
| 145 | */ |
||
| 146 | protected function hasOption($name) |
||
| 147 | { |
||
| 148 | return $this->input->hasOption($name); |
||
| 149 | } |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Add input to the command. |
||
| 153 | * |
||
| 154 | * @param Input $input |
||
| 155 | */ |
||
| 156 | public function addInput(Input $input) |
||
| 157 | { |
||
| 158 | $reflection = new \ReflectionClass($input); |
||
| 159 | |||
| 160 | $method = 'add'.$reflection->getShortName(); |
||
| 161 | |||
| 162 | if (method_exists($this, $method)) { |
||
| 163 | $this->$method(...array_values($input->getAttributes())); |
||
| 164 | } |
||
| 165 | } |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Ask for confirmation. |
||
| 169 | * |
||
| 170 | * @param string $text |
||
| 171 | * |
||
| 172 | * @return mixed |
||
| 173 | */ |
||
| 174 | public function confirm($text) |
||
| 175 | { |
||
| 176 | $helper = $this->getHelper('question'); |
||
| 177 | |||
| 178 | $question = new ConfirmationQuestion($text, false); |
||
| 179 | |||
| 180 | return $helper->ask($this->input, $this->getOutputInterface(), $question); |
||
| 181 | } |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Ask a question. |
||
| 185 | * |
||
| 186 | * @param string $question |
||
| 187 | * @param mixed|null $default |
||
| 188 | * |
||
| 189 | * @return mixed |
||
| 190 | */ |
||
| 191 | public function ask($question, $default = null) |
||
| 192 | { |
||
| 193 | $helper = $this->getHelper('question'); |
||
| 194 | |||
| 195 | $question = new Question($question, $default); |
||
| 196 | |||
| 197 | return $helper->ask($this->input, $this->getOutputInterface(), $question); |
||
| 198 | } |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Ask a password. |
||
| 202 | * |
||
| 203 | * @param string $question |
||
| 204 | * |
||
| 205 | * @return mixed |
||
| 206 | */ |
||
| 207 | public function askPassword($question) |
||
| 208 | { |
||
| 209 | $helper = $this->getHelper('question'); |
||
| 210 | |||
| 211 | $question = new Question($question); |
||
| 212 | |||
| 213 | $question->setHidden(true); |
||
| 214 | |||
| 215 | $question->setHiddenFallback(false); |
||
| 216 | |||
| 217 | return $helper->ask($this->input, $this->getOutputInterface(), $question); |
||
| 218 | } |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Ask a question where the answer is available from a list of predefined choices. |
||
| 222 | * |
||
| 223 | * @param string $question |
||
| 224 | * @param array $choices |
||
| 225 | * @param mixed|null $default |
||
| 226 | * |
||
| 227 | * @return mixed |
||
| 228 | */ |
||
| 229 | public function choose($question, array $choices, $default = null) |
||
| 230 | { |
||
| 231 | $helper = $this->getHelper('question'); |
||
| 232 | |||
| 233 | $question = new ChoiceQuestion($question, $choices, $default); |
||
| 234 | |||
| 235 | $question->setErrorMessage('Option %s is invalid.'); |
||
| 236 | |||
| 237 | return $helper->ask($this->input, $this->getOutputInterface(), $question); |
||
| 238 | } |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Ask a question where some auto-completion help is provided. |
||
| 242 | * |
||
| 243 | * @param string $question |
||
| 244 | * @param array $autoCompletion |
||
| 245 | * @param mixed|null $default |
||
| 246 | * |
||
| 247 | * @return mixed |
||
| 248 | */ |
||
| 249 | public function anticipate($question, array $autoCompletion, $default = null) |
||
| 250 | { |
||
| 251 | $helper = $this->getHelper('question'); |
||
| 252 | |||
| 253 | $question = new Question($question, $default); |
||
| 254 | |||
| 255 | $question->setAutocompleterValues($autoCompletion); |
||
| 256 | |||
| 257 | return $helper->ask($this->input, $this->getOutputInterface(), $question); |
||
| 258 | } |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Ask a question where the answer is available from a list of predefined choices and more choices can be selected. |
||
| 262 | * |
||
| 263 | * @param string $question |
||
| 264 | * @param array $choices |
||
| 265 | * @param mixed|null $default |
||
| 266 | * |
||
| 267 | * @return mixed |
||
| 268 | */ |
||
| 269 | public function choice($question, array $choices, $default = null) |
||
| 270 | { |
||
| 271 | $helper = $this->getHelper('question'); |
||
| 272 | |||
| 273 | $question = new ChoiceQuestion($question, $choices, $default); |
||
| 274 | |||
| 275 | $question->setMultiselect(true); |
||
| 276 | |||
| 277 | return $helper->ask($this->input, $this->getOutputInterface(), $question); |
||
| 278 | } |
||
| 279 | } |
||
| 280 |