-
-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathDirectiveProcessPass.php
More file actions
86 lines (70 loc) · 2.48 KB
/
DirectiveProcessPass.php
File metadata and controls
86 lines (70 loc) · 2.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?php
declare(strict_types=1);
/**
* This file is part of phpDocumentor.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @link https://phpdoc.org
*/
namespace phpDocumentor\Guides\RestructuredText\Compiler\Passes;
use phpDocumentor\Guides\Compiler\CompilerContext;
use phpDocumentor\Guides\Compiler\NodeTransformer;
use phpDocumentor\Guides\Compiler\ReverseNodeTransformer;
use phpDocumentor\Guides\Nodes\Node;
use phpDocumentor\Guides\RestructuredText\Directives\BaseDirective as DirectiveHandler;
use phpDocumentor\Guides\RestructuredText\Directives\GeneralDirective;
use phpDocumentor\Guides\RestructuredText\Nodes\DirectiveNode;
use phpDocumentor\Guides\RestructuredText\Parser\Directive;
use Psr\Log\LoggerInterface;
use function strtolower;
use const PHP_INT_MAX;
/** @implements NodeTransformer<DirectiveNode> */
final class DirectiveProcessPass implements ReverseNodeTransformer
{
/** @var array<string, DirectiveHandler> */
private array $directives;
/** @param iterable<DirectiveHandler> $directives */
public function __construct(
private readonly LoggerInterface $logger,
private readonly GeneralDirective $generalDirective,
iterable $directives = [],
) {
foreach ($directives as $directive) {
$this->registerDirective($directive);
}
}
private function registerDirective(DirectiveHandler $directive): void
{
$this->directives[strtolower($directive->getName())] = $directive;
foreach ($directive->getAliases() as $alias) {
$this->directives[strtolower($alias)] = $directive;
}
}
public function enterNode(Node $node, CompilerContext $compilerContext): Node
{
return $node;
}
public function leaveNode(Node $node, CompilerContext $compilerContext): Node|null
{
$newNode = $this->getDirectiveHandler($node->getDirective())->createNode($node);
if ($newNode === null) {
return null;
}
$newNode->setClasses($node->getClasses());
return $newNode;
}
private function getDirectiveHandler(Directive $directive): DirectiveHandler
{
return $this->directives[strtolower($directive->getName())] ?? $this->generalDirective;
}
public function supports(Node $node): bool
{
return $node instanceof DirectiveNode;
}
public function getPriority(): int
{
return 100;
}
}