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

[FEATURE] Introduce php:global directive #12

Merged
merged 1 commit into from
Nov 25, 2023
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
2 changes: 1 addition & 1 deletion phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ parameters:
ignoreErrors:
-
message: "#^Function phpDocumentor\\\\Guides\\\\DependencyInjection\\\\template not found\\.$#"
count: 7
count: 8
path: src/DependencyInjection/GuidesPhpDomainExtension.php

-
Expand Down
2 changes: 2 additions & 0 deletions resources/config/php-domain.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

use T3Docs\GuidesPhpDomain\Directives\Php\ConstDirective;
use T3Docs\GuidesPhpDomain\Directives\Php\GlobalDirective;
use function Symfony\Component\DependencyInjection\Loader\Configurator\service;

use T3Docs\GuidesPhpDomain\Directives\Php\EnumDirective;
Expand All @@ -30,6 +31,7 @@
->tag('phpdoc.guides.directive')
->set(ConstDirective::class)
->set(EnumDirective::class)
->set(GlobalDirective::class)
->set(InterfaceDirective::class)
->set(MethodDirective::class)
->set(NamespaceDirective::class)
Expand Down
10 changes: 10 additions & 0 deletions resources/template/html/body/directive/php/global.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<dl class="php global">
<dt class="sig sig-object php"
id="{{ node.id }}">
<em class="property"><span class="pre">global</span> </em>
<span class="pre">{{ node.name }}</span>
</dt>
<dd>
{{ renderNode(node.value) }}
</dd>
</dl>
2 changes: 2 additions & 0 deletions src/DependencyInjection/GuidesPhpDomainExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use T3Docs\GuidesPhpDomain\Nodes\MethodNameNode;
use T3Docs\GuidesPhpDomain\Nodes\PhpComponentNode;
use T3Docs\GuidesPhpDomain\Nodes\PhpConstNode;
use T3Docs\GuidesPhpDomain\Nodes\PhpGlobalNode;
use T3Docs\GuidesPhpDomain\Nodes\PhpMethodNode;
use T3Docs\GuidesPhpDomain\Nodes\PhpNamespaceNode;

Expand Down Expand Up @@ -42,6 +43,7 @@ public function prepend(ContainerBuilder $container): void
template(FullyQualifiedNameNode::class, 'body/directive/php/fullyQualifiedName.html.twig'),
template(PhpComponentNode::class, 'body/directive/php/component.html.twig'),
template(PhpConstNode::class, 'body/directive/php/const.html.twig'),
template(PhpGlobalNode::class, 'body/directive/php/global.html.twig'),
template(PhpNamespaceNode::class, 'body/directive/php/namespace.html.twig'),
template(PhpMethodNode::class, 'body/directive/php/method.html.twig'),
template(MemberNameNode::class, 'body/directive/php/memberName.html.twig'),
Expand Down
47 changes: 47 additions & 0 deletions src/Directives/Php/GlobalDirective.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

namespace T3Docs\GuidesPhpDomain\Directives\Php;

use phpDocumentor\Guides\Nodes\CollectionNode;
use phpDocumentor\Guides\Nodes\Node;
use phpDocumentor\Guides\ReferenceResolvers\AnchorReducer;
use phpDocumentor\Guides\RestructuredText\Directives\SubDirective;
use phpDocumentor\Guides\RestructuredText\Parser\BlockContext;
use phpDocumentor\Guides\RestructuredText\Parser\Directive;
use phpDocumentor\Guides\RestructuredText\Parser\Productions\Rule;
use phpDocumentor\Guides\RestructuredText\TextRoles\GenericLinkProvider;
use T3Docs\GuidesPhpDomain\Nodes\PhpGlobalNode;

final class GlobalDirective extends SubDirective
{
public function __construct(
Rule $startingRule,
GenericLinkProvider $genericLinkProvider,
private readonly AnchorReducer $anchorReducer,
) {
parent::__construct($startingRule);
$genericLinkProvider->addGenericLink($this->getName(), $this->getName());
}

public function getName(): string
{
return 'php:global';
}

protected function processSub(
BlockContext $blockContext,
CollectionNode $collectionNode,
Directive $directive,
): Node|null {
$name = trim($directive->getData());
$id = $this->anchorReducer->reduceAnchor($name);

return new PhpGlobalNode(
$id,
$name,
$collectionNode->getChildren(),
);
}
}
37 changes: 37 additions & 0 deletions src/Nodes/PhpGlobalNode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace T3Docs\GuidesPhpDomain\Nodes;

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

/**
* Stores data on global PHP variables
*
* @extends CompoundNode<Node>
*/
final class PhpGlobalNode extends CompoundNode
{
/**
* @param list<Node> $value
*/
public function __construct(
private readonly string $id,
private readonly string $name,
array $value = [],
) {
parent::__construct($value);
}

public function getName(): string
{
return $this->name;
}

public function getId(): string
{
return $this->id;
}
}
18 changes: 18 additions & 0 deletions tests/integration/global-variable/expected/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<!-- 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>
</dt>
<dd>
<p>Contains the TYPO3 Configuration Array</p>
</dd>
</dl>

</div>

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

.. php:global:: $GLOBALS['TCA']

Contains the TYPO3 Configuration Array