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