EventListCommand.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. <?php
  2. namespace Webman\Event;
  3. use Symfony\Component\Console\Command\Command;
  4. use Symfony\Component\Console\Helper\Table;
  5. use Symfony\Component\Console\Input\InputInterface;
  6. use Symfony\Component\Console\Output\OutputInterface;
  7. class EventListCommand extends Command
  8. {
  9. protected static $defaultName = 'event:list';
  10. protected static $defaultDescription = 'Show event list';
  11. /**
  12. * @return void
  13. */
  14. protected function configure()
  15. {
  16. }
  17. /**
  18. * @param InputInterface $input
  19. * @param OutputInterface $output
  20. * @return int
  21. */
  22. protected function execute(InputInterface $input, OutputInterface $output): int
  23. {
  24. $headers = ['id', 'event_name', 'callback'];
  25. $rows = [];
  26. foreach (Event::list() as $id => $item) {
  27. $event_name = $item[0];
  28. $callback = $item[1];
  29. if (is_array($callback) && is_object($callback[0])) {
  30. $callback[0] = get_class($callback[0]);
  31. }
  32. $cb = $callback instanceof \Closure ? 'Closure' : (is_array($callback) ? json_encode($callback) : var_export($callback, 1));
  33. $rows[] = [$id, $event_name, $cb];
  34. }
  35. $table = new Table($output);
  36. $table->setHeaders($headers);
  37. $table->setRows($rows);
  38. $table->render();
  39. return self::SUCCESS;
  40. }
  41. }