Skip to content

Commit e1d1955

Browse files
committed
Improve syntax
1 parent 05a215d commit e1d1955

File tree

8 files changed

+70
-54
lines changed

8 files changed

+70
-54
lines changed

src/KernelFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ private static function getReferences(BuildConfig $buildConfig): array
103103
{
104104
return [
105105
new SymfonyReferences\ClassReference($buildConfig->getSymfonyRepositoryUrl()),
106-
new SymfonyReferences\FileReference($buildConfig->getSymfonyRepositoryUrl()),
106+
new SymfonyReferences\GithubReference($buildConfig->getSymfonyVersion()),
107107
new SymfonyReferences\MethodReference($buildConfig->getSymfonyRepositoryUrl()),
108108
new SymfonyReferences\NamespaceReference($buildConfig->getSymfonyRepositoryUrl()),
109109
new SymfonyReferences\PhpFunctionReference($buildConfig->getPhpDocUrl()),

src/Reference/FileReference.php

Lines changed: 0 additions & 48 deletions
This file was deleted.

src/Reference/GithubReference.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Docs Builder package.
5+
* (c) Ryan Weaver <[email protected]>
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
namespace SymfonyDocsBuilder\Reference;
11+
12+
use Doctrine\RST\Environment;
13+
use Doctrine\RST\References\Reference;
14+
use Doctrine\RST\References\ResolvedReference;
15+
use function Symfony\Component\String\u;
16+
17+
class GithubReference extends Reference
18+
{
19+
private $symfonyVersion;
20+
21+
public function __construct(string $symfonyVersion)
22+
{
23+
$this->symfonyVersion = $symfonyVersion;
24+
}
25+
26+
public function getName(): string
27+
{
28+
return 'github';
29+
}
30+
31+
public function resolve(Environment $environment, string $data): ResolvedReference
32+
{
33+
$match = u($data)->match('/(?<linkName>.+)::(?<githubRepository>.+):(?<relativePath>.+)::/');
34+
35+
if (!$match) {
36+
throw new \RuntimeException(sprintf('Malformed method reference "%s" in file "%s"', $data, $environment->getCurrentFileName()));
37+
}
38+
39+
$linkName = $match['linkName'];
40+
$githubRepository = $match['githubRepository'];
41+
$relativePath = $match['relativePath'];
42+
43+
$resolvedRelativePath = str_replace('{symfonyVersion}', $this->symfonyVersion, $relativePath);
44+
45+
$githubLink = sprintf(
46+
'https://github.com/%s/blob/%s',
47+
$githubRepository,
48+
$resolvedRelativePath
49+
);
50+
51+
return new ResolvedReference(
52+
$environment->getCurrentFileName(),
53+
$linkName,
54+
$githubLink,
55+
[],
56+
[
57+
'title' => $resolvedRelativePath,
58+
]
59+
);
60+
}
61+
}

tests/IntegrationTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,8 +211,8 @@ public function parserUnitBlockProvider()
211211
'blockName' => 'references/class',
212212
];
213213

214-
yield 'file-reference' => [
215-
'blockName' => 'references/file',
214+
yield 'github-reference' => [
215+
'blockName' => 'references/github',
216216
];
217217

218218
yield 'namespace-reference' => [

tests/fixtures/expected/blocks/references/file.html

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<p><a href="https://github.com/symfony/symfony/blob/4.0/src/Symfony/Component/Notifier/Bridge/AmazonSns/README.md" class="reference external" title="4.0/src/Symfony/Component/Notifier/Bridge/AmazonSns/README.md" rel="external noopener noreferrer" target="_blank">First link</a></p>
2+
3+
<p><a href="https://github.com/foo/bar/blob/4.0/src/baz" class="reference external" title="4.0/src/baz" rel="external noopener noreferrer" target="_blank">Link with branch substitution</a></p>

tests/fixtures/source/blocks/references/file.rst

Lines changed: 0 additions & 2 deletions
This file was deleted.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
:github:`First link ::symfony/symfony:4.0/src/Symfony/Component/Notifier/Bridge/AmazonSns/README.md::`
2+
3+
:github:`Link with branch substitution ::foo/bar:{symfonyVersion}/src/baz::`

0 commit comments

Comments
 (0)