Skip to content

Commit 957eb43

Browse files
linawolfjaapio
authored andcommitted
!!![TASK] Remove Exceptions and use message system for inventories
1 parent 655b960 commit 957eb43

File tree

16 files changed

+117
-169
lines changed

16 files changed

+117
-169
lines changed

packages/guides/src/Interlink/DefaultInventoryRepository.php

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,15 @@
44

55
namespace phpDocumentor\Guides\Interlink;
66

7-
use phpDocumentor\Guides\Interlink\Exception\InterlinkInventoryNotFound;
8-
use phpDocumentor\Guides\Interlink\Exception\InterlinkNotFound;
7+
use phpDocumentor\Guides\Nodes\Inline\CrossReferenceNode;
98
use phpDocumentor\Guides\ReferenceResolvers\AnchorNormalizer;
9+
use phpDocumentor\Guides\ReferenceResolvers\Message;
10+
use phpDocumentor\Guides\ReferenceResolvers\Messages;
11+
use phpDocumentor\Guides\RenderContext;
1012

1113
use function array_key_exists;
14+
use function array_merge;
15+
use function sprintf;
1216

1317
final class DefaultInventoryRepository implements InventoryRepository
1418
{
@@ -26,13 +30,12 @@ public function __construct(
2630
}
2731
}
2832

29-
/** @throws InterlinkNotFound */
30-
public function getLink(string $inventoryKey, string $groupKey, string $linkKey): InventoryLink
33+
public function getLink(CrossReferenceNode $node, RenderContext $renderContext, Messages $messages): InventoryLink|null
3134
{
32-
$inventory = $this->getInventory($inventoryKey);
33-
$group = $inventory->getGroup($groupKey);
35+
$inventory = $this->getInventory($node, $renderContext, $messages);
36+
$group = $inventory?->getGroup($node, $renderContext, $messages);
3437

35-
return $group->getLink($linkKey);
38+
return $group?->getLink($node, $renderContext, $messages);
3639
}
3740

3841
public function hasInventory(string $key): bool
@@ -42,12 +45,21 @@ public function hasInventory(string $key): bool
4245
return array_key_exists($reducedKey, $this->inventories);
4346
}
4447

45-
/** @throws InterlinkInventoryNotFound */
46-
public function getInventory(string $key): Inventory
48+
public function getInventory(CrossReferenceNode $node, RenderContext $renderContext, Messages $messages): Inventory|null
4749
{
48-
$reducedKey = $this->anchorNormalizer->reduceAnchor($key);
50+
$reducedKey = $this->anchorNormalizer->reduceAnchor($node->getInterlinkDomain());
4951
if (!$this->hasInventory($reducedKey)) {
50-
throw new InterlinkInventoryNotFound('Inventory with key ' . $reducedKey . ' not found. ', 1_671_398_986);
52+
$messages->addWarning(
53+
new Message(
54+
sprintf(
55+
'Inventory with key %s not found. ',
56+
$node->getInterlinkDomain(),
57+
),
58+
array_merge($renderContext->getLoggerInformation(), $node->getDebugInformation()),
59+
),
60+
);
61+
62+
return null;
5163
}
5264

5365
$this->inventoryLoader->loadInventory($this->inventories[$reducedKey]);

packages/guides/src/Interlink/Exception/InterlinkGroupNotFound.php

Lines changed: 0 additions & 9 deletions
This file was deleted.

packages/guides/src/Interlink/Exception/InterlinkInventoryNotFound.php

Lines changed: 0 additions & 9 deletions
This file was deleted.

packages/guides/src/Interlink/Exception/InterlinkNotFound.php

Lines changed: 0 additions & 11 deletions
This file was deleted.

packages/guides/src/Interlink/Exception/InterlinkTargetNotFound.php

Lines changed: 0 additions & 9 deletions
This file was deleted.

packages/guides/src/Interlink/Inventory.php

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,14 @@
44

55
namespace phpDocumentor\Guides\Interlink;
66

7-
use phpDocumentor\Guides\Interlink\Exception\InterlinkGroupNotFound;
8-
use phpDocumentor\Guides\Interlink\Exception\InterlinkNotFound;
7+
use phpDocumentor\Guides\Nodes\Inline\CrossReferenceNode;
98
use phpDocumentor\Guides\ReferenceResolvers\AnchorNormalizer;
9+
use phpDocumentor\Guides\ReferenceResolvers\Message;
10+
use phpDocumentor\Guides\ReferenceResolvers\Messages;
11+
use phpDocumentor\Guides\RenderContext;
1012

1113
use function array_key_exists;
14+
use function array_merge;
1215
use function sprintf;
1316

1417
final class Inventory
@@ -39,25 +42,26 @@ public function getGroups(): array
3942
return $this->groups;
4043
}
4144

42-
/** @throws InterlinkNotFound */
43-
public function getGroup(string $key): InventoryGroup
45+
public function getGroup(CrossReferenceNode $node, RenderContext $renderContext, Messages $messages): InventoryGroup|null
4446
{
45-
$reducedKey = $this->anchorNormalizer->reduceAnchor($key);
47+
$reducedKey = $this->anchorNormalizer->reduceAnchor($node->getInterlinkGroup());
4648
if (!$this->hasGroup($reducedKey)) {
47-
throw new InterlinkGroupNotFound(
48-
sprintf('Inventory group with key "%s" (%s) not found. ', $key, $reducedKey),
49-
1_671_398_986,
50-
);
49+
$messages->addWarning(new Message(
50+
sprintf(
51+
'Inventory group with key "%s" (%s) not found in inventory %s. ',
52+
$node->getInterlinkGroup(),
53+
$reducedKey,
54+
$this->baseUrl,
55+
),
56+
array_merge($renderContext->getLoggerInformation(), $node->getDebugInformation()),
57+
));
58+
59+
return null;
5160
}
5261

5362
return $this->groups[$reducedKey];
5463
}
5564

56-
public function getLink(string $groupKey, string $key): InventoryLink
57-
{
58-
return $this->getGroup($groupKey)->getLink($key);
59-
}
60-
6165
public function hasGroup(string $key): bool
6266
{
6367
$reducedKey = $this->anchorNormalizer->reduceAnchor($key);

packages/guides/src/Interlink/InventoryGroup.php

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,14 @@
44

55
namespace phpDocumentor\Guides\Interlink;
66

7-
use phpDocumentor\Guides\Interlink\Exception\InterlinkNotFound;
8-
use phpDocumentor\Guides\Interlink\Exception\InterlinkTargetNotFound;
7+
use phpDocumentor\Guides\Nodes\Inline\CrossReferenceNode;
98
use phpDocumentor\Guides\ReferenceResolvers\AnchorNormalizer;
9+
use phpDocumentor\Guides\ReferenceResolvers\Message;
10+
use phpDocumentor\Guides\ReferenceResolvers\Messages;
11+
use phpDocumentor\Guides\RenderContext;
1012

1113
use function array_key_exists;
14+
use function array_merge;
1215
use function sprintf;
1316

1417
final class InventoryGroup
@@ -33,12 +36,23 @@ public function hasLink(string $key): bool
3336
return array_key_exists($reducedKey, $this->links);
3437
}
3538

36-
/** @throws InterlinkNotFound */
37-
public function getLink(string $key): InventoryLink
39+
public function getLink(CrossReferenceNode $node, RenderContext $renderContext, Messages $messages): InventoryLink|null
3840
{
39-
$reducedKey = $this->anchorNormalizer->reduceAnchor($key);
41+
$reducedKey = $this->anchorNormalizer->reduceAnchor($node->getTargetReference());
4042
if (!array_key_exists($reducedKey, $this->links)) {
41-
throw new InterlinkTargetNotFound(sprintf('Inventory link with key "%s" (%s) not found. ', $key, $reducedKey), 1_671_398_986);
43+
$messages->addWarning(
44+
new Message(
45+
sprintf(
46+
'Inventory link with key "%s:%s" (%s) not found. ',
47+
$node->getInterlinkDomain(),
48+
$node->getTargetReference(),
49+
$reducedKey,
50+
),
51+
array_merge($renderContext->getLoggerInformation(), $node->getDebugInformation()),
52+
),
53+
);
54+
55+
return null;
4256
}
4357

4458
return $this->links[$reducedKey];

packages/guides/src/Interlink/InventoryRepository.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@
44

55
namespace phpDocumentor\Guides\Interlink;
66

7-
use phpDocumentor\Guides\Interlink\Exception\InterlinkNotFound;
7+
use phpDocumentor\Guides\Nodes\Inline\CrossReferenceNode;
8+
use phpDocumentor\Guides\ReferenceResolvers\Messages;
9+
use phpDocumentor\Guides\RenderContext;
810

911
interface InventoryRepository
1012
{
11-
/** @throws InterlinkNotFound */
12-
public function getLink(string $inventoryKey, string $groupKey, string $linkKey): InventoryLink;
13+
public function getLink(CrossReferenceNode $node, RenderContext $renderContext, Messages $messages): InventoryLink|null;
1314

1415
public function hasInventory(string $key): bool;
1516

16-
public function getInventory(string $key): Inventory;
17+
public function getInventory(CrossReferenceNode $node, RenderContext $renderContext, Messages $messages): Inventory|null;
1718
}

packages/guides/src/Nodes/Inline/CrossReferenceNode.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,6 @@
1414
interface CrossReferenceNode extends LinkInlineNode
1515
{
1616
public function getInterlinkDomain(): string;
17+
18+
public function getInterlinkGroup(): string;
1719
}

packages/guides/src/Nodes/Inline/DocReferenceNode.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,9 @@ public function getDebugInformation(): array
4040
'interlinkDomain' => $this->getInterlinkDomain(),
4141
]);
4242
}
43+
44+
public function getInterlinkGroup(): string
45+
{
46+
return 'std:doc';
47+
}
4348
}

0 commit comments

Comments
 (0)