Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 20 additions & 6 deletions src/Reference/ClassReference.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,33 @@ public function resolve(Environment $environment, string $data): ResolvedReferen
{
$className = u($data)->replace('\\\\', '\\');

/**
* Symfony AI classes require some special handling because of its monorepo structure. Example:
*
* input: Symfony\AI\Agent\Memory\StaticMemoryProvider
* output: https://github.com/symfony/ai/blob/main/src/agent/src/Memory/StaticMemoryProvider.php
*/
if (str_starts_with($className, 'Symfony\\AI\\')) {
// Example:
// input: Symfony\AI\Agent\Memory\StaticMemoryProvider
// output: https://github.com/symfony/ai/blob/main/src/agent/src/Memory/StaticMemoryProvider.php

$classPath = $className->after('Symfony\\AI\\');
[$monorepoSubRepository, $classRelativePath] = $classPath->split('\\', 2);
// because of monorepo structure, the first part of the classpath needs to be slugged
// 'Agent' -> 'agent', 'AiBundle' -> 'ai-bundle', etc.
$monorepoSubRepository = u($monorepoSubRepository)->snake('-')->lower();
$classRelativePath = u($classRelativePath)->replace('\\', '/');
$monorepoSubRepository = $monorepoSubRepository->snake('-')->lower();
$classRelativePath = $classRelativePath->replace('\\', '/');

$url = \sprintf('https://github.com/symfony/ai/blob/main/src/%s/src/%s.php', $monorepoSubRepository, $classRelativePath);
/**
* Symfony UX classes require some special handling because of its monorepo structure. Example:
*
* input: Symfony\UX\Chartjs\Twig\ChartExtension
* output: https://github.com/symfony/ux/blob/2.x/src/Chartjs/src/Twig/ChartExtension.php
*/
} elseif (str_starts_with($className, 'Symfony\\UX\\')) {
$classPath = $className->after('Symfony\\UX\\');
[$monorepoSubRepository, $classRelativePath] = $classPath->split('\\', 2);
$classRelativePath = $classRelativePath->replace('\\', '/');

$url = \sprintf('https://github.com/symfony/ux/blob/2.x/src/%s/src/%s.php', $monorepoSubRepository, $classRelativePath);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we hardcode 2.x or should we have a configuration setting to allow linking to the appropriate version based on the version of the UX documentation being rendered ? Or is this meant to be used to reference UX classes from symfony-docs (and so not related to a UX version) ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should allow to configure that version, yes. I opened #199 to handle this. Thanks!

} else {
$url = sprintf('%s/%s.php', $this->symfonyRepositoryUrl, $className->replace('\\', '/'));
}
Expand Down
2 changes: 2 additions & 0 deletions tests/fixtures/expected/blocks/references/class.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
<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>

<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>

<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>
2 changes: 2 additions & 0 deletions tests/fixtures/source/blocks/references/class.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
:class:`Symfony\\Component\\HttpKernel\\DependencyInjection\\ContainerAwareHttpKernel`

:class:`Symfony\\AI\\Agent\\Memory\\StaticMemoryProvider`

:class:`Symfony\\UX\\Chartjs\\Twig\\ChartExtension`