|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace TypeLang\PhpDoc\Parser; |
| 6 | + |
| 7 | +use TypeLang\PhpDoc\DocBlock\Tag\GenericTagDefinition; |
| 8 | +use TypeLang\PhpDoc\DocBlock\TagDefinition\TagDefinitionInterface; |
| 9 | +use TypeLang\PhpDoc\Exception\RecursiveTagAliasException; |
| 10 | +use TypeLang\PhpDoc\Exception\UnknownTagAliasException; |
| 11 | +use TypeLang\PhpDoc\TagRegistryInterface; |
| 12 | + |
| 13 | +final class TagRegistryBuilder |
| 14 | +{ |
| 15 | + /** |
| 16 | + * @var array<non-empty-lowercase-string, TagDefinitionInterface> |
| 17 | + */ |
| 18 | + private array $definitions = []; |
| 19 | + |
| 20 | + /** |
| 21 | + * @var array<non-empty-lowercase-string, non-empty-lowercase-string> |
| 22 | + */ |
| 23 | + private array $aliases = []; |
| 24 | + |
| 25 | + /** |
| 26 | + * @param iterable<non-empty-string, TagDefinitionInterface> $definitions |
| 27 | + * @param iterable<non-empty-string, non-empty-string> $aliases |
| 28 | + */ |
| 29 | + public function __construct( |
| 30 | + iterable $definitions = [], |
| 31 | + iterable $aliases = [], |
| 32 | + private TagDefinitionInterface $genericTagDefinition = new GenericTagDefinition(), |
| 33 | + ) { |
| 34 | + $this->addTagDefinitions($definitions); |
| 35 | + $this->addTagAliases($aliases); |
| 36 | + } |
| 37 | + |
| 38 | + private function setGenericTagDefinition(TagDefinitionInterface $genericTagDefinition): void |
| 39 | + { |
| 40 | + $this->genericTagDefinition = $genericTagDefinition; |
| 41 | + } |
| 42 | + |
| 43 | + public function withGenericTagDefinition(TagDefinitionInterface $definition): self |
| 44 | + { |
| 45 | + $self = clone $this; |
| 46 | + $self->setGenericTagDefinition($definition); |
| 47 | + |
| 48 | + return $self; |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * @param iterable<non-empty-string, TagDefinitionInterface> $definitions |
| 53 | + */ |
| 54 | + private function addTagDefinitions(iterable $definitions): void |
| 55 | + { |
| 56 | + foreach ($definitions as $name => $definition) { |
| 57 | + $this->definitions[\strtolower($name)] = $definition; |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * @param iterable<non-empty-string, TagDefinitionInterface> $definitions |
| 63 | + */ |
| 64 | + public function withAddedTagDefinitions(iterable $definitions): self |
| 65 | + { |
| 66 | + $self = clone $this; |
| 67 | + $self->addTagDefinitions($definitions); |
| 68 | + |
| 69 | + return $self; |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * @param iterable<non-empty-string, non-empty-string> $aliases |
| 74 | + */ |
| 75 | + private function addTagAliases(iterable $aliases): void |
| 76 | + { |
| 77 | + foreach ($aliases as $alias => $name) { |
| 78 | + $this->aliases[\strtolower($alias)] = \strtolower($name); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * @param iterable<non-empty-string, non-empty-string> $aliases |
| 84 | + */ |
| 85 | + public function withAddedTagAliases(iterable $aliases): self |
| 86 | + { |
| 87 | + $self = clone $this; |
| 88 | + $self->addTagAliases($aliases); |
| 89 | + |
| 90 | + return $self; |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * @return array<non-empty-lowercase-string, TagDefinitionInterface> |
| 95 | + * @throws UnknownTagAliasException if the alias cannot be resolved to a definition |
| 96 | + * @throws RecursiveTagAliasException if the alias references form a cycle |
| 97 | + */ |
| 98 | + private function getAllTagDefinitions(): array |
| 99 | + { |
| 100 | + $result = $this->definitions; |
| 101 | + |
| 102 | + foreach ($this->aliases as $alias => $_) { |
| 103 | + $result[$alias] = $this->definitions[$this->getCanonicalName($alias)]; |
| 104 | + } |
| 105 | + |
| 106 | + \ksort($result); |
| 107 | + |
| 108 | + return $result; |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Follows the alias chain until a registered tag definition is reached |
| 113 | + * and returns its canonical (definition) name. |
| 114 | + * |
| 115 | + * @param non-empty-lowercase-string $alias |
| 116 | + * @return non-empty-lowercase-string |
| 117 | + * @throws UnknownTagAliasException if a link in the chain has neither a definition nor an alias |
| 118 | + * @throws RecursiveTagAliasException if the chain references itself |
| 119 | + */ |
| 120 | + private function getCanonicalName(string $alias): string |
| 121 | + { |
| 122 | + $current = $alias; |
| 123 | + |
| 124 | + /** @var list<non-empty-lowercase-string> $visited */ |
| 125 | + $visited = []; |
| 126 | + |
| 127 | + while (!isset($this->definitions[$current])) { |
| 128 | + $canonical = $this->aliases[$current] ?? null; |
| 129 | + |
| 130 | + if ($canonical === null) { |
| 131 | + throw UnknownTagAliasException::becauseAliasHasNoDefinition($alias, $visited); |
| 132 | + } |
| 133 | + |
| 134 | + if (\in_array($canonical, $visited, true)) { |
| 135 | + throw RecursiveTagAliasException::becauseAliasIsRecursive($alias, $visited); |
| 136 | + } |
| 137 | + |
| 138 | + $visited[] = $current = $canonical; |
| 139 | + } |
| 140 | + |
| 141 | + return $current; |
| 142 | + } |
| 143 | + |
| 144 | + public function build(): TagRegistryInterface |
| 145 | + { |
| 146 | + return new TagRegistry( |
| 147 | + definitions: $this->getAllTagDefinitions(), |
| 148 | + genericTagDefinition: $this->genericTagDefinition, |
| 149 | + ); |
| 150 | + } |
| 151 | +} |
0 commit comments