|
1 | 1 | <?php
|
| 2 | + |
2 | 3 | namespace Articstudio\PhpBin\Commands;
|
3 | 4 |
|
4 | 5 | use Symfony\Component\Console\Command\Command as SymfonyCommand;
|
5 | 6 | use Articstudio\PhpBin\Ui\Menu;
|
6 | 7 | use Symfony\Component\Console\Output\OutputInterface;
|
| 8 | +use Symfony\Component\Console\Input\InputInterface; |
7 | 9 | use Symfony\Component\Console\Input\ArrayInput;
|
| 10 | +use Symfony\Component\Console\Output\ConsoleOutput; |
8 | 11 | use Articstudio\PhpBin\PhpBinException;
|
| 12 | +use Symfony\Component\Console\Question\Question; |
| 13 | +use Symfony\Component\Console\Question\ConfirmationQuestion; |
| 14 | +use Symfony\Component\Console\Question\ChoiceQuestion; |
| 15 | + |
| 16 | +abstract class AbstractCommand extends SymfonyCommand { |
| 17 | + |
| 18 | + use \Articstudio\PhpBin\Concerns\HasOutput; |
| 19 | + use \Articstudio\PhpBin\Concerns\HasShell; |
| 20 | + use \Articstudio\PhpBin\Concerns\HasPhpBin; |
| 21 | + |
| 22 | + |
| 23 | + /** |
| 24 | + * Create menu |
| 25 | + * |
| 26 | + * @param string $title |
| 27 | + * @param array $options |
| 28 | + * |
| 29 | + * @return Menu |
| 30 | + */ |
| 31 | + public function menu( string $title, array $options ): Menu { |
| 32 | + return new Menu( $title, $options ); |
| 33 | + } |
| 34 | + |
| 35 | + public function showMenu( string $title, array $menu_options ) { |
| 36 | + return $this->menu( $title, $menu_options )->open() ?? null; |
| 37 | + } |
| 38 | + |
| 39 | + public function showChoices( string $message, array $packages ) { |
| 40 | + return $this->choiceQuestion( $message, $packages ); |
| 41 | + } |
| 42 | + |
| 43 | + public function question( string $txt, $default = null, ?OutputInterface $output = null, ?InputInterface $input = null ) { |
| 44 | + $question_helper = $this->getHelper( 'question' ); |
| 45 | + $question = new Question( $txt, $default ); |
| 46 | + |
| 47 | + return $question_helper->ask( |
| 48 | + ( $input ?? new ArrayInput( [] ) ), |
| 49 | + ( $output ?? new ConsoleOutput ), |
| 50 | + $question |
| 51 | + ); |
| 52 | + } |
| 53 | + |
| 54 | + public function confirmation( string $txt, $default = false, ?OutputInterface $output = null, ?InputInterface $input = null ) { |
| 55 | + $question_helper = $this->getHelper( 'question' ); |
| 56 | + $question = new ConfirmationQuestion( $txt, $default ); |
| 57 | + |
| 58 | + return ! ! $question_helper->ask( |
| 59 | + ( $input ?? new ArrayInput( [] ) ), |
| 60 | + ( $output ?? new ConsoleOutput ), |
| 61 | + $question |
| 62 | + ); |
| 63 | + } |
| 64 | + |
| 65 | + public function choiceQuestion( string $txt, array $options, ?OutputInterface $output = null, ?InputInterface $input = null ) { |
| 66 | + $question_helper = $this->getHelper( 'question' ); |
| 67 | + $question = new ChoiceQuestion( $txt, $options ); |
| 68 | + $question->setMultiselect( true ); |
| 69 | + |
| 70 | + return $question_helper->ask( |
| 71 | + ( $input ?? new ArrayInput( [] ) ), |
| 72 | + ( $output ?? new ConsoleOutput ), |
| 73 | + $question |
| 74 | + ); |
| 75 | + |
| 76 | + |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Call command by name |
| 81 | + * |
| 82 | + * @param string $name |
| 83 | + * @param array $arguments |
| 84 | + * @param OutputInterface $output |
| 85 | + * |
| 86 | + * @return int |
| 87 | + */ |
| 88 | + protected function callCommandByName( string $name, ?array $arguments, OutputInterface $output ) { |
| 89 | + if ( ! $this->getApplication()->has( $name ) ) { |
| 90 | + throw new PhpBinException( "Command `{$name}` not found." ); |
| 91 | + } |
| 92 | + $command = $this->getApplication()->get( $name ); |
| 93 | + $input = new ArrayInput( $arguments ?? [] ); |
9 | 94 |
|
10 |
| -abstract class AbstractCommand extends SymfonyCommand |
11 |
| -{ |
12 |
| - |
13 |
| - use \Articstudio\PhpBin\Concerns\HasOutput; |
14 |
| - use \Articstudio\PhpBin\Concerns\HasShell; |
15 |
| - use \Articstudio\PhpBin\Concerns\HasPhpBin; |
16 |
| - |
17 |
| - /** |
18 |
| - * Create menu |
19 |
| - * |
20 |
| - * @param string $title |
21 |
| - * @param array $options |
22 |
| - * @return Menu |
23 |
| - */ |
24 |
| - public function menu(string $title, array $options): Menu |
25 |
| - { |
26 |
| - return new Menu($title, $options); |
27 |
| - } |
28 |
| - |
29 |
| - /** |
30 |
| - * Call command by name |
31 |
| - * |
32 |
| - * @param string $name |
33 |
| - * @param array $arguments |
34 |
| - * @param OutputInterface $output |
35 |
| - * @return int |
36 |
| - */ |
37 |
| - protected function callCommandByName(string $name, ?array $arguments, OutputInterface $output) |
38 |
| - { |
39 |
| - if (!$this->getApplication()->has($name)) { |
40 |
| - throw new PhpBinException("Command `{$name}` not found."); |
41 |
| - } |
42 |
| - $command = $this->getApplication()->get($name); |
43 |
| - $input = new ArrayInput($arguments ?? []); |
44 |
| - return $command->run($input, $output); |
45 |
| - } |
| 95 | + return $command->run( $input, $output ); |
| 96 | + } |
46 | 97 | }
|
0 commit comments