-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The ids also have to contain the fqn name, as different components can have a method of the same name.
- Loading branch information
Showing
44 changed files
with
490 additions
and
215 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace T3Docs\GuidesPhpDomain\Compiler\NodeTransformers; | ||
|
||
use phpDocumentor\Guides\Compiler\CompilerContext; | ||
use phpDocumentor\Guides\Compiler\NodeTransformer; | ||
use phpDocumentor\Guides\Nodes\ClassNode; | ||
use phpDocumentor\Guides\Nodes\DocumentNode; | ||
use phpDocumentor\Guides\Nodes\Node; | ||
|
||
use phpDocumentor\Guides\ReferenceResolvers\AnchorReducer; | ||
use Psr\Log\LoggerInterface; | ||
use T3Docs\GuidesPhpDomain\Nodes\PhpComponentNode; | ||
use T3Docs\GuidesPhpDomain\Nodes\PhpMemberNode; | ||
|
||
use function array_merge; | ||
|
||
/** | ||
* @implements NodeTransformer<Node> | ||
* | ||
* The "class" directive sets the "classes" attribute value on its content or on the first immediately following | ||
* non-comment element. https://docutils.sourceforge.io/docs/ref/rst/directives.html#class | ||
*/ | ||
class MemberNodeTransformer implements NodeTransformer | ||
{ | ||
private ?PhpComponentNode $currentComponent = null; | ||
|
||
public function __construct( | ||
private readonly LoggerInterface $logger, | ||
private readonly AnchorReducer $anchorReducer, | ||
) {} | ||
|
||
public function enterNode(Node $node, CompilerContext $compilerContext): Node | ||
{ | ||
if ($node instanceof PhpComponentNode) { | ||
if ($this->currentComponent instanceof \T3Docs\GuidesPhpDomain\Nodes\PhpComponentNode) { | ||
$this->logger->warning( | ||
sprintf('Nested PHP domain components (php:class, php:interface, php:enum etc) are not supported. | ||
Found php:%s inside %s', $node->toString(), $this->currentComponent->toString()), | ||
$compilerContext->getLoggerInformation() | ||
); | ||
} | ||
$this->currentComponent = $node; | ||
return $node; | ||
} | ||
|
||
if ($node instanceof PhpMemberNode && $this->currentComponent instanceof PhpComponentNode) { | ||
$newId = $this->anchorReducer->reduceAnchor($this->currentComponent->getId() . '::' . $node->getName()); | ||
return $node->withId($newId); | ||
} | ||
|
||
return $node; | ||
} | ||
|
||
public function leaveNode(Node $node, CompilerContext $compilerContext): Node|null | ||
{ | ||
if ($node instanceof PhpComponentNode) { | ||
$this->currentComponent = null; | ||
} | ||
return $node; | ||
} | ||
|
||
public function supports(Node $node): bool | ||
{ | ||
return $node instanceof PhpMemberNode || $node instanceof PhpComponentNode; | ||
} | ||
|
||
public function getPriority(): int | ||
{ | ||
return 40_000; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace T3Docs\GuidesPhpDomain\TextRoles; | ||
|
||
use Doctrine\Common\Lexer\Token; | ||
use phpDocumentor\Guides\Nodes\Inline\AbstractLinkInlineNode; | ||
use phpDocumentor\Guides\Nodes\Inline\ReferenceNode; | ||
use phpDocumentor\Guides\ReferenceResolvers\AnchorReducer; | ||
use phpDocumentor\Guides\RestructuredText\Parser\DocumentParserContext; | ||
use phpDocumentor\Guides\RestructuredText\Parser\InlineLexer; | ||
use phpDocumentor\Guides\RestructuredText\TextRoles\TextRole; | ||
use Psr\Log\LoggerInterface; | ||
|
||
final class MethodTextRole extends PhpComponentTextRole | ||
{ | ||
private const TYPE = 'method'; | ||
|
||
/** | ||
* @see https://regex101.com/r/EKNh6v/1 | ||
*/ | ||
private const METHOD_NAME_REGEX = '/^([a-zA-Z0-9_\\\]+)\:\:(\w+)(\(.*\)){0,1}$/'; | ||
public function __construct( | ||
LoggerInterface $logger, | ||
private readonly AnchorReducer $anchorReducer, | ||
) { | ||
parent::__construct($logger, $anchorReducer); | ||
} | ||
|
||
protected function createNode(DocumentParserContext $documentParserContext, string $referenceTarget, string|null $referenceName, string $role): ReferenceNode | ||
{ | ||
if (preg_match(self::INTERLINK_NAME_REGEX, $referenceTarget, $matches)) { | ||
return $this->createNodeWithInterlink($documentParserContext, $matches[2], $matches[1], $referenceName); | ||
} | ||
return $this->createNodeWithInterlink($documentParserContext, $referenceTarget, '', $referenceName); | ||
} | ||
|
||
private function createNodeWithInterlink(DocumentParserContext $documentParserContext, string $referenceTarget, string $interlinkDomain, string|null $referenceName): ReferenceNode | ||
{ | ||
if (!preg_match(self::METHOD_NAME_REGEX, $referenceTarget, $matches)) { | ||
$this->logger->warning($referenceTarget . ' is not a valid method name. Use the form "\Vendor\Path\Class::method" or "\Vendor\Path\Class::method(int $param)"', $documentParserContext->getLoggerInformation()); | ||
$id = $this->anchorReducer->reduceAnchor($referenceTarget); | ||
return new ReferenceNode($id, $referenceName ?? $referenceTarget, $interlinkDomain, 'php:' . $this->getName()); | ||
} | ||
|
||
$class = $matches[1]; | ||
$method = $matches[2]; | ||
|
||
$id = $this->anchorReducer->reduceAnchor($class . '::' . $method); | ||
|
||
return new ReferenceNode($id, $referenceName ?? $referenceTarget, $interlinkDomain, 'php:' . $this->getName()); | ||
} | ||
|
||
public function getName(): string | ||
{ | ||
return self::TYPE; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.