Skip to content

Commit 1347c74

Browse files
committed
refactor CacheIconRegistry simply decorate IconRegistryInterface
1 parent a1ddab3 commit 1347c74

File tree

2 files changed

+19
-29
lines changed

2 files changed

+19
-29
lines changed

src/Icons/config/services.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
$container->services()
2323
->set('.ux_icons.cache_icon_registry', CacheIconRegistry::class)
2424
->args([
25-
iterator([service('.ux_icons.local_svg_icon_registry')]),
25+
service('.ux_icons.local_svg_icon_registry'),
2626
service('cache.system'),
2727
])
2828
->tag('kernel.cache_warmer')

src/Icons/src/Registry/CacheIconRegistry.php

Lines changed: 18 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -24,41 +24,18 @@
2424
*/
2525
final class CacheIconRegistry implements IconRegistryInterface, CacheWarmerInterface
2626
{
27-
/**
28-
* @param IconRegistryInterface[] $registries
29-
*/
30-
public function __construct(private \Traversable $registries, private CacheInterface $cache)
27+
public function __construct(private IconRegistryInterface $inner, private CacheInterface $cache)
3128
{
3229
}
3330

34-
public function get(string $name, bool $refresh = false): Icon
31+
public function get(string $name): Icon
3532
{
36-
if (!Icon::isValidName($name)) {
37-
throw new IconNotFoundException(sprintf('The icon name "%s" is not valid.', $name));
38-
}
39-
40-
return $this->cache->get(
41-
sprintf('ux-icon-%s', Icon::nameToId($name)),
42-
function () use ($name) {
43-
foreach ($this->registries as $registry) {
44-
try {
45-
return $registry->get($name);
46-
} catch (IconNotFoundException) {
47-
// ignore
48-
}
49-
}
50-
51-
throw new IconNotFoundException(sprintf('The icon "%s" does not exist.', $name));
52-
},
53-
beta: $refresh ? \INF : null,
54-
);
33+
return $this->fetchIcon($name);
5534
}
5635

5736
public function getIterator(): \Traversable
5837
{
59-
foreach ($this->registries as $registry) {
60-
yield from $registry;
61-
}
38+
yield from $this->inner;
6239
}
6340

6441
public function isOptional(): bool
@@ -69,9 +46,22 @@ public function isOptional(): bool
6946
public function warmUp(string $cacheDir, ?string $buildDir = null): array
7047
{
7148
foreach ($this as $name) {
72-
$this->get($name, refresh: true);
49+
$this->fetchIcon($name, refresh: true);
7350
}
7451

7552
return [];
7653
}
54+
55+
private function fetchIcon(string $name, bool $refresh = false): Icon
56+
{
57+
if (!Icon::isValidName($name)) {
58+
throw new IconNotFoundException(sprintf('The icon name "%s" is not valid.', $name));
59+
}
60+
61+
return $this->cache->get(
62+
sprintf('ux-icon-%s', Icon::nameToId($name)),
63+
fn () => $this->inner->get($name),
64+
beta: $refresh ? \INF : null,
65+
);
66+
}
7767
}

0 commit comments

Comments
 (0)