Skip to content

Commit 653be76

Browse files
committed
[TASK] Move Document URL calculations from the RenderContext
It seems like the method is quite hacky, more clean up is needed
1 parent d9d1a73 commit 653be76

File tree

4 files changed

+99
-17
lines changed

4 files changed

+99
-17
lines changed

packages/guides/src/RenderContext.php

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,12 @@
1515

1616
use Exception;
1717
use League\Flysystem\FilesystemInterface;
18-
use League\Uri\Uri;
19-
use League\Uri\UriInfo;
2018
use phpDocumentor\Guides\Nodes\DocumentNode;
2119
use phpDocumentor\Guides\Nodes\DocumentTree\DocumentEntryNode;
2220
use phpDocumentor\Guides\Nodes\Node;
2321
use phpDocumentor\Guides\Nodes\ProjectNode;
2422

2523
use function dirname;
26-
use function ltrim;
2724
use function trim;
2825

2926
class RenderContext
@@ -99,20 +96,14 @@ public function canonicalUrl(string $url): string
9996

10097
public function relativeDocUrl(string $filename, string|null $anchor = null): string
10198
{
102-
if (UriInfo::isAbsolutePath(Uri::createFromString($filename))) {
103-
return $this->destinationPath . $this->urlGenerator->createFileUrl($filename, $this->outputFormat, $anchor);
104-
}
105-
106-
$baseUrl = ltrim($this->urlGenerator->absoluteUrl($this->destinationPath, $this->getDirName()), '/');
107-
108-
if ($this->projectNode->findDocumentEntry($filename) !== null) {
109-
return $this->destinationPath . '/'
110-
. $this->urlGenerator->createFileUrl($filename, $this->outputFormat, $anchor);
111-
}
112-
113-
return $this->urlGenerator->canonicalUrl(
114-
$baseUrl,
115-
$this->urlGenerator->createFileUrl($filename, $this->outputFormat, $anchor),
99+
return $this->urlGenerator->generateOutputUrlFromDocumentPath(
100+
$this->getDirName(),
101+
$this->destinationPath,
102+
// Todo: it does not really make sense to treat relavtive paths that are found in the project node differently?
103+
$this->projectNode->findDocumentEntry($filename) !== null,
104+
$filename,
105+
$this->outputFormat,
106+
$anchor,
116107
);
117108
}
118109

packages/guides/src/UrlGenerator.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
namespace phpDocumentor\Guides;
1515

16+
use League\Uri\Uri;
1617
use League\Uri\UriInfo;
1718

1819
use function array_pop;
@@ -84,4 +85,29 @@ public function createFileUrl(string $filename, string $outputFormat = 'html', s
8485
return $filename . '.' . $outputFormat .
8586
($anchor !== null ? '#' . $anchor : '');
8687
}
88+
89+
public function generateOutputUrlFromDocumentPath(
90+
string $currentDirectory,
91+
string $destinationPath,
92+
bool $validDocumentEntry,
93+
string $linkedDocument,
94+
string $outputFormat,
95+
string|null $anchor = null,
96+
): string {
97+
if (UriInfo::isAbsolutePath(Uri::createFromString($linkedDocument))) {
98+
return $destinationPath . $this->createFileUrl($linkedDocument, $outputFormat, $anchor);
99+
}
100+
101+
$baseUrl = ltrim($this->absoluteUrl($destinationPath, $currentDirectory), '/');
102+
103+
if ($validDocumentEntry) {
104+
return $destinationPath . '/'
105+
. $this->createFileUrl($linkedDocument, $outputFormat, $anchor);
106+
}
107+
108+
return $this->canonicalUrl(
109+
$baseUrl,
110+
$this->createFileUrl($linkedDocument, $outputFormat, $anchor),
111+
);
112+
}
87113
}

packages/guides/src/UrlGeneratorInterface.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,13 @@ public function canonicalUrl(string $basePath, string $url): string;
3030
* Create a url with a file ending derived from the output format
3131
*/
3232
public function createFileUrl(string $filename, string $outputFormat = 'html', string|null $anchor = null): string;
33+
34+
public function generateOutputUrlFromDocumentPath(
35+
string $currentDirectory,
36+
string $destinationPath,
37+
bool $validDocumentEntry,
38+
string $linkedDocument,
39+
string $outputFormat,
40+
string|null $anchor = null,
41+
): string;
3342
}

packages/guides/tests/unit/UrlGeneratorTest.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,4 +137,60 @@ public static function abstractUrlProvider(): array
137137
],
138138
];
139139
}
140+
141+
#[DataProvider('documentPathProvider')]
142+
public function testRelativeDocUrl(
143+
string $currentDirectory,
144+
string $destinationPath,
145+
bool $validDocumentEntry,
146+
string $linkedDocument,
147+
string $result,
148+
string|null $anchor = null,
149+
): void {
150+
$urlGenerator = new UrlGenerator();
151+
self::assertSame($result, $urlGenerator->generateOutputUrlFromDocumentPath(
152+
$currentDirectory,
153+
$destinationPath,
154+
$validDocumentEntry,
155+
$linkedDocument,
156+
'txt',
157+
$anchor,
158+
));
159+
}
160+
161+
/** @return array<string, array<string, bool|string>> */
162+
public static function documentPathProvider(): array
163+
{
164+
return [
165+
'relative document' => [
166+
'currentDirectory' => 'getting-started',
167+
'destinationPath' => 'guide',
168+
'validDocumentEntry' => false,
169+
'linkedDocument' => 'installing',
170+
'result' => 'guide/getting-started/installing.txt',
171+
],
172+
'absolute document path' => [
173+
'currentDirectory' => 'getting-started',
174+
'destinationPath' => 'guide',
175+
'validDocumentEntry' => false,
176+
'linkedDocument' => '/installing',
177+
'result' => 'guide/installing.txt',
178+
],
179+
'relative document path with anchor' => [
180+
'currentDirectory' => 'getting-started',
181+
'destinationPath' => 'guide',
182+
'validDocumentEntry' => true,
183+
'linkedDocument' => 'getting-started/configuration',
184+
'result' => 'guide/getting-started/configuration.txt#composer',
185+
'anchor' => 'composer',
186+
],
187+
'relative document path up in directory' => [
188+
'currentDirectory' => 'getting-started',
189+
'destinationPath' => 'guide',
190+
'validDocumentEntry' => false,
191+
'linkedDocument' => '../references/installing',
192+
'result' => 'guide/references/installing.txt',
193+
],
194+
];
195+
}
140196
}

0 commit comments

Comments
 (0)