Skip to content

Commit 9fabea0

Browse files
committed
refactor
1 parent 6f2e745 commit 9fabea0

File tree

2 files changed

+45
-42
lines changed

2 files changed

+45
-42
lines changed

src/Icons/src/Registry/LocalSvgIconRegistry.php

Lines changed: 1 addition & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
namespace Symfony\UX\Icons\Registry;
1313

1414
use Symfony\Component\Filesystem\Filesystem;
15-
use Symfony\Component\Finder\Finder;
1615
use Symfony\UX\Icons\Exception\IconNotFoundException;
1716
use Symfony\UX\Icons\IconRegistryInterface;
1817
use Symfony\UX\Icons\Svg\Icon;
@@ -34,42 +33,7 @@ public function get(string $name): Icon
3433
throw new IconNotFoundException(sprintf('The icon "%s" (%s) does not exist.', $name, $filename));
3534
}
3635

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);
7337
}
7438

7539
public function add(string $name, string $svg): void
@@ -78,9 +42,4 @@ public function add(string $name, string $svg): void
7842

7943
(new Filesystem())->dumpFile($filename, $svg);
8044
}
81-
82-
private function finder(): Finder
83-
{
84-
return Finder::create()->in($this->iconDir)->files()->name('*.svg');
85-
}
8645
}

src/Icons/src/Svg/Icon.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,50 @@ public static function isValidName(string $name): bool
7676
return (bool) preg_match('#^([a-z0-9]+(-[a-z0-9]+)*)(:[a-z0-9]+(-[a-z0-9]+)*)*$#', $name);
7777
}
7878

79+
public static function fromFile(string $filename): self
80+
{
81+
if (!class_exists(\DOMDocument::class)) {
82+
throw new \LogicException('The "DOM" PHP extension is required to create icons from files.');
83+
}
84+
85+
$svg = file_get_contents($filename) ?: throw new \RuntimeException(sprintf('The icon file "%s" could not be read.', $filename));
86+
87+
$doc = new \DOMDocument();
88+
$doc->preserveWhiteSpace = false;
89+
90+
try {
91+
$doc->loadXML($svg);
92+
} catch (\Throwable $e) {
93+
throw new \RuntimeException(sprintf('The icon file "%s" does not contain a valid SVG.', $filename), previous: $e);
94+
}
95+
96+
$svgElements = $doc->getElementsByTagName('svg');
97+
98+
if (0 === $svgElements->length) {
99+
throw new \RuntimeException(sprintf('The icon file "%s" does not contain a valid SVG.', $filename));
100+
}
101+
102+
if (1 !== $svgElements->length) {
103+
throw new \RuntimeException(sprintf('The icon file "%s" contains more than one SVG.', $filename));
104+
}
105+
106+
$svgElement = $svgElements->item(0) ?? throw new \RuntimeException(sprintf('The icon file "%s" does not contain a valid SVG.', $filename));
107+
108+
$innerSvg = '';
109+
110+
foreach ($svgElement->childNodes as $child) {
111+
$innerSvg .= $doc->saveHTML($child);
112+
}
113+
114+
if (!$innerSvg) {
115+
throw new \RuntimeException(sprintf('The icon file "%s" contains an empty SVG.', $filename));
116+
}
117+
118+
$attributes = array_map(static fn (\DOMAttr $a) => $a->value, [...$svgElement->attributes]);
119+
120+
return new self($innerSvg, $attributes);
121+
}
122+
79123
public function __construct(
80124
private readonly string $innerSvg,
81125
private readonly array $attributes = [],

0 commit comments

Comments
 (0)