Skip to content

Commit 1fa3d6a

Browse files
committed
feature #198 Improve the class directive code and add Symfony UX support (javiereguiluz)
This PR was merged into the main branch. Discussion ---------- Improve the class directive code and add Symfony UX support This addresses `@stof`'s comments about unoptimized code: #196 (comment) and adds support for Symfony UX classes too. Commits ------- bec9b30 Improve the class directive code and add Symfony UX support
2 parents 40ef1a2 + bec9b30 commit 1fa3d6a

File tree

3 files changed

+24
-6
lines changed

3 files changed

+24
-6
lines changed

src/Reference/ClassReference.php

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,33 @@ public function resolve(Environment $environment, string $data): ResolvedReferen
3232
{
3333
$className = u($data)->replace('\\\\', '\\');
3434

35+
/**
36+
* Symfony AI classes require some special handling because of its monorepo structure. Example:
37+
*
38+
* input: Symfony\AI\Agent\Memory\StaticMemoryProvider
39+
* output: https://github.com/symfony/ai/blob/main/src/agent/src/Memory/StaticMemoryProvider.php
40+
*/
3541
if (str_starts_with($className, 'Symfony\\AI\\')) {
36-
// Example:
37-
// input: Symfony\AI\Agent\Memory\StaticMemoryProvider
38-
// output: https://github.com/symfony/ai/blob/main/src/agent/src/Memory/StaticMemoryProvider.php
39-
4042
$classPath = $className->after('Symfony\\AI\\');
4143
[$monorepoSubRepository, $classRelativePath] = $classPath->split('\\', 2);
4244
// because of monorepo structure, the first part of the classpath needs to be slugged
4345
// 'Agent' -> 'agent', 'AiBundle' -> 'ai-bundle', etc.
44-
$monorepoSubRepository = u($monorepoSubRepository)->snake('-')->lower();
45-
$classRelativePath = u($classRelativePath)->replace('\\', '/');
46+
$monorepoSubRepository = $monorepoSubRepository->snake('-')->lower();
47+
$classRelativePath = $classRelativePath->replace('\\', '/');
4648

4749
$url = \sprintf('https://github.com/symfony/ai/blob/main/src/%s/src/%s.php', $monorepoSubRepository, $classRelativePath);
50+
/**
51+
* Symfony UX classes require some special handling because of its monorepo structure. Example:
52+
*
53+
* input: Symfony\UX\Chartjs\Twig\ChartExtension
54+
* output: https://github.com/symfony/ux/blob/2.x/src/Chartjs/src/Twig/ChartExtension.php
55+
*/
56+
} elseif (str_starts_with($className, 'Symfony\\UX\\')) {
57+
$classPath = $className->after('Symfony\\UX\\');
58+
[$monorepoSubRepository, $classRelativePath] = $classPath->split('\\', 2);
59+
$classRelativePath = $classRelativePath->replace('\\', '/');
60+
61+
$url = \sprintf('https://github.com/symfony/ux/blob/2.x/src/%s/src/%s.php', $monorepoSubRepository, $classRelativePath);
4862
} else {
4963
$url = sprintf('%s/%s.php', $this->symfonyRepositoryUrl, $className->replace('\\', '/'));
5064
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
<p><a href="https://github.com/symfony/symfony/blob/4.0/src/Symfony/Component/HttpKernel/DependencyInjection/ContainerAwareHttpKernel.php" class="reference external" title="Symfony\Component\HttpKernel\DependencyInjection\ContainerAwareHttpKernel" rel="external noopener noreferrer" target="_blank">ContainerAwareHttpKernel</a></p>
22

33
<p><a href="https://github.com/symfony/ai/blob/main/src/agent/src/Memory/StaticMemoryProvider.php" class="reference external" title="Symfony\AI\Agent\Memory\StaticMemoryProvider" rel="external noopener noreferrer" target="_blank">StaticMemoryProvider</a></p>
4+
5+
<p><a href="https://github.com/symfony/ux/blob/2.x/src/Chartjs/src/Twig/ChartExtension.php" class="reference external" title="Symfony\UX\Chartjs\Twig\ChartExtension" rel="external noopener noreferrer" target="_blank">ChartExtension</a></p>

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@
22
:class:`Symfony\\Component\\HttpKernel\\DependencyInjection\\ContainerAwareHttpKernel`
33

44
:class:`Symfony\\AI\\Agent\\Memory\\StaticMemoryProvider`
5+
6+
:class:`Symfony\\UX\\Chartjs\\Twig\\ChartExtension`

0 commit comments

Comments
 (0)