Completed
Push — master ( e57357...0c5664 )
by Alexander
9s
created

DebugAspectCommand::getPrettyText()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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