Skip to content

Conversation

@xDeSwa
Copy link
Contributor

@xDeSwa xDeSwa commented Nov 12, 2025

Q A
Bug fix? no
New feature? yes
Deprecations? no
Documentation? yes
Issues #
License MIT

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

    # Lists all icons found in the local icon directory.Can be combined with `--table` or `--group`
    $ php bin/console ux:icons:manage --list

    # List all unused Icons in templates. Can be combined with `--table` or `--group`
    $ php bin/console ux:icons:manage --list --unused

    # Removes a specific icon by name (2 methods)
    $ php bin/console ux:icons:manage --remove=flowbite:user-solid
    $ php bin/console ux:icons:manage --remove user-profile

    # Deletes all icons that are not detected as used in templates. (Use with caution)
    $ php bin/console ux:icons:manage --remove --unused

    # Deletes *all* local icons from the directory. (Use with extreme caution)
    $ php bin/console ux:icons:manage --remove-all

Screenshots

All Icons List example allicons_list
Unused List example unused_list
Unused List Table view example unused_table
Unused List Group view example unused_list_group

@carsonbot carsonbot added Documentation Improvements or additions to documentation Feature New Feature Icons Status: Needs Review Needs to be reviewed labels Nov 12, 2025
Copy link
Member

@Kocal Kocal left a 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!

@xDeSwa
Copy link
Contributor Author

xDeSwa commented Nov 14, 2025

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:

{{ ux_icon('svg-spinners:bars-scale-fade') }}

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:

{{ ux_icon('symfony') }}

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Documentation Improvements or additions to documentation Feature New Feature Icons Status: Needs Review Needs to be reviewed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants