Skip to content

Commit e8dadfa

Browse files
authored
[BUGFIX] Respect typo3-core-preferred for t3src roles (#607)
Resolves #603
1 parent 611538d commit e8dadfa

File tree

10 files changed

+107
-54
lines changed

10 files changed

+107
-54
lines changed

packages/typo3-docs-theme/resources/config/typo3-docs-theme.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
use T3Docs\Typo3DocsTheme\EventListeners\IgnoreLocalizationsFolders;
2929
use T3Docs\Typo3DocsTheme\EventListeners\TestingModeActivator;
3030
use T3Docs\Typo3DocsTheme\Inventory\Typo3InventoryRepository;
31+
use T3Docs\Typo3DocsTheme\Inventory\Typo3VersionService;
3132
use T3Docs\Typo3DocsTheme\Parser\ExtendedInterlinkParser;
3233
use T3Docs\Typo3DocsTheme\Parser\Productions\FieldList\EditOnGitHubFieldListItemRule;
3334
use T3Docs\Typo3DocsTheme\Parser\Productions\FieldList\TemplateFieldListItemRule;
@@ -133,6 +134,8 @@
133134
->tag('twig.extension')
134135
->autowire()
135136

137+
->set(Typo3VersionService::class)
138+
136139
// Register Event Listeners
137140
->set(AddThemeSettingsToProjectNode::class)
138141
->tag('event_listener', ['event' => PostProjectNodeCreated::class])
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{% apply spaceless %}
2-
<a class="reference external" href="https://github.com/typo3/typo3/blob/{{ getSettings('typo3_version') }}/typo3/sysext/{{ node.value }}" title="EXT:{{ node.value }} on GitHub">EXT:{{ node.value }} (GitHub)</a>
2+
<a class="reference external" href="https://github.com/typo3/typo3/blob/{{ getTYPO3Version() }}/typo3/sysext/{{ node.value }}" title="EXT:{{ node.value }} on GitHub">EXT:{{ node.value }} (GitHub)</a>
33
{% endapply %}

packages/typo3-docs-theme/src/Inventory/Typo3InventoryRepository.php

Lines changed: 6 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
use phpDocumentor\Guides\RenderContext;
1515
use Psr\Log\LoggerInterface;
1616
use Symfony\Component\HttpClient\Exception\ClientException;
17-
use T3Docs\Typo3DocsTheme\Settings\Typo3DocsThemeSettings;
1817
use T3Docs\VersionHandling\DefaultInventories;
1918
use T3Docs\VersionHandling\Typo3VersionMapping;
2019

@@ -26,14 +25,6 @@ final class Typo3InventoryRepository implements InventoryRepository
2625
* https://getcomposer.org/doc/04-schema.md#name
2726
*/
2827
private const EXTENSION_INTERLINK_REGEX = '/^([^\/\s]+)\/([^\/\s]+)(\/([^\/\s]+))?$/';
29-
/**
30-
* @see https://regex101.com/r/Kx7VyS/2
31-
*/
32-
private const VERSION_MINOR_REGEX = '/^(\d+\.\d+)\.\d+$/';
33-
/**
34-
* @see https://regex101.com/r/Ljhv1I/1
35-
*/
36-
private const VERSION_MAJOR_REGEX = '/^(\d+)(\.\d+)?(\.\d+)?$/';
3728

3829
/** @var array<string, Inventory> */
3930
private array $inventories = [];
@@ -47,7 +38,7 @@ public function __construct(
4738
// We have to use the specific implementation as the interface does not expose the needed methods
4839
private readonly DefaultInventoryLoader $inventoryLoader,
4940
private readonly JsonLoader $jsonLoader,
50-
private readonly Typo3DocsThemeSettings $settings,
41+
private readonly Typo3VersionService $typo3VersionService,
5142
array $inventoryConfigs,
5243
) {
5344
foreach ($inventoryConfigs as $inventory) {
@@ -64,47 +55,11 @@ public function __construct(
6455
$mappedUrl = str_replace('{typo3_version}', $versionMapping->getVersion(), $url);
6556
$this->addInventory($id . '-' . $versionMapping->value, $mappedUrl, false);
6657
}
67-
$preferred = $this->getPreferredVersion();
58+
$preferred = $this->typo3VersionService->getPreferredVersion();
6859
$this->addInventory($id, str_replace('{typo3_version}', $preferred, $url), false);
6960
}
7061
}
7162

72-
private function getPreferredVersion(): string
73-
{
74-
if ($this->settings->hasSettings('typo3_core_preferred')) {
75-
$preferred = $this->settings->getSettings('typo3_core_preferred');
76-
return $this->resolveVersion($preferred);
77-
}
78-
return Typo3VersionMapping::getDefault()->getVersion();
79-
}
80-
81-
private function resolveCoreVersion(string $versionName): string
82-
{
83-
$version = ltrim($versionName, 'v');
84-
if (preg_match(self::VERSION_MAJOR_REGEX, $version, $matches)) {
85-
$version = $matches[1];
86-
}
87-
$version = Typo3VersionMapping::tryFrom($version)?->getVersion() ?? $version;
88-
89-
return $this->resolveVersion($version);
90-
}
91-
92-
private function resolveVersion(string $versionName): string
93-
{
94-
$version = trim($versionName, 'v');
95-
if (preg_match(self::VERSION_MINOR_REGEX, $version, $matches)) {
96-
return $matches[1];
97-
}
98-
$mappedVersion = Typo3VersionMapping::tryFrom($version);
99-
if ($mappedVersion !== null) {
100-
return $mappedVersion->getVersion();
101-
}
102-
if ($version === '') {
103-
return Typo3VersionMapping::tryFrom('stable')->getVersion();
104-
}
105-
return $version;
106-
}
107-
10863
private function addInventory(string $key, string $url, bool $overrideExisting): void
10964
{
11065
$reducedKey = $this->anchorNormalizer->reduceAnchor($key);
@@ -145,19 +100,19 @@ private function loadInventoryFromComposerExtension(string $reducedKey, string $
145100
{
146101
try {
147102
if ($match1 === 'typo3') {
148-
$version ??= $this->getPreferredVersion();
149-
$version = $this->resolveCoreVersion($version);
103+
$version ??= $this->typo3VersionService->getPreferredVersion();
104+
$version = $this->typo3VersionService->resolveCoreVersion($version);
150105
if ($match2 === 'cms-core') {
151106
$version = 'main';
152107
}
153108
$inventoryUrl = sprintf("https://docs.typo3.org/c/%s/%s/%s/en-us/", $match1, $match2, $version);
154109
} elseif($defaultInventory = DefaultInventories::tryFrom($match1)) {
155110
// we do not have a composer name here but a default inventory with a version, for example "t3coreapi/12.4"
156-
$version = $this->resolveCoreVersion($match2);
111+
$version = $this->typo3VersionService->resolveCoreVersion($match2);
157112
$inventoryUrl = str_replace('{typo3_version}', $version, $defaultInventory->getUrl());
158113
} else {
159114
$version ??= 'main';
160-
$version = $this->resolveVersion($version);
115+
$version = $this->typo3VersionService->resolveVersion($version);
161116
$inventoryUrl = sprintf("https://docs.typo3.org/p/%s/%s/%s/en-us/", $match1, $match2, $version);
162117
}
163118
$json = $this->jsonLoader->loadJsonFromUrl($inventoryUrl . 'objects.inv.json');
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
namespace T3Docs\Typo3DocsTheme\Inventory;
4+
5+
use T3Docs\Typo3DocsTheme\Settings\Typo3DocsThemeSettings;
6+
use T3Docs\VersionHandling\Typo3VersionMapping;
7+
8+
class Typo3VersionService
9+
{
10+
/**
11+
* @see https://regex101.com/r/Kx7VyS/2
12+
*/
13+
private const VERSION_MINOR_REGEX = '/^(\d+\.\d+)\.\d+$/';
14+
/**
15+
* @see https://regex101.com/r/Ljhv1I/1
16+
*/
17+
private const VERSION_MAJOR_REGEX = '/^(\d+)(\.\d+)?(\.\d+)?$/';
18+
public function __construct(
19+
private readonly Typo3DocsThemeSettings $settings,
20+
) {}
21+
22+
public function getPreferredVersion(): string
23+
{
24+
if ($this->settings->hasSettings('typo3_core_preferred')) {
25+
$preferred = $this->settings->getSettings('typo3_core_preferred');
26+
return $this->resolveVersion($preferred);
27+
}
28+
return Typo3VersionMapping::getDefault()->getVersion();
29+
}
30+
31+
public function resolveCoreVersion(string $versionName): string
32+
{
33+
$version = ltrim($versionName, 'v');
34+
if (preg_match(self::VERSION_MAJOR_REGEX, $version, $matches)) {
35+
$version = $matches[1];
36+
}
37+
$version = Typo3VersionMapping::tryFrom($version)?->getVersion() ?? $version;
38+
39+
return $this->resolveVersion($version);
40+
}
41+
42+
public function resolveVersion(string $versionName): string
43+
{
44+
$version = trim($versionName, 'v');
45+
if (preg_match(self::VERSION_MINOR_REGEX, $version, $matches)) {
46+
return $matches[1];
47+
}
48+
$mappedVersion = Typo3VersionMapping::tryFrom($version);
49+
if ($mappedVersion !== null) {
50+
return $mappedVersion->getVersion();
51+
}
52+
if ($version === '') {
53+
return Typo3VersionMapping::tryFrom('stable')->getVersion();
54+
}
55+
return $version;
56+
}
57+
58+
}

packages/typo3-docs-theme/src/Twig/TwigExtension.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use RuntimeException;
2121
use T3Docs\GuidesPhpDomain\Nodes\PhpComponentNode;
2222
use T3Docs\GuidesPhpDomain\Nodes\PhpMemberNode;
23+
use T3Docs\Typo3DocsTheme\Inventory\Typo3VersionService;
2324
use T3Docs\Typo3DocsTheme\Nodes\Metadata\EditOnGitHubNode;
2425
use T3Docs\Typo3DocsTheme\Nodes\Metadata\TemplateNode;
2526
use T3Docs\Typo3DocsTheme\Nodes\PageLinkNode;
@@ -37,6 +38,7 @@ public function __construct(
3738
private readonly UrlGeneratorInterface $urlGenerator,
3839
private readonly Typo3DocsThemeSettings $themeSettings,
3940
private readonly DocumentNameResolverInterface $documentNameResolver,
41+
private readonly Typo3VersionService $typo3VersionService,
4042
) {
4143
if (strlen((string)getenv('GITHUB_ACTIONS')) > 0 && strlen((string)getenv('TYPO3AZUREEDGEURIVERSION')) > 0 && !isset($_ENV['CI_PHPUNIT'])) {
4244
// CI gets special treatment, then we use a fixed URI for assets.
@@ -63,6 +65,7 @@ public function getFunctions(): array
6365
new TwigFunction('getPagerLinks', $this->getPagerLinks(...), ['is_safe' => ['html'], 'needs_context' => true]),
6466
new TwigFunction('getPrevNextLinks', $this->getPrevNextLinks(...), ['is_safe' => ['html'], 'needs_context' => true]),
6567
new TwigFunction('getSettings', $this->getSettings(...), ['is_safe' => ['html'], 'needs_context' => true]),
68+
new TwigFunction('getTYPO3Version', $this->getTYPO3Version(...), ['is_safe' => ['html'], 'needs_context' => false]),
6669
new TwigFunction('isNoSearch', $this->isNoSearch(...), ['is_safe' => ['html'], 'needs_context' => true]),
6770
new TwigFunction('copyDownload', $this->copyDownload(...), ['is_safe' => ['html'], 'needs_context' => true]),
6871
new TwigFunction('getStandardInventories', $this->getStandardInventories(...), ['is_safe' => ['html'], 'needs_context' => true]),
@@ -349,6 +352,11 @@ public function getSettings(array $context, string $key, string $default = ''):
349352
return $this->themeSettings->getSettings($key, $default);
350353
}
351354

355+
public function getTYPO3Version(): string
356+
{
357+
return $this->typo3VersionService->getPreferredVersion();
358+
}
359+
352360
/**
353361
* @param array{env: RenderContext} $context
354362
* @return list<PageLinkNode>

packages/typo3-docs-theme/tests/unit/Inventory/Typo3InventoryRepositoryTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use PHPUnit\Framework\TestCase;
1414
use Psr\Log\NullLogger;
1515
use T3Docs\Typo3DocsTheme\Inventory\Typo3InventoryRepository;
16+
use T3Docs\Typo3DocsTheme\Inventory\Typo3VersionService;
1617
use T3Docs\Typo3DocsTheme\Settings\Typo3DocsThemeSettings;
1718

1819
final class Typo3InventoryRepositoryTest extends TestCase
@@ -226,7 +227,7 @@ private function getInventoryRepository(Typo3DocsThemeSettings $settings, array
226227
$this->anchorNormalizer,
227228
new DefaultInventoryLoader(new NullLogger(), $this->jsonLoaderMock, $this->anchorNormalizer),
228229
$this->jsonLoaderMock,
229-
$settings,
230+
new Typo3VersionService($settings),
230231
$inventoryConfigs,
231232
);
232233
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<!-- content start -->
2+
<section class="section" id="document-title">
3+
<h1>Document Title<a class="headerlink" href="#document-title" data-bs-toggle="modal" data-bs-target="#linkReferenceModal" title="Reference this headline"></a></h1>
4+
5+
<p>Here is an extract of <a class="reference external" href="https://github.com/typo3/typo3/blob/main/typo3/sysext/backend/Configuration/Backend/Routes.php" title="EXT:backend/Configuration/Backend/Routes.php on GitHub">EXT:backend/Configuration/Backend/Routes.php (GitHub)</a>.</p>
6+
7+
</section>
8+
<!-- content end -->
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<guides
3+
xmlns="https://www.phpdoc.org/guides"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:schemaLocation="https://www.phpdoc.org/guides vendor/phpdocumentor/guides-cli/resources/schema/guides.xsd"
6+
>
7+
<project title="Render guides"/>
8+
<extension
9+
class="\T3Docs\Typo3DocsTheme\DependencyInjection\Typo3DocsThemeExtension"
10+
edit-on-github="TYPO3-Documentation/render-guides"
11+
edit-on-github-branch="main"
12+
typo3-core-preferred="main"
13+
/>
14+
<inventory id="h2document" url="https://docs.typo3.org/m/typo3/docs-how-to-document/main/en-us/" />
15+
</guides>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
==============
2+
Document Title
3+
==============
4+
5+
6+
Here is an extract of :t3src:`backend/Configuration/Backend/Routes.php`.

tests/Integration/tests/roles/role-t3src/input/guides.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
<project title="Render guides"/>
88
<extension
99
class="\T3Docs\Typo3DocsTheme\DependencyInjection\Typo3DocsThemeExtension"
10-
typo3-version="12.4"
1110
edit-on-github="TYPO3-Documentation/render-guides"
1211
edit-on-github-branch="main"
1312
/>

0 commit comments

Comments
 (0)