Skip to content

Commit

Permalink
[Icons] Support aliases in LockIconsCommand
Browse files Browse the repository at this point in the history
  • Loading branch information
smnandre committed Nov 10, 2024
1 parent 5fc5604 commit 9ac91b8
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 7 deletions.
16 changes: 12 additions & 4 deletions src/Icons/src/Command/LockIconsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public function __construct(
private Iconify $iconify,
private LocalSvgIconRegistry $registry,
private IconFinder $iconFinder,
private readonly array $iconAliases = [],
private readonly array $iconSetAliases = [],
) {
parent::__construct();
}
Expand All @@ -59,19 +61,25 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$count = 0;

$io->comment('Scanning project for icons...');
$finderIcons = $this->iconFinder->icons();

foreach ($this->iconFinder->icons() as $icon) {
if ($this->iconAliases) {
$io->comment('Adding icons aliases...');
}

foreach ([...array_values($this->iconAliases), ...array_values($finderIcons)] as $icon) {
if (2 !== \count($parts = explode(':', $icon))) {
continue;
}

if (!$force && $this->registry->has($icon)) {
[$prefix, $name] = $parts;
$prefix = $this->iconSetAliases[$prefix] ?? $prefix;

if (!$force && $this->registry->has($prefix.':'.$name)) {
// icon already imported
continue;
}

[$prefix, $name] = $parts;

if (!$this->iconify->hasIconSet($prefix)) {
// not an icon set? example: "og:twitter"
if ($io->isVeryVerbose()) {
Expand Down
5 changes: 5 additions & 0 deletions src/Icons/src/DependencyInjection/UXIconsExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,11 @@ protected function loadInternal(array $mergedConfig, ContainerBuilder $container
}
}

$container->getDefinition('.ux_icons.command.lock')
->setArgument(3, $mergedConfig['aliases'])
->setArgument(4, $iconSetAliases)
;

if (!$container->getParameter('kernel.debug')) {
$container->removeDefinition('.ux_icons.command.import');
}
Expand Down
4 changes: 4 additions & 0 deletions src/Icons/tests/Fixtures/TestKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ protected function configureContainer(ContainerConfigurator $container): void

$container->extension('ux_icons', [
'icon_dir' => '%kernel.project_dir%/tests/Fixtures/icons',
'aliases' => [
'foo' => 'lucide:circle',
'bar' => 'lu:circle-off',
],
'icon_sets' => [
'fla' => [
'path' => '%kernel.project_dir%/tests/Fixtures/images/flags',
Expand Down
14 changes: 11 additions & 3 deletions src/Icons/tests/Integration/Command/LockIconsCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ final class LockIconsCommandTest extends KernelTestCase
private const ICONS = [
__DIR__.'/../../Fixtures/icons/iconamoon/3d-duotone.svg',
__DIR__.'/../../Fixtures/icons/flag/eu-4x3.svg',
__DIR__.'/../../Fixtures/icons/lucide/circle.svg',
__DIR__.'/../../Fixtures/icons/lucide/circle-off.svg',
];

/**
Expand All @@ -50,9 +52,11 @@ public function testImportFoundIcons(): void
$this->executeConsoleCommand('ux:icons:lock')
->assertSuccessful()
->assertOutputContains('Scanning project for icons...')
->assertOutputContains('Imported lucide:circle')
->assertOutputContains('Imported lucide:circle-off')
->assertOutputContains('Imported flag:eu-4x3')
->assertOutputContains('Imported iconamoon:3d-duotone')
->assertOutputContains('Imported 2 icons')
->assertOutputContains('Imported 4 icons')
;

foreach (self::ICONS as $icon) {
Expand All @@ -70,17 +74,21 @@ public function testForceImportFoundIcons(): void
$this->executeConsoleCommand('ux:icons:lock')
->assertSuccessful()
->assertOutputContains('Scanning project for icons...')
->assertOutputContains('Imported lucide:circle')
->assertOutputContains('Imported lucide:circle-off')
->assertOutputContains('Imported flag:eu-4x3')
->assertOutputContains('Imported iconamoon:3d-duotone')
->assertOutputContains('Imported 2 icons')
->assertOutputContains('Imported 4 icons')
;

$this->executeConsoleCommand('ux:icons:lock --force')
->assertSuccessful()
->assertOutputContains('Scanning project for icons...')
->assertOutputContains('Imported lucide:circle')
->assertOutputContains('Imported lucide:circle-off')
->assertOutputContains('Imported flag:eu-4x3')
->assertOutputContains('Imported iconamoon:3d-duotone')
->assertOutputContains('Imported 2 icons')
->assertOutputContains('Imported 4 icons')
;
}
}

0 comments on commit 9ac91b8

Please sign in to comment.