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\Generators; |
9
|
|
|
|
10
|
|
|
use LogicException; |
11
|
|
|
use PhUml\Parser\CodeFinder; |
12
|
|
|
use PhUml\Parser\CodeParser; |
13
|
|
|
use PhUml\Processors\GraphvizProcessor; |
14
|
|
|
use PhUml\Processors\ImageProcessor; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* It generates a UML class diagram from a directory with PHP code |
18
|
|
|
* |
19
|
|
|
* The image produced is a `.png` that will be saved in a specified path |
20
|
|
|
*/ |
21
|
|
|
class ClassDiagramGenerator extends DigraphGenerator |
22
|
|
|
{ |
23
|
|
|
/** @var ImageProcessor */ |
24
|
|
|
private $imageProcessor; |
25
|
|
|
|
26
|
30 |
|
public function __construct( |
27
|
|
|
CodeParser $parser, |
28
|
|
|
GraphvizProcessor $digraphProcessor, |
29
|
|
|
ImageProcessor $imageProcessor |
30
|
|
|
) { |
31
|
30 |
|
parent::__construct($parser, $digraphProcessor); |
32
|
30 |
|
$this->imageProcessor = $imageProcessor; |
33
|
30 |
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* The process to generate a class diagram is as follows |
37
|
|
|
* |
38
|
|
|
* 1. The parser produces a collection of classes and interfaces |
39
|
|
|
* 2. The `graphviz` processor takes this collection and creates a digraph using the DOT language |
40
|
|
|
* 3. Either the `neato` or `dot` will produce a `.png` class diagram from the digraph |
41
|
|
|
* 4. The image is saved to the given path |
42
|
|
|
* |
43
|
|
|
* @throws LogicException If either the image processor or the command are missing |
44
|
|
|
*/ |
45
|
30 |
|
public function generate(CodeFinder $finder, string $imagePath): void |
46
|
|
|
{ |
47
|
30 |
|
$this->display()->start(); |
48
|
24 |
|
$image = $this->generateClassDiagram($this->generateDigraph($this->parseCode($finder))); |
49
|
24 |
|
$this->save($this->imageProcessor, $image, $imagePath); |
50
|
24 |
|
} |
51
|
|
|
|
52
|
|
|
/** @throws LogicException If no command or image processor is provided */ |
53
|
24 |
|
private function generateClassDiagram(string $digraph): string |
54
|
|
|
{ |
55
|
24 |
|
$this->display()->runningProcessor($this->imageProcessor); |
56
|
24 |
|
return $this->imageProcessor->process($digraph); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|