12
12
namespace Symfony \UX \Icons \Registry ;
13
13
14
14
use Symfony \Component \Filesystem \Filesystem ;
15
- use Symfony \Component \Finder \Finder ;
16
15
use Symfony \UX \Icons \Exception \IconNotFoundException ;
17
16
use Symfony \UX \Icons \IconRegistryInterface ;
18
17
use Symfony \UX \Icons \Svg \Icon ;
@@ -34,42 +33,7 @@ public function get(string $name): Icon
34
33
throw new IconNotFoundException (sprintf ('The icon "%s" (%s) does not exist. ' , $ name , $ filename ));
35
34
}
36
35
37
- $ svg = file_get_contents ($ filename ) ?: throw new \RuntimeException (sprintf ('The icon file "%s" could not be read. ' , $ filename ));
38
-
39
- $ doc = new \DOMDocument ();
40
- $ doc ->preserveWhiteSpace = false ;
41
-
42
- try {
43
- $ doc ->loadXML ($ svg );
44
- } catch (\Throwable $ e ) {
45
- throw new \RuntimeException (sprintf ('The icon file "%s" does not contain a valid SVG. ' , $ filename ), previous: $ e );
46
- }
47
-
48
- $ svgElements = $ doc ->getElementsByTagName ('svg ' );
49
-
50
- if (0 === $ svgElements ->length ) {
51
- throw new \RuntimeException (sprintf ('The icon file "%s" does not contain a valid SVG. ' , $ filename ));
52
- }
53
-
54
- if (1 !== $ svgElements ->length ) {
55
- throw new \RuntimeException (sprintf ('The icon file "%s" contains more than one SVG. ' , $ filename ));
56
- }
57
-
58
- $ svgElement = $ svgElements ->item (0 ) ?? throw new \RuntimeException (sprintf ('The icon file "%s" does not contain a valid SVG. ' , $ filename ));
59
-
60
- $ innerSvg = '' ;
61
-
62
- foreach ($ svgElement ->childNodes as $ child ) {
63
- $ innerSvg .= $ doc ->saveHTML ($ child );
64
- }
65
-
66
- if (!$ innerSvg ) {
67
- throw new \RuntimeException (sprintf ('The icon file "%s" contains an empty SVG. ' , $ filename ));
68
- }
69
-
70
- $ attributes = array_map (static fn (\DOMAttr $ a ) => $ a ->value , [...$ svgElement ->attributes ]);
71
-
72
- return new Icon ($ innerSvg , $ attributes );
36
+ return Icon::fromFile ($ filename );
73
37
}
74
38
75
39
public function add (string $ name , string $ svg ): void
@@ -78,9 +42,4 @@ public function add(string $name, string $svg): void
78
42
79
43
(new Filesystem ())->dumpFile ($ filename , $ svg );
80
44
}
81
-
82
- private function finder (): Finder
83
- {
84
- return Finder::create ()->in ($ this ->iconDir )->files ()->name ('*.svg ' );
85
- }
86
45
}
0 commit comments