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\Actions; |
8
|
|
|
|
9
|
|
|
use PhUml\Parser\CodeFinder; |
10
|
|
|
use PhUml\Parser\TokenParser; |
11
|
|
|
use PhUml\Processors\GraphvizProcessor; |
12
|
|
|
use PhUml\Processors\ImageProcessor; |
13
|
|
|
use LogicException; |
14
|
|
|
|
15
|
|
|
class GenerateClassDiagram |
16
|
|
|
{ |
17
|
|
|
/** @var TokenParser */ |
18
|
|
|
private $parser; |
19
|
|
|
|
20
|
|
|
/** @var GraphvizProcessor */ |
21
|
|
|
private $dotProcessor; |
22
|
|
|
|
23
|
|
|
/** @var ImageProcessor */ |
24
|
|
|
private $imageProcessor; |
25
|
|
|
|
26
|
|
|
/** @var CanGenerateClassDiagram */ |
27
|
|
|
private $command; |
28
|
|
|
|
29
|
15 |
|
public function __construct(TokenParser $parser, GraphvizProcessor $dotProcessor) { |
30
|
15 |
|
$this->parser = $parser; |
31
|
15 |
|
$this->dotProcessor = $dotProcessor; |
32
|
15 |
|
} |
33
|
|
|
|
34
|
12 |
|
public function attach(CanGenerateClassDiagram $action): void |
35
|
|
|
{ |
36
|
12 |
|
$this->command = $action; |
37
|
12 |
|
} |
38
|
|
|
|
39
|
6 |
|
public function setImageProcessor(ImageProcessor $imageProcessor): void |
40
|
|
|
{ |
41
|
6 |
|
$this->imageProcessor = $imageProcessor; |
42
|
6 |
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @throws LogicException If either the image processor or the command are missing |
46
|
|
|
*/ |
47
|
12 |
|
public function generate(CodeFinder $finder, string $imagePath): void |
48
|
|
|
{ |
49
|
12 |
|
$this->command()->runningParser(); |
50
|
9 |
|
$structure = $this->parser->parse($finder); |
51
|
9 |
|
$this->command()->runningProcessor($this->dotProcessor); |
52
|
9 |
|
$dotLanguage = $this->dotProcessor->process($structure); |
53
|
9 |
|
$this->command()->runningProcessor($this->imageProcessor()); |
54
|
6 |
|
$image = $this->imageProcessor()->process($dotLanguage); |
55
|
6 |
|
$this->command()->savingResult(); |
56
|
6 |
|
$this->imageProcessor->writeToDisk($image, $imagePath); |
57
|
6 |
|
} |
58
|
|
|
|
59
|
|
|
/** @throws LogicException */ |
60
|
12 |
|
private function command(): CanGenerateClassDiagram |
61
|
|
|
{ |
62
|
12 |
|
if (!$this->command) { |
63
|
3 |
|
throw new LogicException('No command was attached'); |
64
|
|
|
} |
65
|
9 |
|
return $this->command; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** @throws LogicException */ |
69
|
9 |
|
private function imageProcessor(): ImageProcessor |
70
|
|
|
{ |
71
|
9 |
|
if (!$this->imageProcessor) { |
72
|
3 |
|
throw new LogicException('No image processor was provided'); |
73
|
|
|
} |
74
|
6 |
|
return $this->imageProcessor; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|