Skip to content

Commit 484d129

Browse files
committed
Modified to display external packages at the lower level in the package relation diagram.
1 parent 6cfbafb commit 484d129

File tree

14 files changed

+86
-49
lines changed

14 files changed

+86
-49
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/vendor/
22
*.phar
33
*.un~
4-
composer.lock
4+
composer.lock
5+
.vscode

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# CHANGELOG
22

3+
### Features
4+
5+
* Modified to display external packages at the lower level in the package relation diagram.
6+
37
### Bug fix
48

59
* Method's Parameter Dependencies added to arrows.

dogfood-model.png

-6.43 KB
Loading

dogfood-package.png

12.9 KB
Loading

dogfood.png

4.03 KB
Loading

src/DiagramElement/ExternalPackage/PackageHierarchy.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public function register(array $packages): void {
6666

6767
public function dump(int $indent): string {
6868
$lines = [];
69-
$lines[] = sprintf('%spackage %s as %s {', str_repeat(' ', $indent), $this->name, $this->name);
69+
$lines[] = sprintf('%spackage %s as %s #DDDDDD {', str_repeat(' ', $indent), $this->name, $this->name);
7070
foreach ($this->children as $c) {
7171
$lines[] = $c->dump($indent + 1);
7272
}

src/DiagramElement/Package.php

Lines changed: 43 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
class Package {
77
private Options $options;
88

9+
/** @var string[] */
910
public array $parents;
1011
public string $name;
1112
public string $package = '';
@@ -28,7 +29,7 @@ public function getLogicalName(): string {
2829
public function addEntry(array $paths, Entry $entry): string {
2930
if (count($paths) === 0) {
3031
if (empty($this->package)) {
31-
$this->package = implode('.', $entry->getClass()->getClassType()->getNamespace());
32+
$this->package = implode('.', $entry->getClass()->getNamespace());
3233
}
3334
$this->entries[] = $entry;
3435
return $this->package;
@@ -85,26 +86,22 @@ public function dump($level = 0): array {
8586
public function dumpPackages($level = 1): array {
8687
$indent = str_repeat(' ', $level);
8788
$lines = [];
88-
// if ($level == 1) {
89-
// $lines[] = sprintf(
90-
// '%spackage %s as %s {',
91-
// $indent,
92-
// $this->name === 'ROOT' ? (empty($this->package) ? 'ROOT': $this->package) : $this->name,
93-
// $this->getLogicalName()
94-
// );
95-
// } else {
89+
$target = empty($this->package) ? $this->getLogicalName() : $this->package;
90+
$targetElements = explode('.', $target);
91+
if ($level > 1) {
9692
$lines[] = sprintf(
9793
'%spackage %s {',
9894
$indent,
99-
$this->package
95+
end($targetElements)
10096
);
101-
// }
102-
// $lines[] = sprintf(
103-
// '%spackage %s as %s {',
104-
// $indent,
105-
// $this->name === 'ROOT' ? (empty($this->package) ? 'ROOT': $this->package) : $this->name,
106-
// $this->getLogicalName()
107-
// );
97+
} else {
98+
$lines[] = sprintf(
99+
'%spackage %s as %s {',
100+
$indent,
101+
$target,
102+
end($targetElements)
103+
);
104+
}
108105
foreach ($this->children as $n) {
109106
$lines = array_merge($lines, $n->dumpPackages($level + 1));
110107
}
@@ -145,7 +142,8 @@ public function getUses($acc): array {
145142
foreach ($this->entries as $e) {
146143
$uses = array_merge($uses, $e->getClass()->getUses());
147144
}
148-
$acc[$this->package] = $uses;
145+
$package = empty($this->package) ? sprintf('%s.%s', implode('.', $this->parents), $this->name) : $this->package;
146+
$acc[$package] = $uses;
149147
foreach ($this->children as $n) {
150148
$acc = array_merge($acc, $n->getUses($acc));
151149
}
@@ -162,4 +160,31 @@ public function getTargetPackages($acc = []) {
162160
}
163161
return $acc;
164162
}
163+
164+
public function findPackage(string $package): ?Package {
165+
return $this->recFindPackage($package);
166+
}
167+
168+
public function is(string $package): bool {
169+
$segments = $this->parents;
170+
$segments[] = $this->name;
171+
return implode('.', $segments) === $package;
172+
}
173+
174+
/**
175+
* @param string $package パッケージの表記(例: hoge.fuga)
176+
* @return ?Package
177+
*/
178+
private function recFindPackage(string $package): ?Package {
179+
if ($this->is($package)) {
180+
return $this;
181+
}
182+
foreach ($this->children as $c) {
183+
$p = $c->recFindPackage($package);
184+
if ( ! empty($p)) {
185+
return $p;
186+
}
187+
}
188+
return null;
189+
}
165190
}

src/DiagramElement/PackageArrow.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ public function toString(): string {
2929
$format = $this->bothSideArrow
3030
? ' %s <-[#red,plain,thickness=4]-> %s'
3131
: ' %s --> %s';
32-
return sprintf($format, $this->from, $this->to);
32+
$fromElements = explode('.', $this->from);
33+
$toElements = explode('.', $this->to);
34+
return sprintf($format, end($fromElements), end($toElements));
3335
}
3436
}

src/DiagramElement/PackageRelations.php

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,51 +7,52 @@
77
class PackageRelations {
88
/** @var array<string, \Smeghead\PhpClassDiagram\Php\PhpType[]> */
99
private array $uses;
10-
/** @var \Smeghead\PhpClassDiagram\Php\PhpType[] */
11-
private array $targetPackages;
10+
private Package $rootPackage;
1211

1312
/**
1413
* @param array<string, \Smeghead\PhpClassDiagram\Php\PhpType[]> $uses
14+
* @param Package $rootPackage
1515
*/
16-
public function __construct(array $uses, array $targetPackages) {
16+
public function __construct(array $uses, Package $rootPackage) {
1717
$this->uses = $uses;
18-
$this->targetPackages = $targetPackages;
18+
$this->rootPackage = $rootPackage;
1919
}
2020

21-
private function displayPackage($package) {
22-
if (empty($package)) {
23-
return $this->targetPackages[$package];
21+
private function displayPackage(string $package): string {
22+
$p = $this->rootPackage->findPackage($package);
23+
if ( ! empty($p)) {
24+
if (empty($p->package)) {
25+
return $p->getLogicalName();
26+
}
2427
}
25-
return $package;
26-
// if (in_array($package, array_keys($this->targetPackages))) {
27-
// return $this->targetPackages[$package]; // 解析対象のpackageはディレクトリ名で表示
28-
// } else {
29-
// return $package; //外部のpackageはpackage表示
30-
// }
28+
return $package; //外部のpackageはpackage表示
3129
}
3230

3331
public function getArrows(): array {
3432
$lines = [];
3533
$all = [];
3634
$packageRelations = [];
3735
foreach ($this->uses as $namespace => $us) {
38-
$packages = array_unique(array_map(function(PhpType $x){
36+
$packageNames = array_unique(array_map(function(PhpType $x){
3937
return implode('.', $x->getNamespace());
4038
}, $us));
4139
// 対象となっているpackage以外のpackageは、即席で定義する必要がある。
42-
$all = array_unique(array_merge($all, $packages));
43-
$packageRelations[$namespace] = array_map(function($x) {
44-
return $this->displayPackage($x, $this->targetPackages);
45-
}, $packages);
40+
$all = array_unique(array_merge($all, $packageNames));
41+
$packageRelations[$namespace] = array_map(function(string $x) {
42+
return $this->displayPackage($x);
43+
}, $packageNames);
4644
}
47-
$externals = array_diff($all, array_keys($this->targetPackages));
45+
$externals = array_diff($all, array_keys($this->rootPackage->getTargetPackages()));
4846
if (count($externals) > 0) {
4947
$externalPackage = new PackageHierarchy($externals);
5048
$lines[] = $externalPackage->dump();
5149
}
5250
$arrows = [];
5351
foreach ($packageRelations as $package => $dependencies) {
54-
$package = $this->displayPackage($package, $this->targetPackages);
52+
$package = $this->displayPackage($package);
53+
if (empty($package)) {
54+
continue;
55+
}
5556
foreach ($dependencies as $d) {
5657
if (empty($d)) {
5758
continue;

src/DiagramElement/Relation.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,7 @@ public function dumpPackages(): array {
5252
$lines = array_merge($lines, $this->options->headers());
5353
$lines = array_merge($lines, $this->package->dumpPackages());
5454
$uses = $this->getUses();
55-
$targetPackages = $this->package->getTargetPackages();
56-
$packageRelations = new PackageRelations($uses, $targetPackages);
55+
$packageRelations = new PackageRelations($uses, $this->package);
5756
$lines = array_merge($lines, $packageRelations->getArrows());
5857
$lines[] = '@enduml';
5958

0 commit comments

Comments
 (0)