Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extendable compiler context #972

Merged
merged 2 commits into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"php": "^8.1",
"ext-json": "*",
"ext-mbstring": "*",
"doctrine/deprecations": "^1.1",
"phpdocumentor/guides": "^2.0@dev || ^1.0",
"phpdocumentor/guides-cli": "^2.0@dev || ^1.0",
"phpdocumentor/guides-code": "^2.0@dev || ^1.0",
Expand Down
638 changes: 329 additions & 309 deletions composer.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions packages/guides/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"php": "^8.1",
"ext-json": "*",
"ext-zlib": "*",
"doctrine/deprecations": "^1.0",
"league/flysystem": "^1.1",
"league/tactician": "^1.1",
"league/uri": "^7.4.1",
Expand Down
23 changes: 22 additions & 1 deletion packages/guides/src/Compiler/CompilerContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,41 @@

namespace phpDocumentor\Guides\Compiler;

use Doctrine\Deprecations\Deprecation;
use Exception;
use phpDocumentor\Guides\Compiler\ShadowTree\TreeNode;
use phpDocumentor\Guides\Nodes\DocumentNode;
use phpDocumentor\Guides\Nodes\Node;
use phpDocumentor\Guides\Nodes\ProjectNode;

final class CompilerContext
/**
* Context class used in compiler passes to store the state of the nodes.
*
* The {@see Compiler} is making changes to the nodes in a {@see DocumentNode} as the nodes are immutable cannot
* do this directly. This class helps to modify the nodes in the {@see DocumentNode} by creating a shadow tree.
*
* The class is final and should not be extended, if you need to provide more information to the compiler pass
* you can use the {@see CompilerContextInterface} and decorate this class.
*
* @final
*/
class CompilerContext implements CompilerContextInterface
{
/** @var TreeNode<Node> */
private TreeNode $shadowTree;

public function __construct(
private readonly ProjectNode $projectNode,
) {
if (self::class === static::class) {
return;
}

Deprecation::trigger(
'phpdocumentor/guides',
'https://github.com/phpDocumentor/guides/issues/971',
'Extending CompilerContext is deprecated, please use the CompilerContextInterface instead.',
);
}

public function getProjectNode(): ProjectNode
Expand Down
37 changes: 37 additions & 0 deletions packages/guides/src/Compiler/CompilerContextInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

/**
* This file is part of phpDocumentor.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @link https://phpdoc.org
*/

namespace phpDocumentor\Guides\Compiler;

use phpDocumentor\Guides\Compiler\ShadowTree\TreeNode;
use phpDocumentor\Guides\Nodes\DocumentNode;
use phpDocumentor\Guides\Nodes\Node;
use phpDocumentor\Guides\Nodes\ProjectNode;

interface CompilerContextInterface
{
public function getProjectNode(): ProjectNode;

public function getDocumentNode(): DocumentNode;

public function withDocumentShadowTree(DocumentNode $documentNode): self;

/** @param TreeNode<Node> $shadowTree */
public function withShadowTree(TreeNode $shadowTree): self;

/** @return TreeNode<Node> */
public function getShadowTree(): TreeNode;

/** @return array<string, string> */
public function getLoggerInformation(): array;
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@

namespace phpDocumentor\Guides\Compiler\NodeTransformers;

use phpDocumentor\Guides\Compiler\CompilerContext;
use phpDocumentor\Guides\Compiler\CompilerContextInterface;
use phpDocumentor\Guides\Compiler\NodeTransformer;
use phpDocumentor\Guides\Nodes\Inline\CitationInlineNode;
use phpDocumentor\Guides\Nodes\Node;

/** @implements NodeTransformer<Node> */
final class CitationInlineNodeTransformer implements NodeTransformer
{
public function enterNode(Node $node, CompilerContext $compilerContext): Node
public function enterNode(Node $node, CompilerContextInterface $compilerContext): Node
{
if ($node instanceof CitationInlineNode) {
$internalTarget = $compilerContext->getProjectNode()->getCitationTarget($node->getName());
Expand All @@ -31,7 +31,7 @@ public function enterNode(Node $node, CompilerContext $compilerContext): Node
return $node;
}

public function leaveNode(Node $node, CompilerContext $compilerContext): Node|null
public function leaveNode(Node $node, CompilerContextInterface $compilerContext): Node|null
{
return $node;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace phpDocumentor\Guides\Compiler\NodeTransformers;

use phpDocumentor\Guides\Compiler\CompilerContext;
use phpDocumentor\Guides\Compiler\CompilerContextInterface;
use phpDocumentor\Guides\Compiler\NodeTransformer;
use phpDocumentor\Guides\Meta\CitationTarget;
use phpDocumentor\Guides\Nodes\CitationNode;
Expand All @@ -22,7 +22,7 @@
/** @implements NodeTransformer<Node> */
final class CitationTargetTransformer implements NodeTransformer
{
public function enterNode(Node $node, CompilerContext $compilerContext): Node
public function enterNode(Node $node, CompilerContextInterface $compilerContext): Node
{
if ($node instanceof CitationNode) {
$compilerContext->getProjectNode()->addCitationTarget(
Expand All @@ -37,7 +37,7 @@ public function enterNode(Node $node, CompilerContext $compilerContext): Node
return $node;
}

public function leaveNode(Node $node, CompilerContext $compilerContext): Node|null
public function leaveNode(Node $node, CompilerContextInterface $compilerContext): Node|null
{
return $node;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace phpDocumentor\Guides\Compiler\NodeTransformers;

use phpDocumentor\Guides\Compiler\CompilerContext;
use phpDocumentor\Guides\Compiler\CompilerContextInterface;
use phpDocumentor\Guides\Compiler\NodeTransformer;
use phpDocumentor\Guides\Nodes\ClassNode;
use phpDocumentor\Guides\Nodes\DocumentNode;
Expand All @@ -32,7 +32,7 @@ final class ClassNodeTransformer implements NodeTransformer
/** @var string[] */
private array $classes = [];

public function enterNode(Node $node, CompilerContext $compilerContext): Node
public function enterNode(Node $node, CompilerContextInterface $compilerContext): Node
{
if ($node instanceof DocumentNode) {
// unset classes when entering the next document
Expand All @@ -52,7 +52,7 @@ public function enterNode(Node $node, CompilerContext $compilerContext): Node
return $node;
}

public function leaveNode(Node $node, CompilerContext $compilerContext): Node|null
public function leaveNode(Node $node, CompilerContextInterface $compilerContext): Node|null
{
if ($node instanceof ClassNode) {
//Remove the class node from the tree.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace phpDocumentor\Guides\Compiler\NodeTransformers;

use phpDocumentor\Guides\Compiler\CompilerContext;
use phpDocumentor\Guides\Compiler\CompilerContextInterface;
use phpDocumentor\Guides\Compiler\NodeTransformer;
use phpDocumentor\Guides\Meta\InternalTarget;
use phpDocumentor\Guides\Nodes\AnchorNode;
Expand Down Expand Up @@ -49,7 +49,7 @@ public function __construct(
$this->documentStack = new SplStack();
}

public function enterNode(Node $node, CompilerContext $compilerContext): Node
public function enterNode(Node $node, CompilerContextInterface $compilerContext): Node
{
if ($node instanceof DocumentNode) {
$this->documentStack->push($node);
Expand Down Expand Up @@ -137,7 +137,7 @@ public function enterNode(Node $node, CompilerContext $compilerContext): Node
return $node;
}

public function leaveNode(Node $node, CompilerContext $compilerContext): Node|null
public function leaveNode(Node $node, CompilerContextInterface $compilerContext): Node|null
{
if ($node instanceof DocumentNode) {
$this->documentStack->pop();
Expand All @@ -157,7 +157,7 @@ public function getPriority(): int
return 5000;
}

private function addLinkTargetToProject(CompilerContext $compilerContext, InternalTarget $internalTarget): void
private function addLinkTargetToProject(CompilerContextInterface $compilerContext, InternalTarget $internalTarget): void
{
if ($compilerContext->getProjectNode()->hasInternalTarget($internalTarget->getAnchor(), $internalTarget->getLinkType())) {
$otherLink = $compilerContext->getProjectNode()->getInternalTarget($internalTarget->getAnchor(), $internalTarget->getLinkType());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace phpDocumentor\Guides\Compiler\NodeTransformers;

use phpDocumentor\Guides\Compiler\CompilerContext;
use phpDocumentor\Guides\Compiler\CompilerContextInterface;
use phpDocumentor\Guides\Compiler\NodeTransformer;
use phpDocumentor\Guides\Nodes\DocumentBlockNode;
use phpDocumentor\Guides\Nodes\Menu\TocNode;
Expand All @@ -27,12 +27,12 @@
*/
final class DocumentBlockNodeTransformer implements NodeTransformer
{
public function enterNode(Node $node, CompilerContext $compilerContext): Node
public function enterNode(Node $node, CompilerContextInterface $compilerContext): Node
{
return $node;
}

public function leaveNode(Node $node, CompilerContext $compilerContext): Node|null
public function leaveNode(Node $node, CompilerContextInterface $compilerContext): Node|null
{
if ($node instanceof DocumentBlockNode) {
$children = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace phpDocumentor\Guides\Compiler\NodeTransformers;

use phpDocumentor\Guides\Compiler\CompilerContext;
use phpDocumentor\Guides\Compiler\CompilerContextInterface;
use phpDocumentor\Guides\Compiler\NodeTransformer;
use phpDocumentor\Guides\Nodes\DocumentNode;
use phpDocumentor\Guides\Nodes\DocumentTree\DocumentEntryNode;
Expand All @@ -29,12 +29,12 @@ public function __construct(
) {
}

public function enterNode(Node $node, CompilerContext $compilerContext): Node
public function enterNode(Node $node, CompilerContextInterface $compilerContext): Node
{
return $node;
}

public function leaveNode(Node $node, CompilerContext $compilerContext): Node|null
public function leaveNode(Node $node, CompilerContextInterface $compilerContext): Node|null
{
if (!$node instanceof DocumentNode) {
return $node;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace phpDocumentor\Guides\Compiler\NodeTransformers;

use phpDocumentor\Guides\Compiler\CompilerContext;
use phpDocumentor\Guides\Compiler\CompilerContextInterface;
use phpDocumentor\Guides\Compiler\NodeTransformer;
use phpDocumentor\Guides\Meta\FootnoteTarget;
use phpDocumentor\Guides\Nodes\FootnoteNode;
Expand All @@ -22,7 +22,7 @@
/** @implements NodeTransformer<Node> */
final class FootNodeNamedTransformer implements NodeTransformer
{
public function enterNode(Node $node, CompilerContext $compilerContext): Node
public function enterNode(Node $node, CompilerContextInterface $compilerContext): Node
{
if ($node instanceof FootnoteNode && $this->supports($node)) {
$number = $compilerContext->getDocumentNode()->addFootnoteTarget(new FootnoteTarget(
Expand All @@ -37,7 +37,7 @@ public function enterNode(Node $node, CompilerContext $compilerContext): Node
return $node;
}

public function leaveNode(Node $node, CompilerContext $compilerContext): Node|null
public function leaveNode(Node $node, CompilerContextInterface $compilerContext): Node|null
{
return $node;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace phpDocumentor\Guides\Compiler\NodeTransformers;

use phpDocumentor\Guides\Compiler\CompilerContext;
use phpDocumentor\Guides\Compiler\CompilerContextInterface;
use phpDocumentor\Guides\Compiler\NodeTransformer;
use phpDocumentor\Guides\Meta\FootnoteTarget;
use phpDocumentor\Guides\Nodes\FootnoteNode;
Expand All @@ -22,7 +22,7 @@
/** @implements NodeTransformer<Node> */
final class FootNodeNumberedTransformer implements NodeTransformer
{
public function enterNode(Node $node, CompilerContext $compilerContext): Node
public function enterNode(Node $node, CompilerContextInterface $compilerContext): Node
{
if ($node instanceof FootnoteNode && $this->supports($node)) {
$compilerContext->getDocumentNode()->addFootnoteTarget(new FootnoteTarget(
Expand All @@ -36,7 +36,7 @@ public function enterNode(Node $node, CompilerContext $compilerContext): Node
return $node;
}

public function leaveNode(Node $node, CompilerContext $compilerContext): Node|null
public function leaveNode(Node $node, CompilerContextInterface $compilerContext): Node|null
{
return $node;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@

namespace phpDocumentor\Guides\Compiler\NodeTransformers;

use phpDocumentor\Guides\Compiler\CompilerContext;
use phpDocumentor\Guides\Compiler\CompilerContextInterface;
use phpDocumentor\Guides\Compiler\NodeTransformer;
use phpDocumentor\Guides\Nodes\Inline\FootnoteInlineNode;
use phpDocumentor\Guides\Nodes\Node;

/** @implements NodeTransformer<Node> */
final class FootnoteInlineNodeTransformer implements NodeTransformer
{
public function enterNode(Node $node, CompilerContext $compilerContext): Node
public function enterNode(Node $node, CompilerContextInterface $compilerContext): Node
{
if ($node instanceof FootnoteInlineNode) {
if ($node->getNumber() > 0) {
Expand All @@ -38,7 +38,7 @@ public function enterNode(Node $node, CompilerContext $compilerContext): Node
return $node;
}

public function leaveNode(Node $node, CompilerContext $compilerContext): Node|null
public function leaveNode(Node $node, CompilerContextInterface $compilerContext): Node|null
{
return $node;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace phpDocumentor\Guides\Compiler\NodeTransformers;

use phpDocumentor\Guides\Compiler\CompilerContext;
use phpDocumentor\Guides\Compiler\CompilerContextInterface;
use phpDocumentor\Guides\Compiler\NodeTransformer;
use phpDocumentor\Guides\Nodes\ListItemNode;
use phpDocumentor\Guides\Nodes\ListNode;
Expand All @@ -30,12 +30,12 @@ public function __construct(
) {
}

public function enterNode(Node $node, CompilerContext $compilerContext): Node
public function enterNode(Node $node, CompilerContextInterface $compilerContext): Node
{
return $node;
}

public function leaveNode(Node $node, CompilerContext $compilerContext): Node|null
public function leaveNode(Node $node, CompilerContextInterface $compilerContext): Node|null
{
assert($node instanceof ListNode);
foreach ($node->getChildren() as $listItemNode) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
namespace phpDocumentor\Guides\Compiler\NodeTransformers\MenuNodeTransformers;

use Exception;
use phpDocumentor\Guides\Compiler\CompilerContext;
use phpDocumentor\Guides\Compiler\CompilerContextInterface;
use phpDocumentor\Guides\Compiler\NodeTransformer;
use phpDocumentor\Guides\Nodes\Menu\MenuEntryNode;
use phpDocumentor\Guides\Nodes\Menu\MenuNode;
Expand All @@ -32,13 +32,13 @@ public function __construct(
) {
}

final public function enterNode(Node $node, CompilerContext $compilerContext): MenuEntryNode
final public function enterNode(Node $node, CompilerContextInterface $compilerContext): MenuEntryNode
{
return $node;
}

/** @param MenuEntryNode $node */
final public function leaveNode(Node $node, CompilerContext $compilerContext): MenuEntryNode|null
final public function leaveNode(Node $node, CompilerContextInterface $compilerContext): MenuEntryNode|null
{
assert($node instanceof MenuEntryNode);
$currentMenuShaddow = $compilerContext->getShadowTree()->getParent();
Expand Down Expand Up @@ -70,5 +70,5 @@ final public function leaveNode(Node $node, CompilerContext $compilerContext): M
}

/** @return list<MenuEntryNode> */
abstract protected function handleMenuEntry(MenuNode $currentMenu, MenuEntryNode $entryNode, CompilerContext $compilerContext): array;
abstract protected function handleMenuEntry(MenuNode $currentMenu, MenuEntryNode $entryNode, CompilerContextInterface $compilerContext): array;
}
Loading
Loading