1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use PhUml\Parser\CodeFinder; |
4
|
|
|
use PhUml\Parser\TokenParser; |
5
|
|
|
use PhUml\Processors\InvalidInitialProcessor; |
6
|
|
|
use PhUml\Processors\InvalidProcessorChain; |
7
|
|
|
|
8
|
|
|
class plPhuml |
9
|
|
|
{ |
10
|
|
|
/** @var TokenParser */ |
11
|
|
|
private $parser; |
12
|
|
|
|
13
|
|
|
/** @var string[] */ |
14
|
|
|
private $files; |
15
|
|
|
|
16
|
|
|
/** @var plProcessor[] */ |
17
|
|
|
private $processors; |
18
|
|
|
|
19
|
|
|
/** @var CodeFinder */ |
20
|
|
|
private $finder; |
21
|
|
|
|
22
|
24 |
|
public function __construct(TokenParser $parser = null, CodeFinder $finder = null) |
23
|
|
|
{ |
24
|
24 |
|
$this->parser = $parser ?? new TokenParser(); |
25
|
24 |
|
$this->finder = $finder ?? new CodeFinder(); |
26
|
24 |
|
$this->processors = []; |
27
|
24 |
|
$this->files = []; |
28
|
24 |
|
} |
29
|
|
|
|
30
|
6 |
|
public function addDirectory(string $directory, bool $recursive = true): void |
31
|
|
|
{ |
32
|
6 |
|
$this->finder->addDirectory($directory, $recursive); |
33
|
6 |
|
} |
34
|
|
|
|
35
|
|
|
/** @return string[] */ |
36
|
6 |
|
public function files(): array |
37
|
|
|
{ |
38
|
6 |
|
return $this->finder->files(); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @throws InvalidInitialProcessor |
43
|
|
|
* @throws InvalidProcessorChain |
44
|
|
|
*/ |
45
|
18 |
|
public function addProcessor(plProcessor $processor): void |
46
|
|
|
{ |
47
|
18 |
|
if (count($this->processors) === 0 && !$processor->isInitial()) { |
48
|
6 |
|
throw InvalidInitialProcessor::given($processor); |
49
|
|
|
} |
50
|
12 |
|
$lastProcessor = end($this->processors); |
51
|
12 |
|
if (count($this->processors) > 0 && !$lastProcessor->isCompatibleWith($processor)) { |
52
|
12 |
|
throw InvalidProcessorChain::with($lastProcessor, $processor); |
53
|
|
|
} |
54
|
12 |
|
$this->processors[] = $processor; |
55
|
12 |
|
} |
56
|
|
|
|
57
|
|
|
public function generate($outfile): void |
58
|
|
|
{ |
59
|
|
|
echo "[|] Parsing class structure\n"; |
60
|
|
|
$structure = $this->parser->parse($this->finder); |
61
|
|
|
|
62
|
|
|
$input = $structure; |
63
|
|
|
foreach ($this->processors as $processor) { |
64
|
|
|
echo "[|] Running '{$processor->name()}' processor\n"; |
65
|
|
|
$input = $processor->process($input); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
echo "[|] Writing generated data to disk\n"; |
69
|
|
|
end($this->processors)->writeToDisk($input, $outfile); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|