|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors |
| 5 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 6 | + */ |
| 7 | + |
| 8 | +namespace OpenAPIExtractor; |
| 9 | + |
| 10 | +use PhpParser\Node\Stmt\ClassLike; |
| 11 | +use PhpParser\NodeFinder; |
| 12 | +use PhpParser\NodeTraverser; |
| 13 | +use PhpParser\Parser; |
| 14 | + |
| 15 | +/** |
| 16 | + * Lazily resolves a class, interface, trait or enum by its fully qualified name, |
| 17 | + * by mapping namespace prefixes to source directories, PSR-4 style. |
| 18 | + */ |
| 19 | +class ClassResolver { |
| 20 | + /** @var array<string, ClassLike|false> */ |
| 21 | + private array $cache = []; |
| 22 | + |
| 23 | + /** @var array<string, string> Namespace prefix => source directory */ |
| 24 | + private readonly array $namespaceRoots; |
| 25 | + |
| 26 | + /** @param array<string, string> $namespaceRoots Namespace prefix => source directory */ |
| 27 | + public function __construct( |
| 28 | + private readonly Parser $astParser, |
| 29 | + private readonly NodeTraverser $nodeTraverser, |
| 30 | + private readonly NodeFinder $nodeFinder, |
| 31 | + array $namespaceRoots, |
| 32 | + ) { |
| 33 | + $this->namespaceRoots = array_combine( |
| 34 | + array_map(static fn (string $prefix): string => trim($prefix, '\\'), array_keys($namespaceRoots)), |
| 35 | + array_values($namespaceRoots), |
| 36 | + ); |
| 37 | + } |
| 38 | + |
| 39 | + /** Returns null if the class can not be found, e.g. because it is outside of the known namespace roots. */ |
| 40 | + public function resolve(string $fqcn): ?ClassLike { |
| 41 | + $fqcn = ltrim($fqcn, '\\'); |
| 42 | + if (!array_key_exists($fqcn, $this->cache)) { |
| 43 | + $this->cache[$fqcn] = $this->load($fqcn) ?? false; |
| 44 | + } |
| 45 | + |
| 46 | + $node = $this->cache[$fqcn]; |
| 47 | + return $node !== false ? $node : null; |
| 48 | + } |
| 49 | + |
| 50 | + private function load(string $fqcn): ?ClassLike { |
| 51 | + $path = $this->findFile($fqcn); |
| 52 | + if ($path === null || !is_file($path)) { |
| 53 | + return null; |
| 54 | + } |
| 55 | + |
| 56 | + $contents = file_get_contents($path); |
| 57 | + if ($contents === false) { |
| 58 | + return null; |
| 59 | + } |
| 60 | + |
| 61 | + /** @var ClassLike $node */ |
| 62 | + foreach ($this->nodeFinder->findInstanceOf($this->nodeTraverser->traverse($this->astParser->parse($contents)), ClassLike::class) as $node) { |
| 63 | + if ($node->namespacedName?->toString() === $fqcn) { |
| 64 | + $node->setAttribute('sourceFile', $path); |
| 65 | + return $node; |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + return null; |
| 70 | + } |
| 71 | + |
| 72 | + /** Maps the class name to a file path via its longest matching namespace prefix. */ |
| 73 | + private function findFile(string $fqcn): ?string { |
| 74 | + $bestPrefix = null; |
| 75 | + foreach (array_keys($this->namespaceRoots) as $prefix) { |
| 76 | + if (!str_starts_with($fqcn . '\\', $prefix . '\\')) { |
| 77 | + continue; |
| 78 | + } |
| 79 | + if ($bestPrefix === null || strlen($prefix) > strlen($bestPrefix)) { |
| 80 | + $bestPrefix = $prefix; |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + if ($bestPrefix === null) { |
| 85 | + return null; |
| 86 | + } |
| 87 | + |
| 88 | + $relativeName = substr($fqcn, strlen($bestPrefix) + 1); |
| 89 | + return $this->namespaceRoots[$bestPrefix] . '/' . str_replace('\\', '/', $relativeName) . '.php'; |
| 90 | + } |
| 91 | +} |
0 commit comments