|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * This file is part of phpDocumentor. |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + * |
| 11 | + * @link https://phpdoc.org |
| 12 | + */ |
| 13 | + |
| 14 | +namespace phpDocumentor\Guides\ReferenceResolvers\Interlink; |
| 15 | + |
| 16 | +use Doctrine\Deprecations\Deprecation; |
| 17 | +use phpDocumentor\Guides\Nodes\Inline\CrossReferenceNode; |
| 18 | +use phpDocumentor\Guides\ReferenceResolvers\Message; |
| 19 | +use phpDocumentor\Guides\ReferenceResolvers\Messages; |
| 20 | +use phpDocumentor\Guides\RenderContext; |
| 21 | + |
| 22 | +use function array_key_exists; |
| 23 | +use function array_merge; |
| 24 | +use function sprintf; |
| 25 | + |
| 26 | +final class ChainedInventoryLinkResolver implements InventoryLinkResolver |
| 27 | +{ |
| 28 | + /** @var array<string, InventoryRepository|null> */ |
| 29 | + private array $cachedRepositories = []; |
| 30 | + |
| 31 | + /** @param iterable<InventoryRepository> $repositories */ |
| 32 | + public function __construct( |
| 33 | + private readonly iterable $repositories, |
| 34 | + ) { |
| 35 | + } |
| 36 | + |
| 37 | + public function getLink(CrossReferenceNode $node, RenderContext $renderContext, Messages $messages): InventoryLink|null |
| 38 | + { |
| 39 | + return $this->resolveInventoryLink($node, $renderContext, $messages)?->getLink(); |
| 40 | + } |
| 41 | + |
| 42 | + public function hasInventory(string $key): bool |
| 43 | + { |
| 44 | + return $this->findInventoryRepository($key) !== null; |
| 45 | + } |
| 46 | + |
| 47 | + public function getInventory(CrossReferenceNode $node, RenderContext $renderContext, Messages $messages): Inventory|null |
| 48 | + { |
| 49 | + Deprecation::trigger( |
| 50 | + 'phpDocumentor/guides', |
| 51 | + 'https://github.com/phpDocumentor/guides/issues', |
| 52 | + 'InventoryRepository::getInventory() is deprecated. Implement ' |
| 53 | + . 'InventoryLinkResolver::resolveInventoryLink() for one-call interlink resolution.', |
| 54 | + ); |
| 55 | + |
| 56 | + return $this->findInventoryRepository($node->getInterlinkDomain())?->getInventory($node, $renderContext, $messages); |
| 57 | + } |
| 58 | + |
| 59 | + public function resolveInventoryLink( |
| 60 | + CrossReferenceNode $node, |
| 61 | + RenderContext $renderContext, |
| 62 | + Messages $messages, |
| 63 | + ): ResolvedInventoryLink|null { |
| 64 | + $repository = $this->findInventoryRepository($node->getInterlinkDomain()); |
| 65 | + if ($repository === null) { |
| 66 | + $messages->addWarning( |
| 67 | + new Message( |
| 68 | + sprintf('Inventory with key %s not found. ', $node->getInterlinkDomain()), |
| 69 | + array_merge($renderContext->getLoggerInformation(), $node->getDebugInformation()), |
| 70 | + ), |
| 71 | + ); |
| 72 | + |
| 73 | + return null; |
| 74 | + } |
| 75 | + |
| 76 | + if ($repository instanceof InventoryLinkResolver) { |
| 77 | + return $repository->resolveInventoryLink($node, $renderContext, $messages); |
| 78 | + } |
| 79 | + |
| 80 | + $inventory = $repository->getInventory($node, $renderContext, $messages); |
| 81 | + $link = $repository->getLink($node, $renderContext, $messages); |
| 82 | + if ($inventory === null || $link === null) { |
| 83 | + return null; |
| 84 | + } |
| 85 | + |
| 86 | + return new ResolvedInventoryLink($inventory->getBaseUrl(), $link); |
| 87 | + } |
| 88 | + |
| 89 | + private function findInventoryRepository(string $key): InventoryRepository|null |
| 90 | + { |
| 91 | + if (array_key_exists($key, $this->cachedRepositories)) { |
| 92 | + return $this->cachedRepositories[$key]; |
| 93 | + } |
| 94 | + |
| 95 | + foreach ($this->repositories as $repository) { |
| 96 | + if ($repository->hasInventory($key)) { |
| 97 | + $this->cachedRepositories[$key] = $repository; |
| 98 | + |
| 99 | + return $repository; |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + $this->cachedRepositories[$key] = null; |
| 104 | + |
| 105 | + return null; |
| 106 | + } |
| 107 | +} |
0 commit comments