|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Symfony\UX\Icons\Command; |
| 4 | + |
| 5 | +use Symfony\Component\Console\Attribute\AsCommand; |
| 6 | +use Symfony\Component\Console\Command\Command; |
| 7 | +use Symfony\Component\Console\Input\InputArgument; |
| 8 | +use Symfony\Component\Console\Input\InputInterface; |
| 9 | +use Symfony\Component\Console\Output\OutputInterface; |
| 10 | +use Symfony\Component\Console\Style\SymfonyStyle; |
| 11 | +use Symfony\UX\Icons\Exception\IconNotFoundException; |
| 12 | +use Symfony\UX\Icons\Iconify; |
| 13 | +use Symfony\UX\Icons\Registry\LocalSvgIconRegistry; |
| 14 | + |
| 15 | +/** |
| 16 | + * @author Kevin Bond <kevinbond@gmail.com> |
| 17 | + * |
| 18 | + * @internal |
| 19 | + */ |
| 20 | +#[AsCommand( |
| 21 | + name: 'ux:icons:import', |
| 22 | + description: 'Import icon(s) from iconify.design', |
| 23 | +)] |
| 24 | +final class ImportIconCommand extends Command |
| 25 | +{ |
| 26 | + public function __construct(private Iconify $iconify, private LocalSvgIconRegistry $registry) |
| 27 | + { |
| 28 | + parent::__construct(); |
| 29 | + } |
| 30 | + |
| 31 | + protected function configure(): void |
| 32 | + { |
| 33 | + $this |
| 34 | + ->addArgument( |
| 35 | + 'names', |
| 36 | + InputArgument::IS_ARRAY | InputArgument::REQUIRED, |
| 37 | + 'Icon name from iconify.design (suffix with "@<name>" to rename locally)', |
| 38 | + ) |
| 39 | + ; |
| 40 | + } |
| 41 | + |
| 42 | + protected function execute(InputInterface $input, OutputInterface $output): int |
| 43 | + { |
| 44 | + $io = new SymfonyStyle($input, $output); |
| 45 | + $names = $input->getArgument('names'); |
| 46 | + $result = Command::SUCCESS; |
| 47 | + |
| 48 | + foreach ($names as $name) { |
| 49 | + if (!preg_match('#^(([\w-]+):([\w-]+))(@([\w-]+))?$#', $name, $matches)) { |
| 50 | + $io->error(sprintf('Invalid icon name "%s".', $name)); |
| 51 | + $result = Command::FAILURE; |
| 52 | + |
| 53 | + continue; |
| 54 | + } |
| 55 | + |
| 56 | + [,,$prefix, $name] = $matches; |
| 57 | + $localName = $matches[5] ?? $name; |
| 58 | + |
| 59 | + $io->comment(sprintf('Importing <info>%s:%s</info> as <info>%s</info>...', $prefix, $name, $localName)); |
| 60 | + |
| 61 | + try { |
| 62 | + $svg = $this->iconify->fetchSvg($prefix, $name); |
| 63 | + } catch (IconNotFoundException $e) { |
| 64 | + $io->error($e->getMessage()); |
| 65 | + $result = Command::FAILURE; |
| 66 | + |
| 67 | + continue; |
| 68 | + } |
| 69 | + |
| 70 | + $this->registry->add($localName, $svg); |
| 71 | + |
| 72 | + $io->text(sprintf("<info>Imported Icon</info>, render with <comment>{{ ux_icon('%s') }}</comment>.", $localName)); |
| 73 | + $io->newLine(); |
| 74 | + } |
| 75 | + |
| 76 | + return $result; |
| 77 | + } |
| 78 | +} |
0 commit comments