Skip to content

Commit 6ec95ae

Browse files
committed
!!![TASK] Remove all services from render context
The render context should be a service less object only containing the context of the current rendered project/document. This reduces the ways we are generating urls and makes the creation of the object less complex.
1 parent 1bf319a commit 6ec95ae

21 files changed

+197
-201
lines changed

packages/guides/src/Handlers/RenderDocumentCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,6 @@ public function getContext(): RenderContext
2727

2828
public function getFileDestination(): string
2929
{
30-
return $this->renderContext->getCurrentFileDestination();
30+
return $this->renderContext->getDestinationPath() . '/' . $this->renderContext->getCurrentFileName() . '.' . $this->renderContext->getOutputFormat();
3131
}
3232
}

packages/guides/src/NodeRenderers/Html/MenuEntryRenderer.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,16 @@
88
use phpDocumentor\Guides\Nodes\Menu\MenuEntryNode;
99
use phpDocumentor\Guides\Nodes\Node;
1010
use phpDocumentor\Guides\RenderContext;
11+
use phpDocumentor\Guides\Renderer\UrlGenerator\UrlGeneratorInterface;
1112
use phpDocumentor\Guides\TemplateRenderer;
1213

1314
/** @implements NodeRenderer<MenuEntryNode> */
1415
final class MenuEntryRenderer implements NodeRenderer
1516
{
16-
public function __construct(private readonly TemplateRenderer $renderer)
17-
{
17+
public function __construct(
18+
private readonly TemplateRenderer $renderer,
19+
private readonly UrlGeneratorInterface $urlGenerator,
20+
) {
1821
}
1922

2023
public function supports(Node $node): bool
@@ -28,7 +31,7 @@ public function render(Node $node, RenderContext $renderContext): string
2831
$renderContext,
2932
'body/menu/menu-item.html.twig',
3033
[
31-
'url' => $renderContext->generateCanonicalOutputUrl($node->getUrl(), $node->getValue()->getId()),
34+
'url' => $this->urlGenerator->generateCanonicalOutputUrl($renderContext, $node->getUrl(), $node->getValue()->getId()),
3235
'node' => $node,
3336
],
3437
);

packages/guides/src/Parser.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,7 @@ public function registerStrategy(MarkupLanguageParser $strategy): void
5050
$this->parserStrategies[] = $strategy;
5151
}
5252

53-
/**
54-
* @psalm-assert ParserContext $this->parserContext
55-
* @psalm-assert Metas $this->metas
56-
*/
53+
/** @psalm-assert ParserContext $this->parserContext */
5754
public function prepare(
5855
FilesystemInterface|null $origin,
5956
string $sourcePath,

packages/guides/src/ReferenceResolvers/AnchorReferenceResolver.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use phpDocumentor\Guides\Nodes\Inline\LinkInlineNode;
88
use phpDocumentor\Guides\RenderContext;
9+
use phpDocumentor\Guides\Renderer\UrlGenerator\UrlGeneratorInterface;
910

1011
/**
1112
* Resolves references with an anchor URL.
@@ -18,6 +19,7 @@ class AnchorReferenceResolver implements ReferenceResolver
1819

1920
public function __construct(
2021
private readonly AnchorReducer $anchorReducer,
22+
private readonly UrlGeneratorInterface $urlGenerator,
2123
) {
2224
}
2325

@@ -29,7 +31,7 @@ public function resolve(LinkInlineNode $node, RenderContext $renderContext): boo
2931
return false;
3032
}
3133

32-
$node->setUrl($renderContext->generateCanonicalOutputUrl($target->getDocumentPath(), $target->getAnchor()));
34+
$node->setUrl($this->urlGenerator->generateCanonicalOutputUrl($renderContext, $target->getDocumentPath(), $target->getAnchor()));
3335
if ($node->getValue() === '') {
3436
$node->setValue($target->getTitle() ?? '');
3537
}

packages/guides/src/ReferenceResolvers/DocReferenceResolver.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,32 @@
77
use phpDocumentor\Guides\Nodes\Inline\DocReferenceNode;
88
use phpDocumentor\Guides\Nodes\Inline\LinkInlineNode;
99
use phpDocumentor\Guides\RenderContext;
10+
use phpDocumentor\Guides\Renderer\UrlGenerator\UrlGeneratorInterface;
1011

1112
class DocReferenceResolver implements ReferenceResolver
1213
{
1314
public final const PRIORITY = 1000;
1415

16+
public function __construct(
17+
private readonly UrlGeneratorInterface $urlGenerator,
18+
private readonly DocumentNameResolverInterface $documentNameResolver,
19+
) {
20+
}
21+
1522
public function resolve(LinkInlineNode $node, RenderContext $renderContext): bool
1623
{
1724
if (!$node instanceof DocReferenceNode) {
1825
return false;
1926
}
2027

2128
$document = $renderContext->getProjectNode()->findDocumentEntry(
22-
$renderContext->canonicalUrl($node->getTargetReference()),
29+
$this->documentNameResolver->canonicalUrl($renderContext->getDirName(), $node->getTargetReference()),
2330
);
2431
if ($document === null) {
2532
return false;
2633
}
2734

28-
$node->setUrl($renderContext->generateCanonicalOutputUrl($document->getFile()));
35+
$node->setUrl($this->urlGenerator->generateCanonicalOutputUrl($renderContext, $document->getFile()));
2936
if ($node->getValue() === '') {
3037
$node->setValue($document->getTitle()->toString());
3138
}

packages/guides/src/RenderContext.php

Lines changed: 28 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -15,35 +15,28 @@
1515

1616
use Exception;
1717
use League\Flysystem\FilesystemInterface;
18+
use LogicException;
1819
use phpDocumentor\Guides\Nodes\DocumentNode;
1920
use phpDocumentor\Guides\Nodes\DocumentTree\DocumentEntryNode;
2021
use phpDocumentor\Guides\Nodes\Node;
2122
use phpDocumentor\Guides\Nodes\ProjectNode;
22-
use phpDocumentor\Guides\ReferenceResolvers\DocumentNameResolverInterface;
23-
use phpDocumentor\Guides\Renderer\UrlGenerator\UrlGeneratorInterface;
2423

2524
use function dirname;
26-
use function trim;
2725

2826
class RenderContext
2927
{
30-
private string $destinationPath;
31-
3228
private DocumentNode $document;
3329
/** @var DocumentNode[] */
3430
private array $allDocuments;
3531

3632
private function __construct(
37-
private readonly string $outputFolder,
38-
private readonly string $currentFileName,
33+
private readonly string $destinationPath,
34+
private readonly string|null $currentFileName,
3935
private readonly FilesystemInterface $origin,
4036
private readonly FilesystemInterface $destination,
41-
private readonly UrlGeneratorInterface $urlGenerator,
42-
private readonly DocumentNameResolverInterface $documentNameResolver,
4337
private readonly string $outputFormat,
4438
private readonly ProjectNode $projectNode,
4539
) {
46-
$this->destinationPath = trim($outputFolder, '/');
4740
}
4841

4942
/** @param DocumentNode[] $allDocumentNodes */
@@ -53,8 +46,6 @@ public static function forDocument(
5346
FilesystemInterface $origin,
5447
FilesystemInterface $destination,
5548
string $destinationPath,
56-
UrlGeneratorInterface $urlGenerator,
57-
DocumentNameResolverInterface $documentNameResolver,
5849
string $ouputFormat,
5950
ProjectNode $projectNode,
6051
): self {
@@ -63,8 +54,6 @@ public static function forDocument(
6354
$documentNode->getFilePath(),
6455
$origin,
6556
$destination,
66-
$urlGenerator,
67-
$documentNameResolver,
6857
$ouputFormat,
6958
$projectNode,
7059
);
@@ -75,6 +64,23 @@ public static function forDocument(
7564
return $self;
7665
}
7766

67+
public static function forProject(
68+
ProjectNode $projectNode,
69+
FilesystemInterface $origin,
70+
FilesystemInterface $destination,
71+
string $destinationPath,
72+
string $ouputFormat,
73+
): self {
74+
return new self(
75+
$destinationPath,
76+
null,
77+
$origin,
78+
$destination,
79+
$ouputFormat,
80+
$projectNode,
81+
);
82+
}
83+
7884
/**
7985
* @param TType $default
8086
*
@@ -94,37 +100,9 @@ public function getLink(string $name): string
94100
return $link ?? '';
95101
}
96102

97-
public function canonicalUrl(string $url): string
98-
{
99-
return $this->documentNameResolver->canonicalUrl($this->getDirName(), $url);
100-
}
101-
102-
/**
103-
* Generate a canonical output URL with the configured file extension and anchor
104-
*/
105-
public function generateCanonicalOutputUrl(string $linkedDocument, string|null $anchor = null): string
106-
{
107-
if ($this->projectNode->findDocumentEntry($linkedDocument) !== null) {
108-
// todo: this is a hack, existing documents are expected to be handled like absolute links in some places
109-
$linkedDocument = '/' . $linkedDocument;
110-
}
111-
112-
$canonicalUrl = $this->documentNameResolver->canonicalUrl(
113-
$this->getDirName(),
114-
$linkedDocument,
115-
);
116-
117-
$fileUrl = $this->urlGenerator->createFileUrl($canonicalUrl, $this->outputFormat, $anchor);
118-
119-
return $this->urlGenerator->generateInternalUrl(
120-
$this,
121-
$fileUrl,
122-
);
123-
}
124-
125-
private function getDirName(): string
103+
public function getDirName(): string
126104
{
127-
$dirname = dirname($this->currentFileName);
105+
$dirname = dirname($this->getCurrentFileName());
128106

129107
if ($dirname === '.') {
130108
return '';
@@ -135,10 +113,14 @@ private function getDirName(): string
135113

136114
public function getCurrentFileName(): string
137115
{
116+
if ($this->currentFileName === null) {
117+
throw new LogicException('Cannot get current file name when not rendering a document');
118+
}
119+
138120
return $this->currentFileName;
139121
}
140122

141-
/** @return array<string, string> */
123+
/** @return array<string, string|null> */
142124
public function getLoggerInformation(): array
143125
{
144126
return [
@@ -153,29 +135,19 @@ public function getOrigin(): FilesystemInterface
153135

154136
public function getCurrentDocumentEntry(): DocumentEntryNode|null
155137
{
156-
return $this->projectNode->findDocumentEntry($this->currentFileName);
138+
return $this->projectNode->findDocumentEntry($this->getCurrentFileName());
157139
}
158140

159141
public function getDestinationPath(): string
160142
{
161143
return $this->destinationPath;
162144
}
163145

164-
public function setDestinationPath(string $path): void
165-
{
166-
$this->destinationPath = $path;
167-
}
168-
169146
public function getDestination(): FilesystemInterface
170147
{
171148
return $this->destination;
172149
}
173150

174-
public function getCurrentFileDestination(): string
175-
{
176-
return $this->destinationPath . '/' . $this->currentFileName . '.' . $this->outputFormat;
177-
}
178-
179151
public function getProjectNode(): ProjectNode
180152
{
181153
return $this->projectNode;
@@ -201,9 +173,4 @@ public function getOutputFormat(): string
201173
{
202174
return $this->outputFormat;
203175
}
204-
205-
public function getOutputFolder(): string
206-
{
207-
return $this->outputFolder;
208-
}
209176
}

packages/guides/src/Renderer/BaseTypeRenderer.php

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,12 @@
77
use League\Tactician\CommandBus;
88
use phpDocumentor\Guides\Handlers\RenderCommand;
99
use phpDocumentor\Guides\Handlers\RenderDocumentCommand;
10-
use phpDocumentor\Guides\ReferenceResolvers\DocumentNameResolverInterface;
1110
use phpDocumentor\Guides\RenderContext;
12-
use phpDocumentor\Guides\Renderer\UrlGenerator\UrlGeneratorInterface;
1311

1412
abstract class BaseTypeRenderer implements TypeRenderer
1513
{
16-
public function __construct(
17-
protected readonly CommandBus $commandBus,
18-
private readonly UrlGeneratorInterface $urlGenerator,
19-
private readonly DocumentNameResolverInterface $documentNameResolver,
20-
) {
14+
public function __construct(protected readonly CommandBus $commandBus)
15+
{
2116
}
2217

2318
public function render(RenderCommand $renderCommand): void
@@ -32,8 +27,6 @@ public function render(RenderCommand $renderCommand): void
3227
$renderCommand->getOrigin(),
3328
$renderCommand->getDestination(),
3429
$renderCommand->getDestinationPath(),
35-
$this->urlGenerator,
36-
$this->documentNameResolver,
3730
$renderCommand->getOutputFormat(),
3831
$renderCommand->getProjectNode(),
3932
),

packages/guides/src/Renderer/IntersphinxRenderer.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use phpDocumentor\Guides\Handlers\RenderCommand;
88
use phpDocumentor\Guides\ReferenceResolvers\DocumentNameResolverInterface;
9+
use phpDocumentor\Guides\RenderContext;
910
use phpDocumentor\Guides\Renderer\UrlGenerator\UrlGeneratorInterface;
1011

1112
use function json_encode;
@@ -35,10 +36,18 @@ public function render(RenderCommand $renderCommand): void
3536
];
3637
$projectNode = $renderCommand->getProjectNode();
3738

39+
$context = RenderContext::forProject(
40+
$projectNode,
41+
$renderCommand->getOrigin(),
42+
$renderCommand->getDestination(),
43+
$renderCommand->getDestinationPath(),
44+
'html',
45+
);
46+
3847
foreach ($renderCommand->getProjectNode()->getAllDocumentEntries() as $key => $documentEntry) {
3948
$url = $this->documentNameResolver->canonicalUrl(
4049
'',
41-
$this->urlGenerator->createFileUrl($documentEntry->getFile(), 'html'),
50+
$this->urlGenerator->createFileUrl($context, $documentEntry->getFile()),
4251
);
4352
$inventory['std:doc'][$key] = [
4453
$projectNode->getTitle(),
@@ -51,7 +60,7 @@ public function render(RenderCommand $renderCommand): void
5160
foreach ($renderCommand->getProjectNode()->getAllInternalTargets() as $key => $internalTarget) {
5261
$url = $this->documentNameResolver->canonicalUrl(
5362
'',
54-
$this->urlGenerator->createFileUrl($internalTarget->getDocumentPath(), 'html', $internalTarget->getAnchor()),
63+
$this->urlGenerator->createFileUrl($context, $internalTarget->getDocumentPath(), $internalTarget->getAnchor()),
5564
);
5665
$inventory['std:label'][$key] = [
5766
$projectNode->getTitle(),

packages/guides/src/Renderer/UrlGenerator/AbsoluteUrlGenerator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ public function generateInternalPathFromRelativeUrl(
2323
RenderContext $renderContext,
2424
string $canonicalUrl,
2525
): string {
26-
if ($renderContext->getOutputFolder() === '') {
26+
if ($renderContext->getDestinationPath() === '') {
2727
return $canonicalUrl;
2828
}
2929

30-
return rtrim($renderContext->getOutputFolder(), '/') . '/' . $canonicalUrl;
30+
return rtrim($renderContext->getDestinationPath(), '/') . '/' . $canonicalUrl;
3131
}
3232
}

0 commit comments

Comments
 (0)