-
-
Notifications
You must be signed in to change notification settings - Fork 394
[Icons] New console command to manage local icons. #3167
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: 2.x
Are you sure you want to change the base?
Conversation
Kocal
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey, and thanks for your contribution!
I quickly looked at your PR and I think it will be easier if your options --list, --remove, --remove-all would be in fact a single argument action allowing values list, remove, and remove-all.
Also, I'm bit afraid about all these new logic for searching used/unused icons in templates or icons dir. I don't know Icons internals, but this kind of logic does not already exist?
I will take more time soon to better review your PR.
Thanks!
public function icons(): array
{
$found = [];
// https://regex101.com/r/WGa4iF/1
$token = '[a-z0-9]+(?:-[a-z0-9]+)*';
$pattern = "#(?:'$token:$token')|(?:\"$token:$token\")#i";
// Extract icon names from strings in app templates
foreach ($this->templateFiles($this->twig->getLoader()) as $file) {
$contents = file_get_contents($file);
if (preg_match_all($pattern, $contents, $matches)) {
$found[] = array_map(fn ($res) => trim($res, '"\''), $matches[0]);
}
}
$found = array_merge(...$found);
// Extract prefix-less SVG files from the root of the icon directory
if (is_dir($this->iconDirectory)) {
$icons = (new Finder())->files()->in($this->iconDirectory)->depth(0)->name('*.svg');
foreach ($icons as $icon) {
$found[] = $icon->getBasename('.svg');
}
}
return array_unique($found);
}Here is my understanding of the current behavior of the icons() method and why I proposed an additional function. The existing implementation correctly detects: Icons that are used in Twig templates with a prefix, for example:
SVG files that exist directly in the root of the configured icon directory. However, as far as I can see, the current regex only matches icon names in the form prefix:name (containing a colon). Because of that, icons that are used without a prefix — for example:
are not detected at all, even if they exist in the filesystem. To address this, I introduced an additional helper that scans Twig templates and extracts all icon usages, including prefix-less ones. This allows the command to return a complete list of icons referenced in the project. Can you give examples of command arguments? Thanks. |
|
Wow, lot of work here! 🙇 Couple of quick comments: let's not confuse the cache warmer (looking at everything that could be an icon) and the whole component :) There is no way to statically ensure an icon is not used, as we can use variables and context-related names in the component. So "unused" seems pretty definitive to me and may confuse users with very legitimate usage. An exemple I frequently use in my projects: <twig:ux:icon name="circle-flags:{{ user.country }}" />I'm not at ease with the remove commands to be honest, as I'm not sure of the pros/cons balance 😅 You said
And I wonder if calling this command Then I would agree with @Kocal: maybe we could use the first argument as a "command"?
wdyt? |
<twig:ux:icon name="circle-flags:{{ user.country }}" />does not include such dynamically used icons in the list and icons that have not been downloaded locally cannot be included in these lists. I added a warning note about this in the documentation. |
|
The scenario I d really like to avoid is:
:)
|
|
Hello again, These changes include: Screenshots are below. If this makes sense, I'll update the PR. All Command List php bin/console ux:icons:local list
php bin/console ux:icons:local info bi:check
php bin/console ux:icons:local remove bi:check
php bin/console ux:icons:local remove-all |



Summary
This PR introduces a new Symfony console command
ux:icons:manage, designed to simplify the management of locally stored icons within the project.It provides flexible options to list, group, and safely remove unused icons.
Usage Examples
Screenshots
All Icons List example
Unused List example
Unused List Table view example
Unused List Group view example