Skip to content

Commit

Permalink
Merge pull request #645 from phpDocumentor/task/serviceless-context
Browse files Browse the repository at this point in the history
!!![TASK] Remove all services from render context
  • Loading branch information
jaapio authored Oct 31, 2023
2 parents db823ec + 98eca7c commit 5aa985a
Show file tree
Hide file tree
Showing 21 changed files with 197 additions and 201 deletions.
2 changes: 1 addition & 1 deletion packages/guides/src/Handlers/RenderDocumentCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ public function getContext(): RenderContext

public function getFileDestination(): string
{
return $this->renderContext->getCurrentFileDestination();
return $this->renderContext->getDestinationPath() . '/' . $this->renderContext->getCurrentFileName() . '.' . $this->renderContext->getOutputFormat();
}
}
9 changes: 6 additions & 3 deletions packages/guides/src/NodeRenderers/Html/MenuEntryRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@
use phpDocumentor\Guides\Nodes\Menu\MenuEntryNode;
use phpDocumentor\Guides\Nodes\Node;
use phpDocumentor\Guides\RenderContext;
use phpDocumentor\Guides\Renderer\UrlGenerator\UrlGeneratorInterface;
use phpDocumentor\Guides\TemplateRenderer;

/** @implements NodeRenderer<MenuEntryNode> */
final class MenuEntryRenderer implements NodeRenderer
{
public function __construct(private readonly TemplateRenderer $renderer)
{
public function __construct(
private readonly TemplateRenderer $renderer,
private readonly UrlGeneratorInterface $urlGenerator,
) {
}

public function supports(Node $node): bool
Expand All @@ -28,7 +31,7 @@ public function render(Node $node, RenderContext $renderContext): string
$renderContext,
'body/menu/menu-item.html.twig',
[
'url' => $renderContext->generateCanonicalOutputUrl($node->getUrl(), $node->getValue()->getId()),
'url' => $this->urlGenerator->generateCanonicalOutputUrl($renderContext, $node->getUrl(), $node->getValue()->getId()),
'node' => $node,
],
);
Expand Down
5 changes: 1 addition & 4 deletions packages/guides/src/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,7 @@ public function registerStrategy(MarkupLanguageParser $strategy): void
$this->parserStrategies[] = $strategy;
}

/**
* @psalm-assert ParserContext $this->parserContext
* @psalm-assert Metas $this->metas
*/
/** @psalm-assert ParserContext $this->parserContext */
public function prepare(
FilesystemInterface|null $origin,
string $sourcePath,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use phpDocumentor\Guides\Nodes\Inline\LinkInlineNode;
use phpDocumentor\Guides\RenderContext;
use phpDocumentor\Guides\Renderer\UrlGenerator\UrlGeneratorInterface;

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

public function __construct(
private readonly AnchorReducer $anchorReducer,
private readonly UrlGeneratorInterface $urlGenerator,
) {
}

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

$node->setUrl($renderContext->generateCanonicalOutputUrl($target->getDocumentPath(), $target->getAnchor()));
$node->setUrl($this->urlGenerator->generateCanonicalOutputUrl($renderContext, $target->getDocumentPath(), $target->getAnchor()));
if ($node->getValue() === '') {
$node->setValue($target->getTitle() ?? '');
}
Expand Down
11 changes: 9 additions & 2 deletions packages/guides/src/ReferenceResolvers/DocReferenceResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,32 @@
use phpDocumentor\Guides\Nodes\Inline\DocReferenceNode;
use phpDocumentor\Guides\Nodes\Inline\LinkInlineNode;
use phpDocumentor\Guides\RenderContext;
use phpDocumentor\Guides\Renderer\UrlGenerator\UrlGeneratorInterface;

class DocReferenceResolver implements ReferenceResolver
{
public final const PRIORITY = 1000;

public function __construct(
private readonly UrlGeneratorInterface $urlGenerator,
private readonly DocumentNameResolverInterface $documentNameResolver,
) {
}

public function resolve(LinkInlineNode $node, RenderContext $renderContext): bool
{
if (!$node instanceof DocReferenceNode) {
return false;
}

$document = $renderContext->getProjectNode()->findDocumentEntry(
$renderContext->canonicalUrl($node->getTargetReference()),
$this->documentNameResolver->canonicalUrl($renderContext->getDirName(), $node->getTargetReference()),
);
if ($document === null) {
return false;
}

$node->setUrl($renderContext->generateCanonicalOutputUrl($document->getFile()));
$node->setUrl($this->urlGenerator->generateCanonicalOutputUrl($renderContext, $document->getFile()));
if ($node->getValue() === '') {
$node->setValue($document->getTitle()->toString());
}
Expand Down
89 changes: 28 additions & 61 deletions packages/guides/src/RenderContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,35 +15,28 @@

use Exception;
use League\Flysystem\FilesystemInterface;
use LogicException;
use phpDocumentor\Guides\Nodes\DocumentNode;
use phpDocumentor\Guides\Nodes\DocumentTree\DocumentEntryNode;
use phpDocumentor\Guides\Nodes\Node;
use phpDocumentor\Guides\Nodes\ProjectNode;
use phpDocumentor\Guides\ReferenceResolvers\DocumentNameResolverInterface;
use phpDocumentor\Guides\Renderer\UrlGenerator\UrlGeneratorInterface;

use function dirname;
use function trim;

class RenderContext
{
private string $destinationPath;

private DocumentNode $document;
/** @var DocumentNode[] */
private array $allDocuments;

private function __construct(
private readonly string $outputFolder,
private readonly string $currentFileName,
private readonly string $destinationPath,
private readonly string|null $currentFileName,
private readonly FilesystemInterface $origin,
private readonly FilesystemInterface $destination,
private readonly UrlGeneratorInterface $urlGenerator,
private readonly DocumentNameResolverInterface $documentNameResolver,
private readonly string $outputFormat,
private readonly ProjectNode $projectNode,
) {
$this->destinationPath = trim($outputFolder, '/');
}

/** @param DocumentNode[] $allDocumentNodes */
Expand All @@ -53,8 +46,6 @@ public static function forDocument(
FilesystemInterface $origin,
FilesystemInterface $destination,
string $destinationPath,
UrlGeneratorInterface $urlGenerator,
DocumentNameResolverInterface $documentNameResolver,
string $ouputFormat,
ProjectNode $projectNode,
): self {
Expand All @@ -63,8 +54,6 @@ public static function forDocument(
$documentNode->getFilePath(),
$origin,
$destination,
$urlGenerator,
$documentNameResolver,
$ouputFormat,
$projectNode,
);
Expand All @@ -75,6 +64,23 @@ public static function forDocument(
return $self;
}

public static function forProject(
ProjectNode $projectNode,
FilesystemInterface $origin,
FilesystemInterface $destination,
string $destinationPath,
string $ouputFormat,
): self {
return new self(
$destinationPath,
null,
$origin,
$destination,
$ouputFormat,
$projectNode,
);
}

/**
* @param TType $default
*
Expand All @@ -94,37 +100,9 @@ public function getLink(string $name): string
return $link ?? '';
}

public function canonicalUrl(string $url): string
{
return $this->documentNameResolver->canonicalUrl($this->getDirName(), $url);
}

/**
* Generate a canonical output URL with the configured file extension and anchor
*/
public function generateCanonicalOutputUrl(string $linkedDocument, string|null $anchor = null): string
{
if ($this->projectNode->findDocumentEntry($linkedDocument) !== null) {
// todo: this is a hack, existing documents are expected to be handled like absolute links in some places
$linkedDocument = '/' . $linkedDocument;
}

$canonicalUrl = $this->documentNameResolver->canonicalUrl(
$this->getDirName(),
$linkedDocument,
);

$fileUrl = $this->urlGenerator->createFileUrl($canonicalUrl, $this->outputFormat, $anchor);

return $this->urlGenerator->generateInternalUrl(
$this,
$fileUrl,
);
}

private function getDirName(): string
public function getDirName(): string
{
$dirname = dirname($this->currentFileName);
$dirname = dirname($this->getCurrentFileName());

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

public function getCurrentFileName(): string
{
if ($this->currentFileName === null) {
throw new LogicException('Cannot get current file name when not rendering a document');
}

return $this->currentFileName;
}

/** @return array<string, string> */
/** @return array<string, string|null> */
public function getLoggerInformation(): array
{
return [
Expand All @@ -153,29 +135,19 @@ public function getOrigin(): FilesystemInterface

public function getCurrentDocumentEntry(): DocumentEntryNode|null
{
return $this->projectNode->findDocumentEntry($this->currentFileName);
return $this->projectNode->findDocumentEntry($this->getCurrentFileName());
}

public function getDestinationPath(): string
{
return $this->destinationPath;
}

public function setDestinationPath(string $path): void
{
$this->destinationPath = $path;
}

public function getDestination(): FilesystemInterface
{
return $this->destination;
}

public function getCurrentFileDestination(): string
{
return $this->destinationPath . '/' . $this->currentFileName . '.' . $this->outputFormat;
}

public function getProjectNode(): ProjectNode
{
return $this->projectNode;
Expand All @@ -201,9 +173,4 @@ public function getOutputFormat(): string
{
return $this->outputFormat;
}

public function getOutputFolder(): string
{
return $this->outputFolder;
}
}
11 changes: 2 additions & 9 deletions packages/guides/src/Renderer/BaseTypeRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,12 @@
use League\Tactician\CommandBus;
use phpDocumentor\Guides\Handlers\RenderCommand;
use phpDocumentor\Guides\Handlers\RenderDocumentCommand;
use phpDocumentor\Guides\ReferenceResolvers\DocumentNameResolverInterface;
use phpDocumentor\Guides\RenderContext;
use phpDocumentor\Guides\Renderer\UrlGenerator\UrlGeneratorInterface;

abstract class BaseTypeRenderer implements TypeRenderer
{
public function __construct(
protected readonly CommandBus $commandBus,
private readonly UrlGeneratorInterface $urlGenerator,
private readonly DocumentNameResolverInterface $documentNameResolver,
) {
public function __construct(protected readonly CommandBus $commandBus)
{
}

public function render(RenderCommand $renderCommand): void
Expand All @@ -32,8 +27,6 @@ public function render(RenderCommand $renderCommand): void
$renderCommand->getOrigin(),
$renderCommand->getDestination(),
$renderCommand->getDestinationPath(),
$this->urlGenerator,
$this->documentNameResolver,
$renderCommand->getOutputFormat(),
$renderCommand->getProjectNode(),
),
Expand Down
13 changes: 11 additions & 2 deletions packages/guides/src/Renderer/IntersphinxRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use phpDocumentor\Guides\Handlers\RenderCommand;
use phpDocumentor\Guides\ReferenceResolvers\DocumentNameResolverInterface;
use phpDocumentor\Guides\RenderContext;
use phpDocumentor\Guides\Renderer\UrlGenerator\UrlGeneratorInterface;

use function json_encode;
Expand Down Expand Up @@ -35,10 +36,18 @@ public function render(RenderCommand $renderCommand): void
];
$projectNode = $renderCommand->getProjectNode();

$context = RenderContext::forProject(
$projectNode,
$renderCommand->getOrigin(),
$renderCommand->getDestination(),
$renderCommand->getDestinationPath(),
'html',
);

foreach ($renderCommand->getProjectNode()->getAllDocumentEntries() as $key => $documentEntry) {
$url = $this->documentNameResolver->canonicalUrl(
'',
$this->urlGenerator->createFileUrl($documentEntry->getFile(), 'html'),
$this->urlGenerator->createFileUrl($context, $documentEntry->getFile()),
);
$inventory['std:doc'][$key] = [
$projectNode->getTitle(),
Expand All @@ -51,7 +60,7 @@ public function render(RenderCommand $renderCommand): void
foreach ($renderCommand->getProjectNode()->getAllInternalTargets() as $key => $internalTarget) {
$url = $this->documentNameResolver->canonicalUrl(
'',
$this->urlGenerator->createFileUrl($internalTarget->getDocumentPath(), 'html', $internalTarget->getAnchor()),
$this->urlGenerator->createFileUrl($context, $internalTarget->getDocumentPath(), $internalTarget->getAnchor()),
);
$inventory['std:label'][$key] = [
$projectNode->getTitle(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ public function generateInternalPathFromRelativeUrl(
RenderContext $renderContext,
string $canonicalUrl,
): string {
if ($renderContext->getOutputFolder() === '') {
if ($renderContext->getDestinationPath() === '') {
return $canonicalUrl;
}

return rtrim($renderContext->getOutputFolder(), '/') . '/' . $canonicalUrl;
return rtrim($renderContext->getDestinationPath(), '/') . '/' . $canonicalUrl;
}
}
Loading

0 comments on commit 5aa985a

Please sign in to comment.