16
16
use Symfony \Component \HttpClient \ScopingHttpClient ;
17
17
use Symfony \Contracts \Cache \CacheInterface ;
18
18
use Symfony \Contracts \HttpClient \HttpClientInterface ;
19
+ use Symfony \UX \Icons \Exception \HttpClientNotInstalledException ;
19
20
use Symfony \UX \Icons \Exception \IconNotFoundException ;
20
21
21
22
/**
@@ -39,15 +40,10 @@ final class Iconify
39
40
40
41
public function __construct (
41
42
private CacheInterface $ cache ,
42
- string $ endpoint = self ::API_ENDPOINT ,
43
- ?HttpClientInterface $ http = null ,
43
+ private string $ endpoint = self ::API_ENDPOINT ,
44
+ private ?HttpClientInterface $ httpClient = null ,
44
45
?int $ maxIconsQueryLength = null ,
45
46
) {
46
- if (!class_exists (HttpClient::class)) {
47
- throw new \LogicException ('You must install "symfony/http-client" to use Iconify. Try running "composer require symfony/http-client". ' );
48
- }
49
-
50
- $ this ->http = ScopingHttpClient::forBaseUri ($ http ?? HttpClient::create (), $ endpoint );
51
47
$ this ->maxIconsQueryLength = min (self ::MAX_ICONS_QUERY_LENGTH , $ maxIconsQueryLength ?? self ::MAX_ICONS_QUERY_LENGTH );
52
48
}
53
49
@@ -62,7 +58,7 @@ public function fetchIcon(string $prefix, string $name): Icon
62
58
throw new IconNotFoundException (\sprintf ('The icon "%s:%s" does not exist on iconify.design. ' , $ prefix , $ name ));
63
59
}
64
60
65
- $ response = $ this ->http ->request ('GET ' , \sprintf ('/%s.json?icons=%s ' , $ prefix , $ name ));
61
+ $ response = $ this ->http () ->request ('GET ' , \sprintf ('/%s.json?icons=%s ' , $ prefix , $ name ));
66
62
67
63
if (200 !== $ response ->getStatusCode ()) {
68
64
throw new IconNotFoundException (\sprintf ('The icon "%s:%s" does not exist on iconify.design. ' , $ prefix , $ name ));
@@ -112,7 +108,7 @@ public function fetchIcons(string $prefix, array $names): array
112
108
throw new \InvalidArgumentException ('The query string is too long. ' );
113
109
}
114
110
115
- $ response = $ this ->http ->request ('GET ' , \sprintf ('/%s.json ' , $ prefix ), [
111
+ $ response = $ this ->http () ->request ('GET ' , \sprintf ('/%s.json ' , $ prefix ), [
116
112
'headers ' => [
117
113
'Accept ' => 'application/json ' ,
118
114
],
@@ -158,7 +154,7 @@ public function getIconSets(): array
158
154
159
155
public function searchIcons (string $ prefix , string $ query )
160
156
{
161
- $ response = $ this ->http ->request ('GET ' , '/search ' , [
157
+ $ response = $ this ->http () ->request ('GET ' , '/search ' , [
162
158
'query ' => [
163
159
'query ' => $ query ,
164
160
'prefix ' => $ prefix ,
@@ -205,9 +201,22 @@ public function chunk(string $prefix, array $names): iterable
205
201
private function sets (): \ArrayObject
206
202
{
207
203
return $ this ->sets ??= $ this ->cache ->get ('iconify-sets ' , function () {
208
- $ response = $ this ->http ->request ('GET ' , '/collections ' );
204
+ $ response = $ this ->http () ->request ('GET ' , '/collections ' );
209
205
210
206
return new \ArrayObject ($ response ->toArray ());
211
207
});
212
208
}
209
+
210
+ private function http (): HttpClientInterface
211
+ {
212
+ if (isset ($ this ->http )) {
213
+ return $ this ->http ;
214
+ }
215
+
216
+ if (!class_exists (HttpClient::class)) {
217
+ throw new HttpClientNotInstalledException ('You must install "symfony/http-client" to use icons from ux.symfony.com/icons. Try running "composer require symfony/http-client". ' );
218
+ }
219
+
220
+ return $ this ->http = ScopingHttpClient::forBaseUri ($ this ->httpClient ?? HttpClient::create (), $ this ->endpoint );
221
+ }
213
222
}
0 commit comments