Skip to content

Commit

Permalink
[FEATURE] Introduce php:class directive (#11)
Browse files Browse the repository at this point in the history
* [FEATURE] Introduce php:class directive

visibility and other modifiers and the textrole come in a followup

* Update tests/integration/class-with-method/input/index.rst

Co-authored-by: Chris Müller <2566282+brotkrueml@users.noreply.github.com>

---------

Co-authored-by: Chris Müller <2566282+brotkrueml@users.noreply.github.com>
  • Loading branch information
linawolf and brotkrueml authored Nov 25, 2023
1 parent e951aff commit cc00776
Show file tree
Hide file tree
Showing 9 changed files with 233 additions and 0 deletions.
2 changes: 2 additions & 0 deletions resources/config/php-domain.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use phpDocumentor\Guides\RestructuredText\Parser\Productions\DirectiveContentRule;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

use T3Docs\GuidesPhpDomain\Directives\Php\ClassDirective;
use T3Docs\GuidesPhpDomain\Directives\Php\ConstDirective;
use T3Docs\GuidesPhpDomain\Directives\Php\GlobalDirective;
use function Symfony\Component\DependencyInjection\Loader\Configurator\service;
Expand All @@ -29,6 +30,7 @@
->bind('$startingRule', service(DirectiveContentRule::class))
->instanceof(BaseDirective::class)
->tag('phpdoc.guides.directive')
->set(ClassDirective::class)
->set(ConstDirective::class)
->set(EnumDirective::class)
->set(GlobalDirective::class)
Expand Down
55 changes: 55 additions & 0 deletions src/Directives/Php/ClassDirective.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?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\PhpClassNode;
use T3Docs\GuidesPhpDomain\Nodes\PhpInterfaceNode;
use T3Docs\GuidesPhpDomain\PhpDomain\FullyQualifiedNameService;

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

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

protected function processSub(
BlockContext $blockContext,
CollectionNode $collectionNode,
Directive $directive,
): Node|null {
$name = trim($directive->getData());
$fqn = $this->fullyQualifiedNameService->getFullyQualifiedName($name, true);

$id = $this->anchorReducer->reduceAnchor($fqn->toString());

return new PhpClassNode(
$id,
$fqn,
$collectionNode->getChildren(),
null,
[],
[],
);
}
}
27 changes: 27 additions & 0 deletions src/Nodes/PhpClassNode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace T3Docs\GuidesPhpDomain\Nodes;

use phpDocumentor\Guides\Nodes\Node;

final class PhpClassNode extends PhpComponentNode
{
private const TYPE = 'class';
/**
* @param list<PhpMemberNode> $members
* @param list<string> $modifiers
* @param list<Node> $value
*/
public function __construct(
string $id,
FullyQualifiedNameNode $name,
array $value = [],
PhpNamespaceNode|null $namespace = null,
array $members = [],
array $modifiers = [],
) {
parent::__construct($id, self::TYPE, $name, $value, $namespace, $members, $modifiers);
}
}
46 changes: 46 additions & 0 deletions tests/integration/class-namespace-directive/expected/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<!-- content start -->
<div class="section" id="php-class-with-current-namespace-from-directive">
<h1>PHP Class with current namespace from directive</h1>

<dl class="php class">
<dt class="sig sig-object php"
id="typo3-cms-core-testclass">
<em class="property"><span class="pre">class</span> </em>
<span class="sig-prename descclassname"><span class="pre">\TYPO3\CMS\Core\</span></span>
<span class="sig-name descname"><span class="pre">TestClass</span></span>

</dt>
<dd>
<p>Lorem Ipsum Dolor!</p>
</dd>
</dl>

<dl class="php class">
<dt class="sig sig-object php"
id="typo3-cms-core-anotherclass">
<em class="property"><span class="pre">class</span> </em>
<span class="sig-prename descclassname"><span class="pre">\TYPO3\CMS\Core\</span></span>
<span class="sig-name descname"><span class="pre">AnotherClass</span></span>

</dt>
<dd>
<p>Lorem Ipsum Dolor Another!</p>
</dd>
</dl>

<dl class="php class">
<dt class="sig sig-object php"
id="myvendor-some-namespace-anotherclass">
<em class="property"><span class="pre">class</span> </em>
<span class="sig-prename descclassname"><span class="pre">\MyVendor\Some\Namespace\</span></span>
<span class="sig-name descname"><span class="pre">AnotherClass</span></span>

</dt>
<dd>
<p>Lorem Ipsum Dolor Yet Another!</p>
</dd>
</dl>

</div>

<!-- content end -->
17 changes: 17 additions & 0 deletions tests/integration/class-namespace-directive/input/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
===============================================
PHP Class with current namespace from directive
===============================================

.. php:namespace:: TYPO3\CMS\Core
.. php:class:: TestClass
Lorem Ipsum Dolor!

.. php:class:: AnotherClass
Lorem Ipsum Dolor Another!

.. php:class:: MyVendor\Some\Namespace\AnotherClass
Lorem Ipsum Dolor Yet Another!
45 changes: 45 additions & 0 deletions tests/integration/class-with-method/expected/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<!-- content start -->
<div class="section" id="php-class-with-explicit-namespace">
<h1>PHP Class with explicit namespace</h1>

<dl class="php class">
<dt class="sig sig-object php"
id="typo3-cms-core-test">
<em class="property"><span class="pre">class</span> </em>
<span class="sig-prename descclassname"><span class="pre">\TYPO3\CMS\Core\</span></span>
<span class="sig-name descname"><span class="pre">Test</span></span>

</dt>
<dd>
<p>Lorem Ipsum Dolor!</p><dl class="php method">
<dt class="sig sig-object php" id="setdate">

<span class="sig-name descname"><span class="pre">setDate</span></span>
<span class="sig-paren">(</span>
<em class="sig-param"><span class="pre">int $year</span></em>, <em class="sig-param"><span class="pre">int $month</span></em>, <em class="sig-param"><span class="pre">int $day</span></em><span class="sig-paren">)</span>

</dt>
<dd>
<p>Set the date.</p>
</dd>
</dl>
<dl class="php method">
<dt class="sig sig-object php" id="getdate">

<span class="sig-name descname"><span class="pre">getDate</span></span>
<span class="sig-paren">(</span>
<span class="sig-paren">)</span>
<em class="sig-returns"><span class="pre">: int</span></em>

</dt>
<dd>
<p>Get the date.</p>
</dd>
</dl>

</dd>
</dl>

</div>

<!-- content end -->
15 changes: 15 additions & 0 deletions tests/integration/class-with-method/input/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
=================================
PHP Class with explicit namespace
=================================

.. php:class:: TYPO3\CMS\Core\Test
Lorem Ipsum Dolor!

.. php:method:: setDate(int $year, int $month, int $day)
Set the date.
.. php:method:: getDate(): int
Get the date.
19 changes: 19 additions & 0 deletions tests/integration/class-without-namespace/expected/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<!-- content start -->
<div class="section" id="php-class">
<h1>PHP Class</h1>

<dl class="php class">
<dt class="sig sig-object php"
id="testclass">
<em class="property"><span class="pre">class</span> </em>
<span class="sig-name descname"><span class="pre">TestClass</span></span>

</dt>
<dd>
<p>Lorem Ipsum Dolor!</p>
</dd>
</dl>

</div>

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

.. php:class:: TestClass
Lorem Ipsum Dolor!

0 comments on commit cc00776

Please sign in to comment.