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

[TASK] Make globals linkable #43

Merged
merged 2 commits into from
Mar 30, 2024
Merged
Changes from 1 commit
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
Prev Previous commit
[TASK] Make globals linkable
  • Loading branch information
linawolf committed Mar 24, 2024
commit d76567599d1ef6518f0c5bf2453cf573d44a8407
3 changes: 3 additions & 0 deletions resources/config/php-domain.php
Original file line number Diff line number Diff line change
@@ -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;
@@ -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)
14 changes: 13 additions & 1 deletion src/Nodes/PhpGlobalNode.php
Original file line number Diff line number Diff line change
@@ -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
*/
@@ -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']`.
Loading