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
|
|
|
protected function configure() |
30
|
|
|
{ |
31
|
|
|
$this |
32
|
|
|
->setName('phuml:statistics') |
33
|
|
|
->setDescription('Generate statistics about the code of a given directory') |
34
|
|
|
->setHelp(<<<HELP |
35
|
|
|
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
|
|
|
->addOption( |
43
|
|
|
'recursive', |
44
|
|
|
'r', |
45
|
|
|
InputOption::VALUE_NONE, |
46
|
|
|
'Look for classes in the given directory recursively' |
47
|
|
|
) |
48
|
|
|
->addArgument( |
49
|
|
|
'directory', |
50
|
|
|
InputArgument::REQUIRED, |
51
|
|
|
'The directory to be scanned to generate the class diagram' |
52
|
|
|
) |
53
|
|
|
->addArgument( |
54
|
|
|
'output', |
55
|
|
|
InputArgument::REQUIRED, |
56
|
|
|
'The file name for your statistics file' |
57
|
|
|
) |
58
|
|
|
; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @throws \LogicException |
63
|
|
|
* @throws \RuntimeException |
64
|
|
|
* @throws \Symfony\Component\Console\Exception\InvalidArgumentException |
65
|
|
|
*/ |
66
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
67
|
|
|
{ |
68
|
|
|
$this->output = $output; |
69
|
|
|
|
70
|
|
|
$directory = $input->getArgument('directory'); |
71
|
|
|
$statisticsFile = $input->getArgument('output'); |
72
|
|
|
$recursive = (bool)$input->getOption('recursive'); |
73
|
|
|
|
74
|
|
|
if (!is_dir($directory)) { |
75
|
|
|
throw new RuntimeException("'$directory' is not a valid directory"); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$action = new GenerateStatistics(new TokenParser(), new StatisticsProcessor()); |
79
|
|
|
$action->attach($this); |
80
|
|
|
|
81
|
|
|
$finder = new CodeFinder(); |
82
|
|
|
$finder->addDirectory($directory, $recursive); |
83
|
|
|
|
84
|
|
|
$output->writeln('[|] Running... (This may take some time)'); |
85
|
|
|
|
86
|
|
|
$action->generate($finder, $statisticsFile); |
87
|
|
|
|
88
|
|
|
return 0; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function runningParser(): void |
92
|
|
|
{ |
93
|
|
|
$this->output->writeln('[|] Parsing class structure'); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function runningProcessor(StatisticsProcessor $processor): void |
97
|
|
|
{ |
98
|
|
|
$this->output->writeln("[|] Running '{$processor->name()}' processor"); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function savingResult(): void |
102
|
|
|
{ |
103
|
|
|
$this->output->writeln('[|] Writing generated data to disk'); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|