Skip to content

Commit 544f4bd

Browse files
committed
feature #149 Replace {version} in URLs (alamirault)
This PR was squashed before being merged into the main branch. Discussion ---------- Replace `{version}` in URLs In order to make link on symfony codebase, even if is not a class or a method reference Can be usefull in this case for example symfony/symfony-docs#17902 Commits ------- 3d54708 Replace `{version}` in URLs
2 parents 2edacca + 3d54708 commit 544f4bd

File tree

6 files changed

+28
-6
lines changed

6 files changed

+28
-6
lines changed

src/KernelFactory.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ public static function createKernel(BuildConfig $buildConfig, ?UrlChecker $urlCh
4444
new SymfonyHTMLFormat(
4545
$configuration->getTemplateRenderer(),
4646
$configuration->getFormat(),
47-
$urlChecker
47+
$urlChecker,
48+
$buildConfig->getSymfonyVersion()
4849
)
4950
);
5051

src/Renderers/SpanNodeRenderer.php

+11-3
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,27 @@ class SpanNodeRenderer extends AbstractSpanNodeRenderer
2525
private $decoratedSpanNodeRenderer;
2626
/** @var UrlChecker|null */
2727
private $urlChecker;
28+
private $symfonyVersion;
2829

2930
public function __construct(
3031
Environment $environment,
3132
SpanNode $span,
3233
BaseSpanNodeRenderer $decoratedSpanNodeRenderer,
33-
?UrlChecker $urlChecker = null
34-
) {
34+
?UrlChecker $urlChecker = null,
35+
string $symfonyVersion = null
36+
)
37+
{
3538
parent::__construct($environment, $span);
3639

3740
$this->decoratedSpanNodeRenderer = $decoratedSpanNodeRenderer;
3841
$this->urlChecker = $urlChecker;
42+
$this->symfonyVersion = $symfonyVersion;
3943
}
4044

4145
/** @inheritDoc */
4246
public function link(?string $url, string $title, array $attributes = []): string
4347
{
44-
$url = (string)$url;
48+
$url = (string) $url;
4549

4650
if (
4751
$this->urlChecker &&
@@ -55,6 +59,10 @@ public function link(?string $url, string $title, array $attributes = []): strin
5559
$attributes = $this->addAttributesForUnsafeUrl($attributes);
5660
}
5761

62+
if (null !== $this->symfonyVersion) {
63+
$url = u($url)->replace('{version}', $this->symfonyVersion)->toString();
64+
}
65+
5866
return $this->decoratedSpanNodeRenderer->link($url, $title, $attributes);
5967
}
6068

src/SymfonyHTMLFormat.php

+5-2
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,14 @@ final class SymfonyHTMLFormat implements Format
2929
private $htmlFormat;
3030
/** @var UrlChecker|null */
3131
private $urlChecker;
32+
private $symfonyVersion;
3233

33-
public function __construct(TemplateRenderer $templateRenderer, Format $HTMLFormat, ?UrlChecker $urlChecker = null)
34+
public function __construct(TemplateRenderer $templateRenderer, Format $HTMLFormat, ?UrlChecker $urlChecker = null, string $symfonyVersion = null)
3435
{
3536
$this->templateRenderer = $templateRenderer;
3637
$this->htmlFormat = $HTMLFormat;
3738
$this->urlChecker = $urlChecker;
39+
$this->symfonyVersion = $symfonyVersion;
3840
}
3941

4042
public function getFileExtension(): string
@@ -69,7 +71,8 @@ function (SpanNode $node) {
6971
$node->getEnvironment(),
7072
$node,
7173
new BaseSpanNodeRenderer($node->getEnvironment(), $node, $this->templateRenderer),
72-
$this->urlChecker
74+
$this->urlChecker,
75+
$this->symfonyVersion
7376
);
7477
}
7578
);

tests/IntegrationTest.php

+4
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,10 @@ public function parserUnitBlockProvider()
137137
'blockName' => 'nodes/figure',
138138
];
139139

140+
yield 'span-link' => [
141+
'blockName' => 'nodes/span-link',
142+
];
143+
140144
yield 'title' => [
141145
'blockName' => 'nodes/title',
142146
];
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<p><a href="https://github.com/symfony/amazon-sns-notifier/blob/4.0/README.md" class="reference external" rel="external noopener noreferrer" target="_blank">README file with version substitution</a></p>
2+
3+
<p><a href="https://github.com/symfony/amazon-sns-notifier/blob/version/README.md" class="reference external" rel="external noopener noreferrer" target="_blank">README file without version substitution</a></p>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
`README file with version substitution <https://github.com/symfony/amazon-sns-notifier/blob/{version}/README.md>`_
2+
3+
`README file without version substitution <https://github.com/symfony/amazon-sns-notifier/blob/version/README.md>`_

0 commit comments

Comments
 (0)