| 1 | <?php |
| 2 | |
| 3 | final class FolderNode |
| 4 | { |
| 5 | public string $name; |
| 6 | public string $path; |
| 7 | |
| 8 | /** @var array<string, FolderNode> keyed by folder name */ |
| 9 | public array $children = []; |
| 10 | |
| 11 | /** @var array<string, mixed> keyed by filename */ |
| 12 | public array $jsonFiles = []; |
| 13 | |
| 14 | public function __construct(string $name, string $path) |
| 15 | { |
| 16 | $this->name = $name; |
| 17 | $this->path = $path; |
| 18 | } |
| 19 | |
| 20 | /** |
| 21 | * Return parsed JSON objects in THIS folder only (non-recursive), |
| 22 | * as a plain numerically-indexed array. |
| 23 | */ |
| 24 | public function jsonNonRecursive(): array |
| 25 | { |
| 26 | return array_values($this->jsonFiles); |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * Return parsed JSON objects in THIS folder and all subfolders (recursive). |
| 31 | */ |
| 32 | public function jsonRecursive(): array |
| 33 | { |
| 34 | $all = array_values($this->jsonFiles); |
| 35 | foreach ($this->children as $child) { |
| 36 | $all = array_merge($all, $child->jsonRecursive()); |
| 37 | } |
| 38 | return $all; |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Find a folder anywhere in the tree by name (first match depth-first). |
| 43 | */ |
| 44 | public function findFolder(string $name): ?FolderNode |
| 45 | { |
| 46 | if ($this->name === $name) |
| 47 | return $this; |
| 48 | |
| 49 | foreach ($this->children as $child) { |
| 50 | $found = $child->findFolder($name); |
| 51 | if ($found) |
| 52 | return $found; |
| 53 | } |
| 54 | return null; |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Build a FolderNode tree from a root directory. |
| 60 | */ |
| 61 | function loadFolderTree(string $rootPath): FolderNode |
| 62 | { |
| 63 | $rootPath = rtrim($rootPath, DIRECTORY_SEPARATOR); |
| 64 | if (!is_dir($rootPath)) { |
| 65 | throw new InvalidArgumentException("Not a directory: {$rootPath}"); |
| 66 | } |
| 67 | |
| 68 | $name = basename($rootPath); |
| 69 | $node = new FolderNode($name, $rootPath); |
| 70 | |
| 71 | $it = new DirectoryIterator($rootPath); |
| 72 | foreach ($it as $entry) { |
| 73 | if ($entry->isDot()) |
| 74 | continue; |
| 75 | |
| 76 | $fullPath = $entry->getPathname(); |
| 77 | |
| 78 | if ($entry->isDir()) { |
| 79 | $child = loadFolderTree($fullPath); |
| 80 | $node->children[$child->name] = $child; |
| 81 | continue; |
| 82 | } |
| 83 | |
| 84 | if ($entry->isFile() && strtolower($entry->getExtension()) === 'json') { |
| 85 | $filename = $entry->getFilename(); |
| 86 | $node->jsonFiles[$filename] = parseJsonFile($fullPath); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | return $node; |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Parse JSON with sane error handling. Returns array/object depending on JSON. |
| 95 | */ |
| 96 | function parseJsonFile(string $path): mixed |
| 97 | { |
| 98 | $raw = file_get_contents($path); |
| 99 | if ($raw === false) { |
| 100 | throw new RuntimeException("Failed to read JSON file: {$path}"); |
| 101 | } |
| 102 | |
| 103 | $data = json_decode($raw, false, 512, JSON_THROW_ON_ERROR); |
| 104 | $data->wikiPath = substr($path, strlen(__DIR__)); |
| 105 | |
| 106 | return $data; |
| 107 | } |