Completed
Push — symfony-console-application ( 3187e2...c3ee2a )
by Luis
10:39
created

GenerateClassDiagramCommand::configure()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 41
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 34
nc 1
nop 0
dl 0
loc 41
rs 8.8571
c 0
b 0
f 0
1
<?php
2
/**
3
 * PHP version 7.1
4
 *
5
 * This source file is subject to the license that is bundled with this package in the file LICENSE.
6
 */
7
8
namespace PhUml\Console\Commands;
9
10
use PhUml\Actions\CanGenerateClassDiagram;
11
use PhUml\Actions\GenerateClassDiagram;
12
use PhUml\Parser\CodeFinder;
13
use PhUml\Parser\TokenParser;
14
use PhUml\Processors\DotProcessor;
15
use PhUml\Processors\GraphvizProcessor;
16
use PhUml\Processors\NeatoProcessor;
17
use PhUml\Processors\Processor;
18
use RuntimeException;
19
use Symfony\Component\Console\Command\Command;
20
use Symfony\Component\Console\Input\InputArgument;
21
use Symfony\Component\Console\Input\InputInterface;
22
use Symfony\Component\Console\Input\InputOption;
23
use Symfony\Component\Console\Output\OutputInterface;
24
25
class GenerateClassDiagramCommand extends Command implements CanGenerateClassDiagram
26
{
27
    /** @var OutputInterface */
28
    private $output;
29
30
    /** @throws \InvalidArgumentException */
31
    protected function configure()
32
    {
33
        $this
34
            ->setName('phuml:diagram')
35
            ->setDescription('Generate a class diagram scanning the given directory')
36
            ->setHelp(<<<HELP
37
Example:
38
    php bin/phuml phuml:diagram -r -a -p neato ./src out.png
39
40
    This example will scan the `./src` directory recursively for php files.
41
    It will process them with the option `associations` set to true. After that it 
42
    will be send to the `neato` processor and saved to the file `out.png`.
43
HELP
44
            )
45
            ->addOption(
46
                'recursive',
47
                'r',
48
                InputOption::VALUE_NONE,
49
                'Look for classes in the given directory recursively'
50
            )
51
            ->addOption(
52
                'processor',
53
                'p',
54
                InputOption::VALUE_REQUIRED,
55
                'Choose between the neato and dot processors'
56
            )
57
            ->addOption(
58
                'associations',
59
                'a',
60
                InputOption::VALUE_NONE,
61
                'If present, the Graphviz processor will generate association among classes'
62
            )
63
            ->addArgument(
64
                'directory',
65
                InputArgument::REQUIRED,
66
                'The directory to be scanned to generate the class diagram'
67
            )
68
            ->addArgument(
69
                'output',
70
                InputArgument::REQUIRED,
71
                'The file name for your class diagram'
72
            );
73
    }
74
75
    /**
76
     * @throws \LogicException
77
     * @throws \Symfony\Component\Console\Exception\InvalidArgumentException
78
     * @throws \RuntimeException
79
     */
80
    protected function execute(InputInterface $input, OutputInterface $output)
81
    {
82
        $this->output = $output;
83
84
        $directory = $input->getArgument('directory');
85
        $diagramFile = $input->getArgument('output');
86
        $recursive = (bool)$input->getOption('recursive');
87
        $associations = (bool)$input->getOption('associations');
88
        $processor = $input->getOption('processor');
89
90
        if (!is_dir($directory)) {
91
            throw new RuntimeException("'$directory' is not a valid directory");
92
        }
93
94
        $action = new GenerateClassDiagram(new TokenParser(), new GraphvizProcessor($associations));
95
        $action->attach($this);
96
97
        $finder = new CodeFinder();
98
        $finder->addDirectory($directory, $recursive);
99
100
        if (!\in_array($processor, ['neato', 'dot'], true)) {
101
            throw new RuntimeException("Expected processors are neato and dot, '$processor' found");
102
        }
103
104
        if ($processor === 'dot') {
105
            $action->setImageProcessor(new DotProcessor());
106
        } else {
107
            $action->setImageProcessor(new NeatoProcessor());
108
        }
109
110
        $output->writeln('[|] Running... (This may take some time)');
111
112
        $action->generate($finder, $diagramFile);
113
114
        return 0;
115
    }
116
117
    public function runningParser(): void
118
    {
119
        $this->output->writeln('[|] Parsing class structure');
120
    }
121
122
    public function runningProcessor(Processor $processor): void
123
    {
124
        $this->output->writeln("[|] Running '{$processor->name()}' processor");
125
    }
126
127
    public function savingResult(): void
128
    {
129
        $this->output->writeln('[|] Writing generated data to disk');
130
    }
131
}
132