|
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
|
|
|
namespace PhUml\Console\Commands; |
|
8
|
|
|
|
|
9
|
|
|
use PhUml\Actions\CanGenerateStatistics; |
|
10
|
|
|
use PhUml\Actions\GenerateStatistics; |
|
11
|
|
|
use PhUml\Parser\CodeFinder; |
|
12
|
|
|
use PhUml\Parser\TokenParser; |
|
13
|
|
|
use PhUml\Processors\StatisticsProcessor; |
|
14
|
|
|
use RuntimeException; |
|
15
|
|
|
use Symfony\Component\Console\Command\Command; |
|
16
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
17
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
18
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
19
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
20
|
|
|
|
|
21
|
|
|
class GenerateStatisticsCommand extends Command implements CanGenerateStatistics |
|
22
|
|
|
{ |
|
23
|
|
|
/** @var OutputInterface */ |
|
24
|
|
|
private $output; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @throws \Symfony\Component\Console\Exception\InvalidArgumentException |
|
28
|
|
|
*/ |
|
29
|
24 |
|
protected function configure() |
|
30
|
|
|
{ |
|
31
|
|
|
$this |
|
32
|
24 |
|
->setName('phuml:statistics') |
|
33
|
24 |
|
->setDescription('Generate statistics about the code of a given directory') |
|
34
|
24 |
|
->setHelp(<<<HELP |
|
35
|
24 |
|
Example: |
|
36
|
|
|
php bin/phuml phuml:statistics -r ./src statistics.txt |
|
37
|
|
|
|
|
38
|
|
|
This example will scan the `./src` directory recursively for php files. |
|
39
|
|
|
It will generate the statistics and save them to the file `statistics.txt`. |
|
40
|
|
|
HELP |
|
41
|
|
|
) |
|
42
|
24 |
|
->addOption( |
|
43
|
24 |
|
'recursive', |
|
44
|
24 |
|
'r', |
|
45
|
24 |
|
InputOption::VALUE_NONE, |
|
46
|
24 |
|
'Look for classes in the given directory recursively' |
|
47
|
|
|
) |
|
48
|
24 |
|
->addArgument( |
|
49
|
24 |
|
'directory', |
|
50
|
24 |
|
InputArgument::REQUIRED, |
|
51
|
24 |
|
'The directory to be scanned to generate the class diagram' |
|
52
|
|
|
) |
|
53
|
24 |
|
->addArgument( |
|
54
|
24 |
|
'output', |
|
55
|
24 |
|
InputArgument::REQUIRED, |
|
56
|
24 |
|
'The file name for your statistics file' |
|
57
|
|
|
) |
|
58
|
|
|
; |
|
59
|
24 |
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @throws \LogicException |
|
63
|
|
|
* @throws \RuntimeException |
|
64
|
|
|
* @throws \Symfony\Component\Console\Exception\InvalidArgumentException |
|
65
|
|
|
*/ |
|
66
|
6 |
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
67
|
|
|
{ |
|
68
|
6 |
|
$this->output = $output; |
|
69
|
|
|
|
|
70
|
6 |
|
$directory = $input->getArgument('directory'); |
|
71
|
6 |
|
$statisticsFile = $input->getArgument('output'); |
|
72
|
6 |
|
$recursive = (bool)$input->getOption('recursive'); |
|
73
|
|
|
|
|
74
|
6 |
|
if (!is_dir($directory)) { |
|
75
|
3 |
|
throw new RuntimeException("'$directory' is not a valid directory"); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
3 |
|
$action = new GenerateStatistics(new TokenParser(), new StatisticsProcessor()); |
|
79
|
3 |
|
$action->attach($this); |
|
80
|
|
|
|
|
81
|
3 |
|
$finder = new CodeFinder(); |
|
82
|
3 |
|
$finder->addDirectory($directory, $recursive); |
|
83
|
|
|
|
|
84
|
3 |
|
$output->writeln('[|] Running... (This may take some time)'); |
|
85
|
|
|
|
|
86
|
3 |
|
$action->generate($finder, $statisticsFile); |
|
87
|
|
|
|
|
88
|
3 |
|
return 0; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
3 |
|
public function runningParser(): void |
|
92
|
|
|
{ |
|
93
|
3 |
|
$this->output->writeln('[|] Parsing class structure'); |
|
94
|
3 |
|
} |
|
95
|
|
|
|
|
96
|
3 |
|
public function runningProcessor(StatisticsProcessor $processor): void |
|
97
|
|
|
{ |
|
98
|
3 |
|
$this->output->writeln("[|] Running '{$processor->name()}' processor"); |
|
99
|
3 |
|
} |
|
100
|
|
|
|
|
101
|
3 |
|
public function savingResult(): void |
|
102
|
|
|
{ |
|
103
|
3 |
|
$this->output->writeln('[|] Writing generated data to disk'); |
|
104
|
3 |
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|