|
| 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\Compiler\NodeTransformers; |
| 15 | + |
| 16 | +use phpDocumentor\Guides\Compiler\CompilerContext; |
| 17 | +use phpDocumentor\Guides\Compiler\NodeTransformer; |
| 18 | +use phpDocumentor\Guides\Nodes\DocumentNode; |
| 19 | +use phpDocumentor\Guides\Nodes\DocumentTree\SectionEntryNode; |
| 20 | +use phpDocumentor\Guides\Nodes\Node; |
| 21 | +use phpDocumentor\Guides\Nodes\ProjectNode; |
| 22 | +use phpDocumentor\Guides\Nodes\SectionNode; |
| 23 | + |
| 24 | +use phpDocumentor\Guides\Nodes\TitleNode; |
| 25 | +use function array_pop; |
| 26 | +use function assert; |
| 27 | +use function count; |
| 28 | +use function end; |
| 29 | + |
| 30 | +/** @implements NodeTransformer<Node> */ |
| 31 | +final class SectionCreationTransformer implements NodeTransformer |
| 32 | +{ |
| 33 | + /** @var SectionNode[] $sectionStack */ |
| 34 | + private array $sectionStack = []; |
| 35 | + |
| 36 | + |
| 37 | + public function enterNode(Node $node, CompilerContext $compilerContext): Node |
| 38 | + { |
| 39 | + if (!$compilerContext->getShadowTree()->getParent()?->getNode() instanceof DocumentNode) { |
| 40 | + return $node; |
| 41 | + } |
| 42 | + if ($node instanceof TitleNode) { |
| 43 | + if (count($this->sectionStack) === 0) { |
| 44 | + $this->sectionStack[] = new SectionNode($node); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + $lastSection = end($this->sectionStack); |
| 49 | + if ($lastSection instanceof SectionNode) { |
| 50 | + $lastSection->addChildNode($node); |
| 51 | + } |
| 52 | + |
| 53 | + return $node; |
| 54 | + } |
| 55 | + |
| 56 | + public function leaveNode(Node $node, CompilerContext $compilerContext): Node|null |
| 57 | + { |
| 58 | + if (!$compilerContext->getShadowTree()->getParent()?->getNode() instanceof DocumentNode) { |
| 59 | + return $node; |
| 60 | + } |
| 61 | + // Try removing all nodes... |
| 62 | + return null; |
| 63 | + /* |
| 64 | + if (count($this->sectionStack) === 0) { |
| 65 | + return $node; |
| 66 | + } |
| 67 | + if ($node instanceof TitleNode) { |
| 68 | + return array_pop($this->sectionStack); |
| 69 | + } |
| 70 | +
|
| 71 | + return null; |
| 72 | + */ |
| 73 | + } |
| 74 | + |
| 75 | + public function supports(Node $node): bool |
| 76 | + { |
| 77 | + return true; |
| 78 | + } |
| 79 | + |
| 80 | + public function getPriority(): int |
| 81 | + { |
| 82 | + // Before SectionEntryRegistrationTransformer |
| 83 | + return 1; |
| 84 | + } |
| 85 | +} |
0 commit comments