Skip to content

Commit 14d7918

Browse files
committed
bug #1724 [Icons] Fix Icon aliases not found (smnandre)
This PR was squashed before being merged into the 2.x branch. Discussion ---------- [Icons] Fix Icon aliases not found | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | Issues | Fix #1721 | License | MIT I'm adding a dedicated functional test because aliases will require specific attention/code regarding automatic class generation. Update: other CS fails fixed in #1725 Commits ------- d8589c0 [Icons] Fix Icon aliases not found
2 parents 7b9773f + d8589c0 commit 14d7918

File tree

3 files changed

+51
-2
lines changed

3 files changed

+51
-2
lines changed

src/Icons/src/Iconify.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,19 @@ public function fetchIcon(string $prefix, string $name): Icon
6161
throw new IconNotFoundException(sprintf('The icon "%s:%s" does not exist on iconify.design.', $prefix, $name));
6262
}
6363

64+
$nameArg = $name;
65+
if (isset($data['aliases'][$name])) {
66+
$name = $data['aliases'][$name]['parent'];
67+
}
68+
6469
if (!isset($data['icons'][$name]['body'])) {
65-
throw new IconNotFoundException(sprintf('The icon "%s:%s" does not exist on iconify.design.', $prefix, $name));
70+
throw new IconNotFoundException(sprintf('The icon "%s:%s" does not exist on iconify.design.', $prefix, $nameArg));
6671
}
6772

6873
$height = $data['icons'][$name]['height'] ?? $data['height'] ?? $this->sets()[$prefix]['height'] ?? null;
6974
$width = $data['icons'][$name]['width'] ?? $data['width'] ?? $this->sets()[$prefix]['width'] ?? null;
7075
if (null === $width && null === $height) {
71-
throw new \RuntimeException(sprintf('The icon "%s:%s" does not have a width or height.', $prefix, $name));
76+
throw new \RuntimeException(sprintf('The icon "%s:%s" does not have a width or height.', $prefix, $nameArg));
7277
}
7378

7479
return new Icon($data['icons'][$name]['body'], [

src/Icons/tests/Integration/RenderIconsInTwigTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,17 @@ public function testRenderIcons(): void
3838
trim($output)
3939
);
4040
}
41+
42+
public function testRenderAliasIcons(): void
43+
{
44+
$templateIcon = '<twig:ux:icon name="flowbite:close-outline" />';
45+
$outputIcon = self::getContainer()->get(Environment::class)->createTemplate($templateIcon)->render();
46+
47+
$templateAlias = '<twig:ux:icon name="flowbite:x-outline" />';
48+
$outputAlias = self::getContainer()->get(Environment::class)->createTemplate($templateAlias)->render();
49+
50+
$expected = '<svg viewBox="0 0 24 24" fill="currentColor" aria-hidden="true"><path fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L17.94 6M18 18L6.06 6"/></svg>';
51+
$this->assertSame($outputIcon, $expected);
52+
$this->assertSame($outputIcon, $outputAlias);
53+
}
4154
}

src/Icons/tests/Unit/IconifyTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,37 @@ public function testFetchIcon(): void
5050
$this->assertEquals($icon->getAttributes(), ['viewBox' => '0 0 24 24']);
5151
}
5252

53+
public function testFetchIconByAlias(): void
54+
{
55+
$iconify = new Iconify(
56+
cache: new NullAdapter(),
57+
endpoint: 'https://example.com',
58+
http: new MockHttpClient([
59+
new JsonMockResponse([
60+
'bi' => [],
61+
]),
62+
new JsonMockResponse([
63+
'aliases' => [
64+
'foo' => [
65+
'parent' => 'bar',
66+
],
67+
],
68+
'icons' => [
69+
'bar' => [
70+
'body' => '<path d="M0 0h24v24H0z" fill="none"/>',
71+
'height' => 24,
72+
],
73+
],
74+
]),
75+
]),
76+
);
77+
78+
$icon = $iconify->fetchIcon('bi', 'foo');
79+
80+
$this->assertEquals($icon->getInnerSvg(), '<path d="M0 0h24v24H0z" fill="none"/>');
81+
$this->assertEquals($icon->getAttributes(), ['viewBox' => '0 0 24 24']);
82+
}
83+
5384
public function testFetchIconThrowsWhenIconSetDoesNotExists(): void
5485
{
5586
$iconify = new Iconify(new NullAdapter(), 'https://example.com', new MockHttpClient(new JsonMockResponse([])));

0 commit comments

Comments
 (0)