|
| 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 T3Docs\Typo3DocsTheme\Compiler\NodeTransformers; |
| 15 | + |
| 16 | +use phpDocumentor\Guides\Compiler\CompilerContextInterface; |
| 17 | +use phpDocumentor\Guides\Compiler\NodeTransformer; |
| 18 | +use phpDocumentor\Guides\Nodes\DocumentNode; |
| 19 | +use phpDocumentor\Guides\Nodes\Node; |
| 20 | +use phpDocumentor\Guides\Nodes\SectionNode; |
| 21 | +use T3Docs\Typo3DocsTheme\Nodes\Typo3TalkNode; |
| 22 | + |
| 23 | +/** @implements NodeTransformer<DocumentNode|SectionNode|Typo3TalkNode> */ |
| 24 | +final class Typo3TalkNodeTransformer implements NodeTransformer |
| 25 | +{ |
| 26 | + /** @var SectionNode[] $sectionStack */ |
| 27 | + private array $sectionStack = []; |
| 28 | + |
| 29 | + public function __construct( |
| 30 | + ) {} |
| 31 | + |
| 32 | + public function enterNode(Node $node, CompilerContextInterface $compilerContext): Node |
| 33 | + { |
| 34 | + if ($node instanceof DocumentNode) { |
| 35 | + $this->sectionStack = []; |
| 36 | + return $node; |
| 37 | + } |
| 38 | + if ($node instanceof SectionNode) { |
| 39 | + $this->sectionStack[] = $node; |
| 40 | + return $node; |
| 41 | + } |
| 42 | + if ($node instanceof Typo3TalkNode && $this->sectionStack !== []) { |
| 43 | + $currentSection = end($this->sectionStack); |
| 44 | + $node->setSectionNode($currentSection); |
| 45 | + } |
| 46 | + return $node; |
| 47 | + } |
| 48 | + |
| 49 | + public function leaveNode(Node $node, CompilerContextInterface $compilerContext): Node|null |
| 50 | + { |
| 51 | + if ($node instanceof DocumentNode) { |
| 52 | + $this->sectionStack = []; |
| 53 | + return $node; |
| 54 | + } |
| 55 | + if ($node instanceof SectionNode) { |
| 56 | + array_pop($this->sectionStack); |
| 57 | + return $node; |
| 58 | + } |
| 59 | + return $node; |
| 60 | + } |
| 61 | + |
| 62 | + public function supports(Node $node): bool |
| 63 | + { |
| 64 | + return $node instanceof DocumentNode || $node instanceof SectionNode || $node instanceof Typo3TalkNode; |
| 65 | + } |
| 66 | + |
| 67 | + public function getPriority(): int |
| 68 | + { |
| 69 | + return 100000; |
| 70 | + } |
| 71 | +} |
0 commit comments