|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\UX\Icons\Command; |
| 13 | + |
| 14 | +use Symfony\Component\Console\Attribute\AsCommand; |
| 15 | +use Symfony\Component\Console\Command\Command; |
| 16 | +use Symfony\Component\Console\Input\InputInterface; |
| 17 | +use Symfony\Component\Console\Input\InputOption; |
| 18 | +use Symfony\Component\Console\Output\OutputInterface; |
| 19 | +use Symfony\Component\Console\Style\SymfonyStyle; |
| 20 | +use Symfony\UX\Icons\Exception\IconNotFoundException; |
| 21 | +use Symfony\UX\Icons\Iconify; |
| 22 | +use Symfony\UX\Icons\Registry\LocalSvgIconRegistry; |
| 23 | +use Symfony\UX\Icons\Twig\IconFinder; |
| 24 | + |
| 25 | +/** |
| 26 | + * @author Kevin Bond <[email protected]> |
| 27 | + * |
| 28 | + * @internal |
| 29 | + */ |
| 30 | +#[AsCommand( |
| 31 | + name: 'ux:icons:lock', |
| 32 | + description: 'Scan project and import icon(s) from iconify.design', |
| 33 | +)] |
| 34 | +final class LockIconsCommand extends Command |
| 35 | +{ |
| 36 | + public function __construct( |
| 37 | + private Iconify $iconify, |
| 38 | + private LocalSvgIconRegistry $registry, |
| 39 | + private IconFinder $iconFinder, |
| 40 | + ) { |
| 41 | + parent::__construct(); |
| 42 | + } |
| 43 | + |
| 44 | + protected function configure(): void |
| 45 | + { |
| 46 | + $this |
| 47 | + ->addOption( |
| 48 | + name: 'force', |
| 49 | + mode: InputOption::VALUE_NONE, |
| 50 | + description: 'Force re-import of all found icons' |
| 51 | + ) |
| 52 | + ; |
| 53 | + } |
| 54 | + |
| 55 | + protected function execute(InputInterface $input, OutputInterface $output): int |
| 56 | + { |
| 57 | + $io = new SymfonyStyle($input, $output); |
| 58 | + $force = $input->getOption('force'); |
| 59 | + $count = 0; |
| 60 | + |
| 61 | + $io->comment('Scanning project for icons...'); |
| 62 | + |
| 63 | + foreach ($this->iconFinder->icons() as $icon) { |
| 64 | + if (2 !== \count($parts = explode(':', $icon))) { |
| 65 | + continue; |
| 66 | + } |
| 67 | + |
| 68 | + if (!$force && $this->registry->has($icon)) { |
| 69 | + // icon already imported |
| 70 | + continue; |
| 71 | + } |
| 72 | + |
| 73 | + [$prefix, $name] = $parts; |
| 74 | + |
| 75 | + try { |
| 76 | + $svg = $this->iconify->fetchSvg($prefix, $name); |
| 77 | + } catch (IconNotFoundException) { |
| 78 | + // icon not found on iconify |
| 79 | + continue; |
| 80 | + } |
| 81 | + |
| 82 | + $this->registry->add(sprintf('%s/%s', $prefix, $name), $svg); |
| 83 | + |
| 84 | + $license = $this->iconify->metadataFor($prefix)['license']; |
| 85 | + ++$count; |
| 86 | + |
| 87 | + $io->text(sprintf( |
| 88 | + " <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>", |
| 89 | + $prefix, |
| 90 | + $name, |
| 91 | + $license['url'], |
| 92 | + $license['title'], |
| 93 | + $icon, |
| 94 | + )); |
| 95 | + } |
| 96 | + |
| 97 | + $io->success(sprintf('Imported %d icons.', $count)); |
| 98 | + |
| 99 | + return Command::SUCCESS; |
| 100 | + } |
| 101 | +} |
0 commit comments