Skip to content

Commit

Permalink
[TASK] Make globals linkable (#43)
Browse files Browse the repository at this point in the history
* [TASK] Update integration test baseline

Whitespace handling changed in the guides, therefore
our integration tests need to be updated for whitespace.

* [TASK] Make globals linkable
  • Loading branch information
linawolf authored Mar 30, 2024
1 parent 77a6e5c commit 1f09ec2
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 1 deletion.
3 changes: 3 additions & 0 deletions resources/config/php-domain.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use T3Docs\GuidesPhpDomain\TextRoles\ConstTextRole;
use T3Docs\GuidesPhpDomain\TextRoles\EnumTextRole;
use T3Docs\GuidesPhpDomain\TextRoles\ExceptionTextRole;
use T3Docs\GuidesPhpDomain\TextRoles\GlobalTextRole;
use T3Docs\GuidesPhpDomain\TextRoles\MethodTextRole;
use T3Docs\GuidesPhpDomain\TextRoles\PropertyTextRole;
use T3Docs\GuidesPhpDomain\TextRoles\TraitTextRole;
Expand Down Expand Up @@ -75,6 +76,8 @@
->tag('phpdoc.guides.parser.rst.text_role', ['domain' => 'php'])
->set(ExceptionTextRole::class)
->tag('phpdoc.guides.parser.rst.text_role', ['domain' => 'php'])
->set(GlobalTextRole::class)
->tag('phpdoc.guides.parser.rst.text_role', ['domain' => 'php'])
->set(InterfaceTextRole::class)
->tag('phpdoc.guides.parser.rst.text_role', ['domain' => 'php'])
->set(MethodTextRole::class)
Expand Down
14 changes: 13 additions & 1 deletion src/Nodes/PhpGlobalNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@
namespace T3Docs\GuidesPhpDomain\Nodes;

use phpDocumentor\Guides\Nodes\CompoundNode;
use phpDocumentor\Guides\Nodes\LinkTargetNode;
use phpDocumentor\Guides\Nodes\Node;

/**
* Stores data on global PHP variables
*
* @extends CompoundNode<Node>
*/
final class PhpGlobalNode extends CompoundNode
final class PhpGlobalNode extends CompoundNode implements LinkTargetNode
{
private const TYPE = 'global';
/**
* @param list<Node> $value
*/
Expand All @@ -34,4 +36,14 @@ public function getId(): string
{
return $this->id;
}

public function getLinkType(): string
{
return 'php:' . self::TYPE;
}

public function getLinkText(): string
{
return $this->getName();
}
}
50 changes: 50 additions & 0 deletions src/TextRoles/GlobalTextRole.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

declare(strict_types=1);

namespace T3Docs\GuidesPhpDomain\TextRoles;

use phpDocumentor\Guides\Nodes\Inline\ReferenceNode;
use phpDocumentor\Guides\ReferenceResolvers\AnchorNormalizer;
use phpDocumentor\Guides\RestructuredText\Parser\DocumentParserContext;
use Psr\Log\LoggerInterface;

final class GlobalTextRole extends PhpComponentTextRole
{
private const TYPE = 'global';

public function __construct(
LoggerInterface $logger,
private readonly AnchorNormalizer $anchorNormalizer,
) {
parent::__construct($logger, $anchorNormalizer);
}

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($matches[2], $matches[1], $referenceName);
}
return $this->createNodeWithInterlink($referenceTarget, '', $referenceName);
}

private function createNodeWithInterlink(string $referenceTarget, string $interlinkDomain, string|null $referenceName): ReferenceNode
{
$id = $this->anchorNormalizer->reduceAnchor($referenceTarget);

return new ReferenceNode($id, $referenceName ?? $referenceTarget, $interlinkDomain, 'php:' . $this->getName());
}

public function getName(): string
{
return self::TYPE;
}

/**
* @return list<string>
*/
public function getAliases(): array
{
return [];
}
}
20 changes: 20 additions & 0 deletions tests/integration/global-link/expected/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<!-- content start -->
<div class="section" id="php-global-variable">
<h1>PHP Global Variable</h1>
<dl class="php global">
<dt class="sig sig-object php"
id="globals-tca">
<em class="property"><span class="pre">global</span> </em>
<span class="pre">$GLOBALS[&#039;TCA&#039;]</span>
<a class="headerlink" href="#globals-tca" title="Permalink to this definition"></a> </dt>
<dd>

<p>Contains the TYPO3 Configuration Array</p>

</dd>
</dl>

<p>See also <a href="/index.html#globals-tca">$GLOBALS[&#039;TCA&#039;]</a>.</p>

</div>
<!-- content end -->
9 changes: 9 additions & 0 deletions tests/integration/global-link/input/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
===================
PHP Global Variable
===================

.. php:global:: $GLOBALS['TCA']
Contains the TYPO3 Configuration Array

See also :php:global:`$GLOBALS['TCA']`.

0 comments on commit 1f09ec2

Please sign in to comment.