|
| 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\Helper\Table; |
| 17 | +use Symfony\Component\Console\Input\InputInterface; |
| 18 | +use Symfony\Component\Console\Input\InputOption; |
| 19 | +use Symfony\Component\Console\Output\OutputInterface; |
| 20 | +use Symfony\Component\Console\Style\SymfonyStyle; |
| 21 | +use Symfony\Component\Finder\Finder; |
| 22 | +use Symfony\UX\Icons\Registry\LocalSvgIconRegistry; |
| 23 | +use Symfony\UX\Icons\Twig\IconFinderInterface; |
| 24 | + |
| 25 | +#[AsCommand( |
| 26 | + name: 'ux:icons:manage', |
| 27 | + description: 'List, group or remove icons from your local icon directory.', |
| 28 | +)] |
| 29 | +final class ManageIconsCommand extends Command |
| 30 | +{ |
| 31 | + public function __construct( |
| 32 | + private readonly LocalSvgIconRegistry $localRegistry, |
| 33 | + private readonly IconFinderInterface $iconFinder, |
| 34 | + ) { |
| 35 | + parent::__construct(); |
| 36 | + } |
| 37 | + |
| 38 | + protected function configure(): void |
| 39 | + { |
| 40 | + $this |
| 41 | + ->setHelp(<<<'HELP' |
| 42 | + The <info>%command.name%</info> command helps you manage locally stored icons. |
| 43 | +
|
| 44 | + Note: |
| 45 | + Icons used dynamically, such as {{ ux_icon('flag-' ~ locale) }}, may appear as unused even if they are rendered at runtime. |
| 46 | +
|
| 47 | + Usage: |
| 48 | + php %command.full_name% [options] |
| 49 | +
|
| 50 | + Options: |
| 51 | + --list List all icons |
| 52 | + --group Group icons by set (use with --list) |
| 53 | + --unused Filter only unused icons (use with --list or --remove) |
| 54 | + --table Show listing in table format (use with --list) |
| 55 | + --remove[=ICON] Remove a specific icon by name, or use with --unused to delete all unused icons |
| 56 | + --remove-all Delete all local icons from the directory (use with caution) |
| 57 | +
|
| 58 | + Examples: |
| 59 | + php %command.full_name% --list |
| 60 | + php %command.full_name% --list --group |
| 61 | + php %command.full_name% --list --unused |
| 62 | + php %command.full_name% --list --group --unused |
| 63 | + php %command.full_name% --list --table |
| 64 | + php %command.full_name% --remove bi:clock |
| 65 | + php %command.full_name% --remove --unused |
| 66 | + php %command.full_name% --remove-all |
| 67 | + HELP |
| 68 | + ) |
| 69 | + ->addOption('list', null, InputOption::VALUE_NONE, 'List icons.') |
| 70 | + ->addOption('group', null, InputOption::VALUE_NONE, 'Group icons by set when listing.') |
| 71 | + ->addOption('unused', null, InputOption::VALUE_NONE, 'Filter unused icons.') |
| 72 | + ->addOption('table', null, InputOption::VALUE_NONE, 'Show listing in table format.') |
| 73 | + ->addOption('remove', null, InputOption::VALUE_OPTIONAL, 'Remove a specific icon or use with --unused to delete all unused icons.') |
| 74 | + ->addOption('remove-all', null, InputOption::VALUE_NONE, 'Delete all icons from the local directory.'); |
| 75 | + } |
| 76 | + |
| 77 | + protected function execute(InputInterface $input, OutputInterface $output): int |
| 78 | + { |
| 79 | + $io = new SymfonyStyle($input, $output); |
| 80 | + |
| 81 | + // Get icon directory |
| 82 | + $ref = new \ReflectionProperty($this->localRegistry, 'iconDir'); |
| 83 | + $iconDir = str_replace('\\', '/', $ref->getValue($this->localRegistry)); |
| 84 | + |
| 85 | + if (!is_dir($iconDir)) { |
| 86 | + $io->error("Icon directory not found: {$iconDir}"); |
| 87 | + |
| 88 | + return Command::FAILURE; |
| 89 | + } |
| 90 | + |
| 91 | + $list = $input->getOption('list'); |
| 92 | + $group = $input->getOption('group'); |
| 93 | + $unusedOnly = $input->getOption('unused'); |
| 94 | + $tableMode = $input->getOption('table'); |
| 95 | + $remove = $input->getOption('remove'); |
| 96 | + $removeAll = $input->getOption('remove-all'); |
| 97 | + |
| 98 | + // Scan local icons if needed |
| 99 | + $localIcons = []; |
| 100 | + if ($list || $removeAll || ($remove && $unusedOnly) || $remove) { |
| 101 | + $finder = new Finder(); |
| 102 | + $finder->files()->in($iconDir)->name('*.svg'); |
| 103 | + foreach ($finder as $file) { |
| 104 | + $relative = str_replace('\\', '/', $file->getRelativePathname()); |
| 105 | + $iconName = str_replace('/', ':', preg_replace('#\.svg$#', '', $relative)); |
| 106 | + $localIcons[$iconName] = $file->getRealPath(); |
| 107 | + } |
| 108 | + |
| 109 | + if (empty($localIcons)) { |
| 110 | + $io->warning('No local icons found.'); |
| 111 | + |
| 112 | + return Command::SUCCESS; |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + // Find used icons if needed |
| 117 | + $usedIcons = ($list || ($remove && $unusedOnly)) ? $this->iconFinder->iconsInTemplates() : []; |
| 118 | + $unusedIcons = array_diff_key($localIcons, array_flip($usedIcons)); |
| 119 | + |
| 120 | + // Listing |
| 121 | + if ($list) { |
| 122 | + $title = $unusedOnly ? 'Unused Icons Listing' : 'All Icons Listing'; |
| 123 | + $io->title($title); |
| 124 | + |
| 125 | + $iconsToShow = $unusedOnly ? $unusedIcons : $localIcons; |
| 126 | + |
| 127 | + if (empty($iconsToShow)) { |
| 128 | + $io->success($unusedOnly ? 'No unused icons found.' : 'No icons found.'); |
| 129 | + |
| 130 | + return Command::SUCCESS; |
| 131 | + } |
| 132 | + |
| 133 | + if ($tableMode) { |
| 134 | + $table = new Table($output); |
| 135 | + $table->setHeaders(['Set', 'Icon Name', 'Status']); |
| 136 | + foreach ($iconsToShow as $icon => $path) { |
| 137 | + $parts = explode(':', $icon, 2); |
| 138 | + $set = isset($parts[1]) ? $parts[0] : '-'; |
| 139 | + $status = isset($unusedIcons[$icon]) ? '<fg=red>Unused</>' : '<fg=green>Used</>'; |
| 140 | + $table->addRow([$set, $icon, $status]); |
| 141 | + } |
| 142 | + $table->render(); |
| 143 | + } else { |
| 144 | + // Group |
| 145 | + if ($group) { |
| 146 | + $grouped = []; |
| 147 | + foreach ($iconsToShow as $icon => $path) { |
| 148 | + $parts = explode(':', $icon, 2); |
| 149 | + $set = isset($parts[1]) ? $parts[0] : '-'; |
| 150 | + $grouped[$set][] = $icon; |
| 151 | + } |
| 152 | + |
| 153 | + foreach ($grouped as $set => $icons) { |
| 154 | + $io->section("Set: <info>$set</info>"); |
| 155 | + foreach ($icons as $icon) { |
| 156 | + $color = isset($unusedIcons[$icon]) ? 'red' : 'green'; |
| 157 | + $io->writeln(" - <fg={$color}>{$icon}</>"); |
| 158 | + } |
| 159 | + $io->newLine(); |
| 160 | + } |
| 161 | + } else { |
| 162 | + foreach ($iconsToShow as $icon => $path) { |
| 163 | + $color = isset($unusedIcons[$icon]) ? 'red' : 'green'; |
| 164 | + $io->writeln(" - <fg={$color}>{$icon}</>"); |
| 165 | + } |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + $io->note(\sprintf('%d %s icons found.', \count($iconsToShow), $unusedOnly ? 'unused' : 'total')); |
| 170 | + |
| 171 | + return Command::SUCCESS; |
| 172 | + } |
| 173 | + |
| 174 | + // Remove all |
| 175 | + if ($removeAll) { |
| 176 | + if ($io->confirm('Are you sure you want to delete ALL icons?', false)) { |
| 177 | + foreach ($localIcons as $icon => $path) { |
| 178 | + @unlink($path); |
| 179 | + } |
| 180 | + $io->success('All icons have been deleted.'); |
| 181 | + } else { |
| 182 | + $io->info('Operation cancelled.'); |
| 183 | + } |
| 184 | + |
| 185 | + return Command::SUCCESS; |
| 186 | + } |
| 187 | + |
| 188 | + // Remove unused |
| 189 | + if ($remove && $unusedOnly) { |
| 190 | + if (empty($unusedIcons)) { |
| 191 | + $io->success('No unused icons to delete.'); |
| 192 | + |
| 193 | + return Command::SUCCESS; |
| 194 | + } |
| 195 | + |
| 196 | + if ($io->confirm(\sprintf('Delete all %d unused icons?', \count($unusedIcons)), true)) { |
| 197 | + foreach ($unusedIcons as $icon => $path) { |
| 198 | + @unlink($path); |
| 199 | + } |
| 200 | + $io->success('All unused icons deleted.'); |
| 201 | + } else { |
| 202 | + $io->info('Operation cancelled.'); |
| 203 | + } |
| 204 | + |
| 205 | + return Command::SUCCESS; |
| 206 | + } |
| 207 | + |
| 208 | + // Remove single icon |
| 209 | + if ($remove && !$unusedOnly) { |
| 210 | + $target = str_replace(':', '/', $remove); |
| 211 | + $iconPath = "{$iconDir}/{$target}.svg"; |
| 212 | + |
| 213 | + if (!file_exists($iconPath)) { |
| 214 | + $io->warning("Icon not found: {$remove}"); |
| 215 | + |
| 216 | + return Command::SUCCESS; |
| 217 | + } |
| 218 | + |
| 219 | + if ($io->confirm("Are you sure you want to delete '{$remove}'?", false)) { |
| 220 | + @unlink($iconPath); |
| 221 | + $io->success("Deleted icon: {$remove}"); |
| 222 | + } else { |
| 223 | + $io->info('Operation cancelled.'); |
| 224 | + } |
| 225 | + |
| 226 | + return Command::SUCCESS; |
| 227 | + } |
| 228 | + |
| 229 | + $io->note('Use --list, --remove, or --remove-all options to manage your icons.'); |
| 230 | + |
| 231 | + return Command::SUCCESS; |
| 232 | + } |
| 233 | +} |
0 commit comments