Skip to content

[Icons] Cache icon set list #1601

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions src/Icons/src/Iconify.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Symfony\Component\HttpClient\Exception\JsonException;
use Symfony\Component\HttpClient\HttpClient;
use Symfony\Component\HttpClient\ScopingHttpClient;
use Symfony\Contracts\Cache\CacheInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\UX\Icons\Exception\IconNotFoundException;
use Symfony\UX\Icons\Svg\Icon;
Expand All @@ -27,9 +28,12 @@ final class Iconify
{
private HttpClientInterface $http;

private array $iconSets;

public function __construct(
string $endpoint = 'https://api.iconify.design',
?HttpClientInterface $http = null,
private readonly ?CacheInterface $cache = null,
) {
if (!class_exists(HttpClient::class)) {
throw new \LogicException('You must install "symfony/http-client" to use Iconify. Try running "composer require symfony/http-client".');
Expand All @@ -45,6 +49,15 @@ public function metadataFor(string $prefix): array
return $response->toArray()[$prefix] ?? throw new \RuntimeException(sprintf('The icon prefix "%s" does not exist on iconify.design.', $prefix));
}

public function getIconSets(): array
{
if (!isset($this->iconSets) && $this->cache) {
return $this->iconSets = $this->cache->get('iconify.design.icon_sets', $this->fetchCollections(...));
}

return $this->iconSets ??= $this->fetchCollections();
}

public function fetchIcon(string $prefix, string $name): Icon
{
$response = $this->http->request('GET', sprintf('/%s.json?icons=%s', $prefix, $name));
Expand All @@ -68,13 +81,25 @@ public function fetchSvg(string $prefix, string $name): string
{
$content = $this->http
->request('GET', sprintf('/%s/%s.svg', $prefix, $name))
->getContent()
;
->getContent();

if (!str_starts_with($content, '<svg')) {
throw new IconNotFoundException(sprintf('The icon "%s:%s" does not exist on iconify.design.', $prefix, $name));
}

return $content;
}

private function fetchCollections(): array
{
$response = $this->http->request('GET', '/collections');
if (200 !== $response->getStatusCode()) {
throw new \RuntimeException('Could not fetch icon collections from iconify.design.');
}
if (null === $collections = $response->toArray()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

toArray never returns null

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Omg what did wrote ?

(the idea behind this PR was to prevent some unhandled 404 (when the icon set does not exists).. but with your refacto this is probably no more usefull)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can close it now i think ?

throw new \RuntimeException('Could not fetch icon collections from iconify.design.');
}

return $collections;
}
}