Skip to content

Commit c2c93d9

Browse files
committed
[Icons] Ensure prefix exists before fetching icons from iconify
1 parent b992fb2 commit c2c93d9

File tree

3 files changed

+23
-4
lines changed

3 files changed

+23
-4
lines changed

src/Icons/config/iconify.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
->set('.ux_icons.iconify', Iconify::class)
2727
->args([
28+
service('cache.system'),
2829
abstract_arg('endpoint'),
2930
service('http_client')->nullOnInvalid(),
3031
])

src/Icons/src/DependencyInjection/UXIconsExtension.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ protected function loadInternal(array $mergedConfig, ContainerBuilder $container
9191
$loader->load('iconify.php');
9292

9393
$container->getDefinition('.ux_icons.iconify')
94-
->setArgument(0, $mergedConfig['iconify']['endpoint'])
94+
->setArgument(1, $mergedConfig['iconify']['endpoint'])
9595
;
9696
}
9797

src/Icons/src/Iconify.php

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Component\HttpClient\Exception\JsonException;
1515
use Symfony\Component\HttpClient\HttpClient;
1616
use Symfony\Component\HttpClient\ScopingHttpClient;
17+
use Symfony\Contracts\Cache\CacheInterface;
1718
use Symfony\Contracts\HttpClient\HttpClientInterface;
1819
use Symfony\UX\Icons\Exception\IconNotFoundException;
1920
use Symfony\UX\Icons\Svg\Icon;
@@ -26,8 +27,10 @@
2627
final class Iconify
2728
{
2829
private HttpClientInterface $http;
30+
private \ArrayObject $sets;
2931

3032
public function __construct(
33+
private CacheInterface $cache,
3134
string $endpoint = 'https://api.iconify.design',
3235
?HttpClientInterface $http = null,
3336
) {
@@ -40,13 +43,15 @@ public function __construct(
4043

4144
public function metadataFor(string $prefix): array
4245
{
43-
$response = $this->http->request('GET', sprintf('/collections?prefix=%s', $prefix));
44-
45-
return $response->toArray()[$prefix] ?? throw new \RuntimeException(sprintf('The icon prefix "%s" does not exist on iconify.design.', $prefix));
46+
return $this->sets()[$prefix] ?? throw new \RuntimeException(sprintf('The icon prefix "%s" does not exist on iconify.design.', $prefix));
4647
}
4748

4849
public function fetchIcon(string $prefix, string $name): Icon
4950
{
51+
if (!isset($this->sets()[$prefix])) {
52+
throw new IconNotFoundException(sprintf('The icon "%s:%s" does not exist on iconify.design.', $prefix, $name));
53+
}
54+
5055
$response = $this->http->request('GET', sprintf('/%s.json?icons=%s', $prefix, $name));
5156

5257
try {
@@ -66,6 +71,10 @@ public function fetchIcon(string $prefix, string $name): Icon
6671

6772
public function fetchSvg(string $prefix, string $name): string
6873
{
74+
if (!isset($this->sets()[$prefix])) {
75+
throw new IconNotFoundException(sprintf('The icon "%s:%s" does not exist on iconify.design.', $prefix, $name));
76+
}
77+
6978
$content = $this->http
7079
->request('GET', sprintf('/%s/%s.svg', $prefix, $name))
7180
->getContent()
@@ -77,4 +86,13 @@ public function fetchSvg(string $prefix, string $name): string
7786

7887
return $content;
7988
}
89+
90+
private function sets(): \ArrayObject
91+
{
92+
return $this->sets ??= $this->cache->get('ux-iconify-sets', function () {
93+
$response = $this->http->request('GET', '/collections');
94+
95+
return new \ArrayObject($response->toArray());
96+
});
97+
}
8098
}

0 commit comments

Comments
 (0)