Thanks to visit codestin.com
Credit goes to github.com

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
8 changes: 1 addition & 7 deletions src/Command/GenerateLlmsFilesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,13 +216,7 @@ private function generateToolkit(SymfonyStyle $io): array
$toolkitKits[] = ['kitId' => $kitId, 'kit' => $kit];

foreach ($kit->getRecipes(RecipeType::Component) as $recipe) {
$apiRef = $this->toolkitService->extractRecipeApiReference($recipe);
$md = $this->twig->render('llms/toolkit_component.md.twig', [
'kitId' => $kitId,
'kit' => $kit,
'recipe' => $recipe,
'apiRef' => $apiRef,
]);
$md = $this->toolkitService->renderRecipeMarkdown($kitId, $recipe, isLlm: true);
$path = $this->generateMdPath('app_toolkit_component', [
'kitId' => $kitId->value,
'componentName' => $recipe->name,
Expand Down
33 changes: 33 additions & 0 deletions src/Service/Toolkit/ToolkitService.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Symfony\UX\Toolkit\Kit\Kit;
use Symfony\UX\Toolkit\Recipe\Recipe;
use Symfony\UX\Toolkit\Registry\RegistryFactory;
use Twig\Environment;

use function Symfony\Component\String\s;

Expand All @@ -37,6 +38,7 @@ class ToolkitService
public function __construct(
#[Autowire(service: 'ux_toolkit.registry.registry_factory')]
private RegistryFactory $registryFactory,
private Environment $twig,
) {
}

Expand Down Expand Up @@ -67,6 +69,37 @@ public function resolveRecipePool(Kit $kit, Recipe $component): Pool
return (new PoolResolver())->resolveForRecipe($kit, $component);
}

public function renderRecipeMarkdown(ToolkitKitId $kitId, Recipe $recipe, bool $isLlm = false): string
{
$kit = $this->getKit($kitId);
$pool = $this->resolveRecipePool($kit, $recipe);
$apiReference = $this->extractRecipeApiReference($recipe);

$files = [];
foreach ($pool->getFiles() as $recipeFullPath => $recipeFiles) {
foreach ($recipeFiles as $recipeFile) {
$recipeFileSourcePath = Path::join($recipeFullPath, $recipeFile->sourceRelativePathName);
$files[] = [
'path_name' => $recipeFile->sourceRelativePathName,
'content' => file_get_contents($recipeFileSourcePath),
'language' => pathinfo($recipeFileSourcePath, \PATHINFO_EXTENSION),
];
}
}

return $this->twig->render(\sprintf('toolkit/docs/%s/%s.md.twig', $kitId->value, $recipe->name), [
'kit_id' => $kitId,
'kit' => $kit,
'component' => $recipe,
'files' => $files,
'php_package_dependencies' => $pool->getPhpPackageDependencies(),
'npm_package_dependencies' => $pool->getNpmPackageDependencies(),
'importmap_package_dependencies' => $pool->getImportmapPackageDependencies(),
'api_reference' => $apiReference,
'is_llm' => $isLlm,
]);
}

/**
* @return array<string, array{props: list<array{name: string, type: string, description: string}>, blocks: list<array{name: string, description: string}>}>
*/
Expand Down
30 changes: 1 addition & 29 deletions src/Twig/Components/Toolkit/ComponentDoc.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

use App\Enum\ToolkitKitId;
use App\Service\Toolkit\ToolkitService;
use Symfony\Component\Filesystem\Path;
use Symfony\UX\Toolkit\Recipe\Recipe;
use Symfony\UX\TwigComponent\Attribute\AsTwigComponent;

Expand All @@ -25,38 +24,11 @@ final class ComponentDoc

public function __construct(
private readonly ToolkitService $toolkitService,
private readonly \Twig\Environment $twig,
) {
}

public function getMarkdownContent(): string
{
$kit = $this->toolkitService->getKit($this->kitId);
$pool = $this->toolkitService->resolveRecipePool($kit, $this->component);
$apiReference = $this->toolkitService->extractRecipeApiReference($this->component);

$files = [];
foreach ($pool->getFiles() as $recipeFullPath => $recipeFiles) {
foreach ($recipeFiles as $recipeFile) {
$recipeFileSourcePath = Path::join($recipeFullPath, $recipeFile->sourceRelativePathName);
$files[] = [
'path_name' => $recipeFile->sourceRelativePathName,
'content' => file_get_contents($recipeFileSourcePath),
'language' => pathinfo($recipeFileSourcePath, \PATHINFO_EXTENSION),
];
}
}

$templateName = \sprintf('toolkit/docs/%s/%s.md.twig', $this->kitId->value, $this->component->name);

return $this->twig->render($templateName, [
'kit_id' => $this->kitId,
'component' => $this->component,
'files' => $files,
'php_package_dependencies' => $pool->getPhpPackageDependencies(),
'npm_package_dependencies' => $pool->getNpmPackageDependencies(),
'importmap_package_dependencies' => $pool->getImportmapPackageDependencies(),
'api_reference' => $apiReference,
]);
return $this->toolkitService->renderRecipeMarkdown($this->kitId, $this->component);
}
}
6 changes: 3 additions & 3 deletions src/Twig/Extension/ToolkitExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ final class ToolkitExtension extends AbstractExtension
{
public function getFunctions(): iterable
{
yield new TwigFunction('toolkit_code_example', [ToolkitRuntime::class, 'codeExample'], ['is_safe' => ['html']]);
yield new TwigFunction('toolkit_code_demo', [ToolkitRuntime::class, 'codeDemo'], ['is_safe' => ['html']]);
yield new TwigFunction('toolkit_code_usage', [ToolkitRuntime::class, 'codeUsage'], ['is_safe' => ['html']]);
yield new TwigFunction('toolkit_code_example', [ToolkitRuntime::class, 'codeExample'], ['is_safe' => ['html'], 'needs_context' => true]);
yield new TwigFunction('toolkit_code_demo', [ToolkitRuntime::class, 'codeDemo'], ['is_safe' => ['html'], 'needs_context' => true]);
yield new TwigFunction('toolkit_code_usage', [ToolkitRuntime::class, 'codeUsage'], ['is_safe' => ['html'], 'needs_context' => true]);
yield new TwigFunction('toolkit_color', [ToolkitRuntime::class, 'kitColor']);
}
}
26 changes: 21 additions & 5 deletions src/Twig/Extension/ToolkitRuntime.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ public function __construct(
}

/**
* @param array<string, mixed> $context
* @param array<string, mixed> $options
*/
public function codeExample(string $kitId, string $recipeName, string $exampleName, array $options = [], bool $preview = true): string
public function codeExample(array $context, string $kitId, string $recipeName, string $exampleName, array $options = [], bool $preview = true): string
{
$kitId = ToolkitKitId::from($kitId);
$kit = $this->toolkitService->getKit($kitId);
Expand All @@ -38,6 +39,19 @@ public function codeExample(string $kitId, string $recipeName, string $exampleNa

$exampleCode = trim(file_get_contents($exampleFile));
$language = 'twig';

if ($context['is_llm'] ?? false) {
return \sprintf(
<<<'MARKDOWN'
```%2$s
%1$s
```
MARKDOWN,
$exampleCode,
$language,
);
}

$options = json_encode($options + ['kit' => $kitId->value]);

if ($preview) {
Expand Down Expand Up @@ -78,19 +92,21 @@ public function codeExample(string $kitId, string $recipeName, string $exampleNa
}

/**
* @param array<string, mixed> $context
* @param array<string, mixed> $options
*/
public function codeDemo(string $kitId, string $recipeName, array $options = []): string
public function codeDemo(array $context, string $kitId, string $recipeName, array $options = []): string
{
return $this->codeExample($kitId, $recipeName, 'Demo', $options + ['height' => '450px'], preview: true);
return $this->codeExample($context, $kitId, $recipeName, 'Demo', $options + ['height' => '450px'], preview: true);
}

/**
* @param array<string, mixed> $context
* @param array<string, mixed> $options
*/
public function codeUsage(string $kitId, string $recipeName, array $options = []): string
public function codeUsage(array $context, string $kitId, string $recipeName, array $options = []): string
{
return $this->codeExample($kitId, $recipeName, 'Usage', $options, preview: false);
return $this->codeExample($context, $kitId, $recipeName, 'Usage', $options, preview: false);
}

public function kitColor(string $kitId): string
Expand Down
37 changes: 0 additions & 37 deletions templates/llms/toolkit_component.md.twig

This file was deleted.

13 changes: 12 additions & 1 deletion templates/toolkit/docs/_base_component.md.twig
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
{# twig-cs-fixer-disable FileName.Error #}
# {{ component.manifest.name }}
# {{ component.manifest.name }}{% if is_llm ?? false %} ({{ kit.manifest.name }} kit){% endif %}

{{ component.manifest.description }}

{{ block('demo')|trim|raw }}

## Installation

{% if is_llm ?? false %}
{% if component.manifest.versionAdded is not null %}
> [!NOTE]
> Available since UX Toolkit {{ component.manifest.versionAdded }}.
{% endif %}

```shell
bin/console ux:install {{ component.name }} --kit {{ kit_id.value }}
```
{% else %}
::: tabs

:: tab Automatic
Expand Down Expand Up @@ -63,6 +73,7 @@ Copy the following file(s) into your Symfony app:
Happy coding!

:::
{% endif %}

{% if block('usage') is defined %}
## Usage
Expand Down
Loading