|
| 1 | +<?php |
| 2 | +declare(strict_types=1); |
| 3 | + |
| 4 | +namespace Soap\WsdlReader\Console\Command; |
| 5 | + |
| 6 | +use PhpTui\Term\Actions; |
| 7 | +use PhpTui\Term\ClearType; |
| 8 | +use PhpTui\Term\Terminal; |
| 9 | +use PhpTui\Tui\Bridge\PhpTerm\PhpTermBackend; |
| 10 | +use PhpTui\Tui\Display\Display; |
| 11 | +use PhpTui\Tui\DisplayBuilder; |
| 12 | +use Psl\Ref; |
| 13 | +use Soap\Wsdl\Console\Helper\ConfiguredLoader; |
| 14 | +use Soap\Wsdl\Loader\CallbackLoader; |
| 15 | +use Soap\Wsdl\Loader\FlatteningLoader; |
| 16 | +use Soap\Wsdl\Loader\WsdlLoader; |
| 17 | +use Soap\WsdlReader\Console\UI\Components\LoadingWidget; |
| 18 | +use Soap\WsdlReader\Console\UI\Layout; |
| 19 | +use Soap\WsdlReader\Console\UI\UIState; |
| 20 | +use Symfony\Component\Console\Command\Command; |
| 21 | +use Symfony\Component\Console\Exception\InvalidArgumentException; |
| 22 | +use Symfony\Component\Console\Input\InputArgument; |
| 23 | +use Symfony\Component\Console\Input\InputInterface; |
| 24 | +use Symfony\Component\Console\Input\InputOption; |
| 25 | +use Symfony\Component\Console\Output\OutputInterface; |
| 26 | +use function Psl\Type\non_empty_string; |
| 27 | + |
| 28 | +final class InspectUICommand extends Command |
| 29 | +{ |
| 30 | + public static function getDefaultName(): string |
| 31 | + { |
| 32 | + return 'inspect:ui'; |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * @throws InvalidArgumentException |
| 37 | + */ |
| 38 | + protected function configure(): void |
| 39 | + { |
| 40 | + $this->setDescription('Inspects WSDL file through a user interface.'); |
| 41 | + $this->addArgument('wsdl', InputArgument::REQUIRED, 'Provide the URI of the WSDL you want to validate'); |
| 42 | + $this->addOption('loader', 'l', InputOption::VALUE_REQUIRED, 'Customize the WSDL loader file that will be used'); |
| 43 | + } |
| 44 | + |
| 45 | + protected function execute(InputInterface $input, OutputInterface $output): int |
| 46 | + { |
| 47 | + $terminal = Terminal::new(); |
| 48 | + $terminal->execute(Actions::alternateScreenEnable()); |
| 49 | + $terminal->execute(Actions::enableMouseCapture()); |
| 50 | + $terminal->execute(Actions::cursorHide()); |
| 51 | + $terminal->enableRawMode(); |
| 52 | + |
| 53 | + $display = DisplayBuilder::default(PhpTermBackend::new($terminal))->build(); |
| 54 | + $state = $this->loadState($input, $display); |
| 55 | + |
| 56 | + try { |
| 57 | + while ($state->running) { |
| 58 | + while (null !== $event = $terminal->events()->next()) { |
| 59 | + $state->handle($event); |
| 60 | + } |
| 61 | + |
| 62 | + $display->draw(Layout::create($state)); |
| 63 | + |
| 64 | + usleep(50_000); |
| 65 | + } |
| 66 | + } finally { |
| 67 | + $terminal->disableRawMode(); |
| 68 | + $terminal->execute(Actions::alternateScreenDisable()); |
| 69 | + $terminal->execute(Actions::disableMouseCapture()); |
| 70 | + $terminal->execute(Actions::cursorShow()); |
| 71 | + $terminal->execute(Actions::clear(ClearType::All)); |
| 72 | + } |
| 73 | + |
| 74 | + return self::SUCCESS; |
| 75 | + } |
| 76 | + |
| 77 | + private function loadState(InputInterface $input, Display $display): UIState |
| 78 | + { |
| 79 | + $wsdl = non_empty_string()->assert($input->getArgument('wsdl')); |
| 80 | + /** @var Ref<list<string>> $info */ |
| 81 | + $info = new Ref(['Loading WSDL ...']); |
| 82 | + $display->draw(LoadingWidget::create($info->value)); |
| 83 | + |
| 84 | + $loader = ConfiguredLoader::createFromConfig( |
| 85 | + $input->getOption('loader'), |
| 86 | + static fn (WsdlLoader $loader) => new FlatteningLoader( |
| 87 | + new CallbackLoader(static function (string $location) use ($loader, $display, $info): string { |
| 88 | + $info->value[] = '> Loading '.$location.' ...'; |
| 89 | + $currentIndex = count($info->value) - 1; |
| 90 | + $display->draw(LoadingWidget::create($info->value)); |
| 91 | + |
| 92 | + $result = $loader($location); |
| 93 | + |
| 94 | + $info->value[$currentIndex] .= ' OK'; |
| 95 | + $display->draw(LoadingWidget::create($info->value)); |
| 96 | + |
| 97 | + return $result; |
| 98 | + }) |
| 99 | + ), |
| 100 | + ); |
| 101 | + |
| 102 | + return UIState::load($wsdl, $loader); |
| 103 | + } |
| 104 | +} |
0 commit comments