Skip to content

Commit

Permalink
Way to get the list of available components in current theme
Browse files Browse the repository at this point in the history
  • Loading branch information
Halleck45 committed Feb 2, 2025
1 parent 6b3b126 commit 2c83380
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 20 deletions.
68 changes: 68 additions & 0 deletions src/Toolkit/src/Command/DebugUxToolkitCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\UX\Toolkit\Command;

use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\UX\Toolkit\ComponentRepository\CurrentTheme;
use Symfony\UX\Toolkit\Registry\RegistryFactory;

/**
* @author Jean-François Lépine
*
* @internal
*/
#[AsCommand(
name: 'debug:ux:toolkit',
description: 'This command list all components available in the current theme.'
)]
class DebugUxToolkitCommand extends Command
{
public function __construct(
private readonly CurrentTheme $currentTheme,
private readonly RegistryFactory $registryFactory,
) {
parent::__construct();
}

protected function configure(): void
{
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);

$repository = $this->currentTheme->getIdentity();
$finder = $this->currentTheme->getRepository()->fetch($repository);
$registry = $this->registryFactory->create($finder);

$io->title('Current theme:');
$io->note('Update your config/packages/ux_toolkit.yaml to change the current theme.');
$io->table(['Vendor', 'Package'], [[$repository->getVendor(), $repository->getPackage()]]);

$io->title('Available components:');
$table = [];
foreach ($registry->all() as $component) {
$table[] = [$component->name];
}

$io->table(['Component'], $table);

$io->note('Run "symfony console ux:toolkit:install <component>" to install a component.');

return Command::SUCCESS;
}
}
1 change: 0 additions & 1 deletion src/Toolkit/src/Command/UxToolkitInstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);

// Download sources, or get them from vendors
$repository = $this->currentTheme->getIdentity();

$io->info(
Expand Down
14 changes: 8 additions & 6 deletions src/Toolkit/src/ComponentRepository/GithubRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,14 @@ public function fetch(RepositoryIdentity $component): Finder
);

$destination = $this->getCacheDir();
$zipFile = $destination.'/'.basename($zipUrl);
$finder = new Finder();
$finder->files()->in($destination);

if ($this->filesystem->exists($destination.'/registry.json')) {
return $finder;
}

$zipFile = $destination.'/'.basename($zipUrl);
$response = $this->httpClient->request('GET', $zipUrl, [
'sink' => $zipFile,
]);
Expand All @@ -58,7 +64,7 @@ public function fetch(RepositoryIdentity $component): Finder
throw new \RuntimeException(\sprintf('Failed to download the file from "%s".', $zipUrl));
}

// Ensure response contains valid github headers
// Ensure response contains valid headers
$headers = $response->getHeaders();
if (!isset($headers['content-type']) || !\in_array('application/zip', $headers['content-type'])) {
throw new \RuntimeException(\sprintf('The file from "%s" is not a valid zip file.', $zipUrl));
Expand All @@ -73,10 +79,6 @@ public function fetch(RepositoryIdentity $component): Finder
$zip->extractTo($destination);
$zip->close();

$rootDir = $destination;
$finder = new Finder();
$finder->files()->in($rootDir);

return $finder;
}

Expand Down
13 changes: 12 additions & 1 deletion src/Toolkit/src/UxToolkitBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
use Symfony\Component\HttpKernel\Bundle\AbstractBundle;
use Symfony\UX\Toolkit\Command\DebugUxToolkitCommand;
use Symfony\UX\Toolkit\Command\UxToolkitInstallCommand;
use Symfony\UX\Toolkit\Compiler\TwigComponentCompiler;
use Symfony\UX\Toolkit\ComponentRepository\CurrentTheme;
Expand Down Expand Up @@ -51,17 +52,27 @@ public function build(ContainerBuilder $container): void
'$prefix' => '%ux_toolkit.prefix%',
]);

// Prepare command
// Prepare commands
$container->autowire(UxToolkitInstallCommand::class);
$container
->registerForAutoconfiguration(UxToolkitInstallCommand::class)
->addTag('console.command');

$container->autowire(DebugUxToolkitCommand::class);
$container
->registerForAutoconfiguration(DebugUxToolkitCommand::class)
->addTag('console.command');

$container
->getDefinition(UxToolkitInstallCommand::class)
->setPublic(true)
->addTag('console.command');

$container
->getDefinition(DebugUxToolkitCommand::class)
->setPublic(true)
->addTag('console.command');

// Inject http client (if exists) to github repository
if ($container->has('http_client')) {
$container->getDefinition(GithubRepository::class)
Expand Down
40 changes: 40 additions & 0 deletions src/Toolkit/tests/Command/UxToolkitDebugCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\UX\Toolkit\Tests\Command;

use PHPUnit\Framework\TestCase;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Zenstruck\Console\Test\InteractsWithConsole;

/**
* @author Jean-François Lépine
*/
class UxToolkitDebugCommandTest extends KernelTestCase
{
use InteractsWithConsole;

public function testShouldBeAbleToLiseComponents(): void
{
$destination = sys_get_temp_dir().\DIRECTORY_SEPARATOR.uniqid();
mkdir($destination);

$this->bootKernel();
$this->consoleCommand('debug:ux:toolkit')
->execute()
->assertSuccessful()
->assertOutputContains('Current theme:')
->assertOutputContains('Available components:')
->assertOutputContains('Badge')
->assertOutputContains('Button')
;
}
}
12 changes: 0 additions & 12 deletions src/Toolkit/tests/Command/UxToolkitInstallCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,6 @@

use PHPUnit\Framework\TestCase;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\BufferedOutput;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\UX\Toolkit\Command\UxToolkitInstallCommand;
use Symfony\UX\Toolkit\Compiler\TwigComponentCompiler;
use Symfony\UX\Toolkit\ComponentRepository\CurrentTheme;
use Symfony\UX\Toolkit\ComponentRepository\RepositoryIdentifier;
use Symfony\UX\Toolkit\ComponentRepository\GithubRepository;
use Symfony\UX\Toolkit\ComponentRepository\OfficialRepository;
use Symfony\UX\Toolkit\ComponentRepository\RepositoryFactory;
use Symfony\UX\Toolkit\Registry\RegistryFactory;
use Zenstruck\Console\Test\InteractsWithConsole;

/**
Expand Down

0 comments on commit 2c83380

Please sign in to comment.