Skip to content

Commit bfe8eec

Browse files
committed
add IconCacheWarmer
1 parent 951e1e5 commit bfe8eec

File tree

3 files changed

+65
-14
lines changed

3 files changed

+65
-14
lines changed

src/Icons/config/services.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
1313

1414
use Symfony\UX\Icons\Command\WarmCacheCommand;
15+
use Symfony\UX\Icons\IconCacheWarmer;
1516
use Symfony\UX\Icons\IconRenderer;
1617
use Symfony\UX\Icons\Registry\CacheIconRegistry;
1718
use Symfony\UX\Icons\Registry\ChainIconRegistry;
@@ -54,11 +55,16 @@
5455
service('twig'),
5556
])
5657

57-
->set('.ux_icons.command.warm_cache', WarmCacheCommand::class)
58+
->set('.ux_icons.cache_warmer', IconCacheWarmer::class)
5859
->args([
5960
service('.ux_icons.cache_icon_registry'),
6061
service('.ux_icons.twig_icon_finder'),
6162
])
63+
64+
->set('.ux_icons.command.warm_cache', WarmCacheCommand::class)
65+
->args([
66+
service('.ux_icons.cache_warmer'),
67+
])
6268
->tag('console.command')
6369
;
6470
};

src/Icons/src/Command/WarmCacheCommand.php

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@
1616
use Symfony\Component\Console\Input\InputInterface;
1717
use Symfony\Component\Console\Output\OutputInterface;
1818
use Symfony\Component\Console\Style\SymfonyStyle;
19-
use Symfony\UX\Icons\Exception\IconNotFoundException;
20-
use Symfony\UX\Icons\Registry\CacheIconRegistry;
21-
use Symfony\UX\Icons\Twig\IconFinder;
19+
use Symfony\UX\Icons\IconCacheWarmer;
2220

2321
/**
2422
* @author Kevin Bond <[email protected]>
@@ -31,7 +29,7 @@
3129
)]
3230
final class WarmCacheCommand extends Command
3331
{
34-
public function __construct(private CacheIconRegistry $registry, private IconFinder $icons)
32+
public function __construct(private IconCacheWarmer $warmer)
3533
{
3634
parent::__construct();
3735
}
@@ -41,16 +39,14 @@ protected function execute(InputInterface $input, OutputInterface $output): int
4139
$io = new SymfonyStyle($input, $output);
4240
$io->comment('Warming the icon cache...');
4341

44-
foreach ($this->icons->icons() as $icon) {
45-
try {
46-
$this->registry->get($icon, refresh: true);
47-
48-
if ($output->isVerbose()) {
49-
$io->writeln(sprintf(' Warmed icon <comment>%s</comment>.', $icon));
42+
$this->warmer->warm(
43+
onSuccess: function (string $name) use ($io) {
44+
if ($io->isVerbose()) {
45+
$io->writeln(sprintf(' Warmed icon <comment>%s</comment>.', $name));
5046
}
51-
} catch (IconNotFoundException) {
52-
}
53-
}
47+
},
48+
onFailure: fn (string $name) => $io->warning(sprintf('Icon <comment>%s</comment> not found.', $name))
49+
);
5450

5551
$io->success('Icon cache warmed.');
5652

src/Icons/src/IconCacheWarmer.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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;
13+
14+
use Symfony\UX\Icons\Exception\IconNotFoundException;
15+
use Symfony\UX\Icons\Registry\CacheIconRegistry;
16+
use Symfony\UX\Icons\Svg\Icon;
17+
use Symfony\UX\Icons\Twig\IconFinder;
18+
19+
/**
20+
* @author Kevin Bond <[email protected]>
21+
*
22+
* @internal
23+
*/
24+
final class IconCacheWarmer
25+
{
26+
public function __construct(private CacheIconRegistry $registry, private IconFinder $icons)
27+
{
28+
}
29+
30+
/**
31+
* @param callable(string,Icon):void|null $onSuccess
32+
* @param callable(string):void|null $onFailure
33+
*/
34+
public function warm(?callable $onSuccess = null, ?callable $onFailure = null): void
35+
{
36+
$onSuccess ??= fn (string $name, Icon $icon) => null;
37+
$onFailure ??= fn (string $name) => null;
38+
39+
foreach ($this->icons->icons() as $name) {
40+
try {
41+
$icon = $this->registry->get($name, refresh: true);
42+
43+
$onSuccess($name, $icon);
44+
} catch (IconNotFoundException) {
45+
$onFailure($name);
46+
}
47+
}
48+
}
49+
}

0 commit comments

Comments
 (0)