Skip to content

Commit

Permalink
Merge pull request #980 from phpDocumentor/backport/972
Browse files Browse the repository at this point in the history
Backport/972
  • Loading branch information
jaapio authored Apr 12, 2024
2 parents 2938f8b + e8a578e commit 31b0b98
Show file tree
Hide file tree
Showing 33 changed files with 240 additions and 123 deletions.
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": "^1.0@dev || ^0.3",
"phpdocumentor/guides-cli": "^1.0@dev || ^0.3",
"phpdocumentor/guides-code": "^1.0@dev || ^0.3",
Expand Down
99 changes: 50 additions & 49 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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": "^6.5 || ^7.0",
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
Loading

0 comments on commit 31b0b98

Please sign in to comment.