24
24
*/
25
25
final class CacheIconRegistry implements IconRegistryInterface, CacheWarmerInterface
26
26
{
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 )
31
28
{
32
29
}
33
30
34
- public function get (string $ name, bool $ refresh = false ): Icon
31
+ public function get (string $ name ): Icon
35
32
{
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 );
55
34
}
56
35
57
36
public function getIterator (): \Traversable
58
37
{
59
- foreach ($ this ->registries as $ registry ) {
60
- yield from $ registry ;
61
- }
38
+ yield from $ this ->inner ;
62
39
}
63
40
64
41
public function isOptional (): bool
@@ -69,9 +46,22 @@ public function isOptional(): bool
69
46
public function warmUp (string $ cacheDir , ?string $ buildDir = null ): array
70
47
{
71
48
foreach ($ this as $ name ) {
72
- $ this ->get ($ name , refresh: true );
49
+ $ this ->fetchIcon ($ name , refresh: true );
73
50
}
74
51
75
52
return [];
76
53
}
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
+ }
77
67
}
0 commit comments