Skip to content

Commit db59f2d

Browse files
committed
[Icons] Fetch icons in batch
First usage of symfony#2351 with the import command Next steps: * lock * warm-up :)
1 parent 8215922 commit db59f2d

File tree

1 file changed

+47
-25
lines changed

1 file changed

+47
-25
lines changed

src/Icons/src/Command/ImportIconCommand.php

Lines changed: 47 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,7 @@ public function __construct(private Iconify $iconify, private LocalSvgIconRegist
4141
protected function configure(): void
4242
{
4343
$this
44-
->addArgument(
45-
'names',
46-
InputArgument::IS_ARRAY | InputArgument::REQUIRED,
47-
'Icon name from ux.symfony.com/icons (e.g. "mdi:home")',
48-
)
44+
->addArgument('names', InputArgument::IS_ARRAY | InputArgument::REQUIRED, 'Icon name from ux.symfony.com/icons (e.g. "mdi:home")')
4945
;
5046
}
5147

@@ -55,6 +51,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
5551
$names = $input->getArgument('names');
5652
$result = Command::SUCCESS;
5753

54+
$prefixIcons = [];
55+
$importedIcons = 0;
5856
foreach ($names as $name) {
5957
if (!preg_match('#^([\w-]+):([\w-]+)$#', $name, $matches)) {
6058
$io->error(\sprintf('Invalid icon name "%s".', $name));
@@ -65,33 +63,57 @@ protected function execute(InputInterface $input, OutputInterface $output): int
6563

6664
[$fullName, $prefix, $name] = $matches;
6765

68-
$io->comment(\sprintf('Importing %s...', $fullName));
66+
$prefixIcons[$prefix] ??= [];
67+
$prefixIcons[$prefix][$name] = $fullName;
68+
}
69+
70+
foreach ($prefixIcons as $prefix => $icons) {
6971

70-
try {
71-
$iconSvg = $this->iconify->fetchIcon($prefix, $name)->toHtml();
72-
} catch (IconNotFoundException $e) {
73-
$io->error($e->getMessage());
72+
if (!$this->iconify->hasIconSet($prefix)) {
73+
$io->error(\sprintf('Icon set "%s" not found.', $prefix));
7474
$result = Command::FAILURE;
7575

7676
continue;
7777
}
7878

79-
$cursor = new Cursor($output);
80-
$cursor->moveUp(2);
81-
82-
$this->registry->add(\sprintf('%s/%s', $prefix, $name), $iconSvg);
83-
84-
$license = $this->iconify->metadataFor($prefix)['license'];
79+
$metadata = $this->iconify->metadataFor($prefix);
80+
$io->writeln([
81+
"\n",
82+
\sprintf(' IconSet: <fg=bright-white;bg=black>%s</> - %s', $metadata['name'], $prefix),
83+
\sprintf(' (Licence: %s)', $metadata['license']['title']),
84+
"\n",
85+
]);
86+
87+
foreach (array_chunk($icons, 25, true) as $iconBatch) {
88+
foreach ($iconBatch as $name => $fullName) {
89+
$io->writeln(\sprintf(' Importing %s:%s ...', $prefix, $name));
90+
}
91+
92+
try {
93+
$batchResults = $this->iconify->fetchIcons($prefix, $names);
94+
} catch (IconNotFoundException $e) {
95+
$io->error($e->getMessage());
96+
$result = Command::FAILURE;
97+
98+
continue;
99+
}
100+
101+
$cursor = new Cursor($output);
102+
$cursor->moveUp(count($iconBatch));
103+
104+
foreach ($batchResults as $name => $icon) {
105+
$this->registry->add(\sprintf('%s/%s', $prefix, $name), (string) $icon);
106+
++$importedIcons;
107+
$cursor->clearLine();
108+
$io->writeln(\sprintf(" <fg=bright-green;options=bold>✓</> Imported <fg=bright-white;bg=black>%s:</><fg=bright-magenta;bg=black;options>%s</>", $prefix, $name));
109+
}
110+
111+
$cursor->clearLineAfter();
112+
}
113+
}
85114

86-
$io->text(\sprintf(
87-
" <fg=bright-green;options=bold>✓</> Imported <fg=bright-white;bg=black>%s:</><fg=bright-magenta;bg=black;options>%s</> (License: <href=%s>%s</>). Render with: <comment>{{ ux_icon('%s') }}</comment>",
88-
$prefix,
89-
$name,
90-
$license['url'] ?? '#',
91-
$license['title'],
92-
$fullName,
93-
));
94-
$io->newLine();
115+
if (Command::SUCCESS === $result) {
116+
$io->success(sprintf('Imported %d icons.', $importedIcons));
95117
}
96118

97119
return $result;

0 commit comments

Comments
 (0)