symfony / ux

Symfony UX initiative: a JavaScript ecosystem for Symfony
https://ux.symfony.com/
MIT License
857 stars 315 forks source link

[icons] Dispatch an event during ux:icons:lock #2365

Open tacman opened 2 weeks ago

tacman commented 2 weeks ago

I'm using knp_dictionary to define icons by class:

    $containerConfigurator->extension('knp_dictionary', [
        'dictionaries' => [
            'class_icons' => [
                'type' => 'key_value',
                'content' => [
                    \App\Entity\Member::class => 'mdi:user',
                    \App\Entity\DirectoryCollection::class => 'mdi:users',
                    \App\Entity\Tag::class => 'mdi:tag',
                    \App\Entity\Donation::class => 'mdi:dollar',
                    \App\Entity\CommunicationLog::class => 'mdi:envelope',
                    \App\Entity\Event::class => 'tabler:calendar'
                ]
            ],

These work as expected in dev, as the ux_icon renderer fetches the icon on demand. I'd like to import them to production during the lock

bin/console ux:icons:lock

Of course, it doesn't now. My idea for implementation is dispatching an event that I could listen for and add the icons based on how I store them. For example, I recently discovered https://github.com/zenstruck/class-metadata and considered putting my icons there instead, e.g.

#[Metadata('icon', 'mdi:user')]
class User

In one project I have the icons as twig globals in twig.yaml.

In short, I'd like a way to register variable icons to make the discoverable during the lock command.

smnandre commented 2 weeks ago

If you already have a way to list all of your icons, right now the best method would be to import them just before the lock with some PhpSubProccess?

I've updated its code the other day and it now downloads up to 60 icons at once (if they have common iconset), this should be very quick.

tacman commented 2 weeks ago

the best method would be to import them just before the lock with some PhpSubProccess?

I'm not following. I have my icons sprinkled about, in twig, in yaml, in php, in attributes, and maybe someday in the database.

They work in dev, but not in prod (because of the very slick fetching). I'm not familiar with PhpSubProccesses.

Obviously, I could copy the lock command and tweak it for my purposes. But it seems ripe for extending, right now it's pretty much a regex in the twig file. Which pretty much finds anything with a colon in a string:

ux:icons:lock -vvv

 // Scanning project for icons...                                                                                       

 ✗ IconSet Not Found: max-height:40vh.
 ✗ IconSet Not Found: max-height:70vh.
 ✗ IconSet Not Found: max-width:40rem.
 ✗ IconSet Not Found: max-height:30vh.
 ✗ IconSet Not Found: color:transparent.
 ✗ IconSet Not Found: lg:col-6.
 ✗ IconSet Not Found: width:30px.

Can you point me to what you mean by a PhpSubProccess?

smnandre commented 2 weeks ago

Of course! Here is the documentation related to your need: Executing a PHP Child Process with the Same Configuration

Let's say your icons are a:foo, b:bar and b:foobar

$childProcess = new PhpSubprocess(['bin/console', 'ux:icons:import', ['a:foo', 'b:bar', 'b:foobar']);
$childProcess->run();

This will run the import command in the same configuration / environment that your code is, and will allow you to use the UX Icons import command !

Finally, if you have a fixed list of icons, the best method is probably to commit them in the repository :)