Completed
Push — dot-language-processor ( 82cdb7...13d982 )
by Luis
14:10
created

GenerateDotFileCommand   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 63
rs 10
c 0
b 0
f 0
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
B configure() 0 35 1
A execute() 0 22 2
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\GenerateDotFile;
11
use PhUml\Parser\CodeFinder;
12
use PhUml\Parser\TokenParser;
13
use PhUml\Processors\GraphvizProcessor;
14
use RuntimeException;
15
use Symfony\Component\Console\Input\InputArgument;
16
use Symfony\Component\Console\Input\InputInterface;
17
use Symfony\Component\Console\Input\InputOption;
18
use Symfony\Component\Console\Output\OutputInterface;
19
20
class GenerateDotFileCommand extends GeneratorCommand
21
{
22
    protected function configure()
23
    {
24
        $this
25
            ->setName('phuml:dot')
26
            ->setDescription('Generates a digraph in DOT format of a given directory')
27
            ->setHelp(<<<HELP
28
Example:
29
    php bin/phuml phuml:dot -r -a ./src dot.gv
30
31
    This example will scan the `./src` directory recursively for php files.
32
    It will process them with the option `associations` set to true.
33
    It will generate a digraph in dot format and save it to the file `dot.gv`.
34
HELP
35
            )
36
            ->addOption(
37
                'recursive',
38
                'r',
39
                InputOption::VALUE_NONE,
40
                'Look for classes in the given directory recursively'
41
            )
42
            ->addOption(
43
                'associations',
44
                'a',
45
                InputOption::VALUE_NONE,
46
                'If present, the Graphviz processor will generate association among classes'
47
            )
48
            ->addArgument(
49
                'directory',
50
                InputArgument::REQUIRED,
51
                'The directory to be scanned to generate the dot file'
52
            )
53
            ->addArgument(
54
                'output',
55
                InputArgument::REQUIRED,
56
                'The file name for your dot file'
57
            )
58
        ;
59
    }
60
61
    protected function execute(InputInterface $input, OutputInterface $output)
62
    {
63
        $directory = $input->getArgument('directory');
64
        $dotFile = $input->getArgument('output');
65
        $associations = (bool)$input->getOption('associations');
66
        $recursive = (bool)$input->getOption('recursive');
67
68
        if (!is_dir($directory)) {
69
            throw new RuntimeException("'$directory' is not a valid directory");
70
        }
71
72
        $action = new GenerateDotFile(new TokenParser(), new GraphvizProcessor($associations));
73
        $action->attach($this->display);
74
75
        $finder = new CodeFinder();
76
        $finder->addDirectory($directory, $recursive);
77
78
        $output->writeln('[|] Running... (This may take some time)');
79
80
        $action->generate($finder, $dotFile);
81
82
        return 0;
83
    }
84
}
85