|
4 | 4 |
|
5 | 5 | namespace Smeghead\PhpClassDiagram;
|
6 | 6 |
|
7 |
| -use Symfony\Component\Finder\Finder; |
| 7 | +use RuntimeException; |
8 | 8 | use Smeghead\PhpClassDiagram\Config\Options;
|
9 |
| -use Smeghead\PhpClassDiagram\DiagramElement\{ |
10 |
| - Entry, |
11 |
| - Relation, |
12 |
| -}; |
| 9 | +use Smeghead\PhpClassDiagram\DiagramElement\Entry; |
| 10 | +use Smeghead\PhpClassDiagram\DiagramElement\Relation; |
13 | 11 | use Smeghead\PhpClassDiagram\Php\PhpReader;
|
| 12 | +use Symfony\Component\Finder\Finder; |
14 | 13 |
|
15 | 14 | final class Main
|
16 | 15 | {
|
17 |
| - const VERSION = 'v1.3.0'; |
| 16 | + public const VERSION = 'v1.3.0'; |
| 17 | + |
| 18 | + public function __construct( |
| 19 | + private string $directory, |
| 20 | + private Options $options, |
| 21 | + ) { |
| 22 | + } |
18 | 23 |
|
19 |
| - public function __construct(string $directory, Options $options) |
| 24 | + public function run(): void |
| 25 | + { |
| 26 | + $finder = $this->createFinder(); |
| 27 | + $entries = $this->findEntries($finder); |
| 28 | + $this->renderEntries($entries); |
| 29 | + } |
| 30 | + |
| 31 | + private function createFinder(): Finder |
20 | 32 | {
|
21 | 33 | $finder = new Finder();
|
22 |
| - $finder->files()->in($directory); |
23 |
| - $finder->files()->name($options->includes()); |
24 |
| - $excludes = $options->excludes(); |
| 34 | + $finder->files()->in($this->directory); |
| 35 | + $finder->files()->name($this->options->includes()); |
| 36 | + $excludes = $this->options->excludes(); |
| 37 | + |
25 | 38 | if (count($excludes) > 0) {
|
26 | 39 | $finder->files()->notName($excludes)->notPath($excludes);
|
27 | 40 | }
|
| 41 | + |
| 42 | + return $finder; |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * @return list<Entry> |
| 47 | + */ |
| 48 | + private function findEntries(Finder $finder): array |
| 49 | + { |
28 | 50 | $entries = [];
|
29 | 51 | foreach ($finder as $file) {
|
30 | 52 | try {
|
31 |
| - $reflections = PhpReader::parseFile(realpath($directory), $file->getRealPath(), $options); |
| 53 | + $reflections = PhpReader::parseFile( |
| 54 | + realpath($this->directory), |
| 55 | + $file->getRealPath(), |
| 56 | + $this->options |
| 57 | + ); |
32 | 58 | foreach ($reflections as $reflection) {
|
33 |
| - $entries[] = new Entry($file->getRelativePath(), $reflection->getInfo(), $options); |
| 59 | + $entries[] = new Entry($file->getRelativePath(), $reflection->getInfo(), $this->options); |
34 | 60 | }
|
35 | 61 | } catch (\Exception $e) {
|
36 |
| - fputs(STDERR, $e->getMessage() . "\r\n"); |
| 62 | + fwrite(STDERR, $e->getMessage() . "\r\n"); |
37 | 63 | }
|
38 | 64 | }
|
39 |
| - $relation = new Relation($entries, $options); |
40 |
| - switch ($options->diagram()) { |
41 |
| - case Options::DIAGRAM_CLASS: |
42 |
| - echo implode("\r\n", $relation->dump()) . "\r\n"; |
43 |
| - break; |
44 |
| - case OPTIONS::DIAGRAM_PACKAGE: |
45 |
| - echo implode("\r\n", $relation->dumpPackages()) . "\r\n"; |
46 |
| - break; |
47 |
| - case OPTIONS::DIAGRAM_JIG: |
48 |
| - echo implode("\r\n", $relation->dump()) . "\r\n"; |
49 |
| - echo implode("\r\n", $relation->dumpPackages()) . "\r\n"; |
50 |
| - echo implode("\r\n", $relation->dumpDivisions()) . "\r\n"; |
51 |
| - break; |
52 |
| - case OPTIONS::DIAGRAM_DIVSION: |
53 |
| - echo implode("\r\n", $relation->dumpDivisions()) . "\r\n"; |
54 |
| - break; |
55 |
| - default: |
56 |
| - throw new \Exception('invalid diagram.'); |
57 |
| - } |
| 65 | + return $entries; |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * @param list<Entry> $entries |
| 70 | + */ |
| 71 | + private function renderEntries(array $entries): void |
| 72 | + { |
| 73 | + $relation = new Relation($entries, $this->options); |
| 74 | + |
| 75 | + match ($this->options->diagram()) { |
| 76 | + Options::DIAGRAM_CLASS => $this->renderDiagramClass($relation), |
| 77 | + OPTIONS::DIAGRAM_PACKAGE => $this->renderDiagramPackage($relation), |
| 78 | + OPTIONS::DIAGRAM_JIG => $this->renderDiagramJig($relation), |
| 79 | + OPTIONS::DIAGRAM_DIVISION => $this->renderDiagramDivision($relation), |
| 80 | + default => throw new RuntimeException('invalid diagram.') |
| 81 | + }; |
| 82 | + } |
| 83 | + |
| 84 | + private function renderDiagramClass(Relation $relation): void |
| 85 | + { |
| 86 | + echo implode("\r\n", $relation->dump()) . "\r\n"; |
| 87 | + } |
| 88 | + |
| 89 | + private function renderDiagramPackage(Relation $relation): void |
| 90 | + { |
| 91 | + echo implode("\r\n", $relation->dumpPackages()) . "\r\n"; |
| 92 | + } |
| 93 | + |
| 94 | + private function renderDiagramJig(Relation $relation): void |
| 95 | + { |
| 96 | + echo implode("\r\n", $relation->dump()) . "\r\n"; |
| 97 | + echo implode("\r\n", $relation->dumpPackages()) . "\r\n"; |
| 98 | + echo implode("\r\n", $relation->dumpDivisions()) . "\r\n"; |
| 99 | + } |
| 100 | + |
| 101 | + private function renderDiagramDivision(Relation $relation): void |
| 102 | + { |
| 103 | + echo implode("\r\n", $relation->dumpDivisions()) . "\r\n"; |
58 | 104 | }
|
59 | 105 | }
|
0 commit comments