|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace phpDocumentor\Guides\ReferenceResolvers; |
| 6 | + |
| 7 | +use phpDocumentor\Guides\Intersphinx\InventoryRepository; |
| 8 | +use phpDocumentor\Guides\Nodes\Inline\CrossReferenceNode; |
| 9 | +use phpDocumentor\Guides\Nodes\Inline\DocReferenceNode; |
| 10 | +use phpDocumentor\Guides\Nodes\Inline\LinkInlineNode; |
| 11 | +use phpDocumentor\Guides\RenderContext; |
| 12 | + |
| 13 | +use function explode; |
| 14 | +use function str_contains; |
| 15 | + |
| 16 | +class IntersphinxReferenceResolver implements ReferenceResolver |
| 17 | +{ |
| 18 | + public final const PRIORITY = 50; |
| 19 | + |
| 20 | + public function __construct(private InventoryRepository $inventoryRepository) |
| 21 | + { |
| 22 | + } |
| 23 | + |
| 24 | + public function resolve(LinkInlineNode $node, RenderContext $renderContext): bool |
| 25 | + { |
| 26 | + if (!$node instanceof CrossReferenceNode || !str_contains($node->getTargetReference(), ':')) { |
| 27 | + return false; |
| 28 | + } |
| 29 | + |
| 30 | + [$domain, $target] = explode(':', $node->getTargetReference(), 2); |
| 31 | + if (!$this->inventoryRepository->hasInventory($domain)) { |
| 32 | + return false; |
| 33 | + } |
| 34 | + |
| 35 | + $inventory = $this->inventoryRepository->getInventory($domain); |
| 36 | + $group = $node instanceof DocReferenceNode ? 'std:doc' : 'std:label'; |
| 37 | + if (!$inventory->hasInventoryGroup($group)) { |
| 38 | + return false; |
| 39 | + } |
| 40 | + |
| 41 | + $inventoryGroup = $inventory->getInventory($group); |
| 42 | + if (!$inventoryGroup->hasLink($target)) { |
| 43 | + return false; |
| 44 | + } |
| 45 | + |
| 46 | + $link = $inventory->getLink($group, $target); |
| 47 | + |
| 48 | + $node->setUrl($inventory->getBaseUrl() . $link->getPath()); |
| 49 | + if ($node->getValue() === '') { |
| 50 | + $node->setValue($link->getTitle()); |
| 51 | + } |
| 52 | + |
| 53 | + return false; |
| 54 | + } |
| 55 | + |
| 56 | + public static function getPriority(): int |
| 57 | + { |
| 58 | + return self::PRIORITY; |
| 59 | + } |
| 60 | +} |
0 commit comments