AspectsCommand.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * This file is part of Hyperf.
  5. *
  6. * @link https://www.hyperf.io
  7. * @document https://hyperf.wiki
  8. * @contact group@hyperf.io
  9. * @license https://github.com/hyperf/hyperf/blob/master/LICENSE
  10. */
  11. namespace Hyperf\Devtool\Describe;
  12. use Hyperf\Command\Annotation\Command;
  13. use Hyperf\Command\Command as HyperfCommand;
  14. use Hyperf\Di\Annotation\AspectCollector;
  15. use Symfony\Component\Console\Helper\Table;
  16. use Symfony\Component\Console\Helper\TableSeparator;
  17. use Symfony\Component\Console\Input\InputOption;
  18. use Symfony\Component\Console\Output\OutputInterface;
  19. #[Command]
  20. class AspectsCommand extends HyperfCommand
  21. {
  22. public function __construct()
  23. {
  24. parent::__construct('describe:aspects');
  25. }
  26. public function handle()
  27. {
  28. $classes = $this->input->getOption('classes');
  29. $classes = $classes ? explode(',', $classes) : null;
  30. $aspects = $this->input->getOption('aspects');
  31. $aspects = $aspects ? explode(',', $aspects) : null;
  32. $collector = AspectCollector::list();
  33. $this->show('Classes', $this->handleData($collector['classes'], $classes, $aspects), $this->output);
  34. $this->show('Annotations', $this->handleData($collector['annotations'], $classes, $aspects), $this->output);
  35. }
  36. protected function configure()
  37. {
  38. $this->setDescription('Describe the aspects.')
  39. ->addOption('classes', 'e', InputOption::VALUE_OPTIONAL, 'Get the detail of the specified information by classes.')
  40. ->addOption('aspects', 'l', InputOption::VALUE_OPTIONAL, 'Get the detail of the specified information by aspects.');
  41. }
  42. protected function handleData(array $collector, ?array $classes, ?array $aspects): array
  43. {
  44. $data = [];
  45. foreach ($collector as $aspect => $targets) {
  46. foreach ($targets as $target) {
  47. if ($classes && ! $this->isMatch($target, $classes)) {
  48. continue;
  49. }
  50. if ($aspects && ! $this->isMatch($aspect, $aspects)) {
  51. continue;
  52. }
  53. $data[$target]['targets'] = $target;
  54. $data[$target]['aspects'] = array_merge($data[$target]['aspects'] ?? [], [$aspect]);
  55. }
  56. }
  57. return $data;
  58. }
  59. protected function isMatch(string $target, array $keywords = [])
  60. {
  61. foreach ($keywords as $keyword) {
  62. if (str_contains($target, $keyword)) {
  63. return true;
  64. }
  65. }
  66. return false;
  67. }
  68. protected function show(string $title, array $data, OutputInterface $output)
  69. {
  70. $rows = [];
  71. foreach ($data as $route) {
  72. $route['aspects'] = implode(PHP_EOL, (array) $route['aspects']);
  73. $rows[] = $route;
  74. $rows[] = new TableSeparator();
  75. }
  76. $rows = array_slice($rows, 0, count($rows) - 1);
  77. if ($rows) {
  78. $table = new Table($output);
  79. $table->setHeaders([$title, 'Aspects'])->setRows($rows);
  80. $table->render();
  81. }
  82. }
  83. }