DebugAspectCommand   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 2
dl 0
loc 110
c 0
b 0
f 0
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A showAspectInfo() 0 12 2
A configure() 0 14 1
A execute() 0 22 2
A showRegisteredAspectsInfo() 0 6 2
A showAspectPointcutsAndAdvisors() 0 22 4
A getPrettyText() 0 6 1
1
<?php
2
3
declare(strict_types=1);
4
/*
5
 * Go! AOP framework
6
 *
7
 * @copyright Copyright 2015, Lisachenko Alexander <[email protected]>
8
 *
9
 * This source file is subject to the license that is bundled
10
 * with this source code in the file LICENSE.
11
 */
12
13
namespace Go\Console\Command;
14
15
use Go\Aop\Advisor;
16
use Go\Aop\Aspect;
17
use Go\Aop\Pointcut;
18
use Go\Core\AspectLoader;
19
use ReflectionObject;
20
use Symfony\Component\Console\Input\InputInterface;
21
use Symfony\Component\Console\Input\InputOption;
22
use Symfony\Component\Console\Output\OutputInterface;
23
use Symfony\Component\Console\Style\SymfonyStyle;
24
25
/**
26
 * Console command for querying an information about aspects
27
 */
28
class DebugAspectCommand extends BaseAspectCommand
29
{
30
    /**
31
     * {@inheritDoc}
32
     */
33
    protected function configure(): void
34
    {
35
        parent::configure();
36
        $this
37
            ->setName('debug:aspect')
38
            ->addOption('aspect', null, InputOption::VALUE_OPTIONAL, 'Optional aspect name to filter')
39
            ->setDescription('Provides an interface for querying the information about aspects')
40
            ->setHelp(
41
                <<<EOT
42
Allows to query an information about enabled aspects.
43
EOT
44
            )
45
        ;
46
    }
47
48
    /**
49
     * {@inheritDoc}
50
     */
51
    protected function execute(InputInterface $input, OutputInterface $output): int
52
    {
53
        $this->loadAspectKernel($input, $output);
54
55
        $io = new SymfonyStyle($input, $output);
56
57
        $container = $this->aspectKernel->getContainer();
58
        $aspects   = [];
59
        $io->title('Aspect debug information');
60
61
        $aspectName = $input->getOption('aspect');
62
        if (!$aspectName) {
63
            $io->text('<info>' . get_class($this->aspectKernel) . '</info> has following enabled aspects:');
64
            $aspects = $container->getByTag('aspect');
65
        } else {
66
            $aspect    = $container->getAspect($aspectName);
67
            $aspects[] = $aspect;
68
        }
69
        $this->showRegisteredAspectsInfo($io, $aspects);
70
71
        return 0;
72
    }
73
74
    /**
75
     * Shows an information about registered aspects
76
     *
77
     * @param Aspect[] $aspects List of aspects
78
     */
79
    private function showRegisteredAspectsInfo(SymfonyStyle $io, array $aspects): void
80
    {
81
        foreach ($aspects as $aspect) {
82
            $this->showAspectInfo($io, $aspect);
83
        }
84
    }
85
86
    /**
87
     * Displays an information about single aspect
88
     */
89
    private function showAspectInfo(SymfonyStyle $io, Aspect $aspect): void
90
    {
91
        $refAspect  = new ReflectionObject($aspect);
92
        $aspectName = $refAspect->getName();
93
        $io->section($aspectName);
94
        $io->writeln('Defined in: <info>' . $refAspect->getFileName() . '</info>');
95
        $docComment = $refAspect->getDocComment();
96
        if ($docComment) {
97
            $io->writeln($this->getPrettyText($docComment));
98
        }
99
        $this->showAspectPointcutsAndAdvisors($io, $aspect);
100
    }
101
102
    /**
103
     * Shows an information about aspect pointcuts and advisors
104
     */
105
    private function showAspectPointcutsAndAdvisors(SymfonyStyle $io, Aspect $aspect): void
106
    {
107
        $container = $this->aspectKernel->getContainer();
108
109
        /** @var AspectLoader $aspectLoader */
110
        $aspectLoader = $container->get('aspect.loader');
111
        $io->writeln('<comment>Pointcuts and advices</comment>');
112
113
        $aspectItems     = $aspectLoader->load($aspect);
114
        $aspectItemsInfo = [];
115
        foreach ($aspectItems as $itemId => $item) {
116
            $itemType = 'Unknown';
117
            if ($item instanceof Pointcut) {
118
                $itemType = 'Pointcut';
119
            }
120
            if ($item instanceof Advisor) {
121
                $itemType = 'Advisor';
122
            }
123
            $aspectItemsInfo[] = [$itemType, $itemId];
124
        }
125
        $io->table(['Type', 'Identifier'], $aspectItemsInfo);
126
    }
127
128
    /**
129
     * Gets the reformatted comment text.
130
     */
131
    private function getPrettyText(string $comment): string
132
    {
133
        $text = preg_replace('|^\s*/?\*+/?|m', '', $comment);
134
135
        return $text;
136
    }
137
}
138