Skip to content

Commit 22cfef0

Browse files
smnandrejaviereguiluz
authored andcommitted
[Icons] Expose IconRendererInterface
1 parent 2c360c6 commit 22cfef0

File tree

5 files changed

+81
-5
lines changed

5 files changed

+81
-5
lines changed

src/Icons/config/services.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@
5050
])
5151
->tag('twig.runtime')
5252

53+
->alias('Symfony\UX\Icons\IconRendererInterface', '.ux_icons.icon_renderer')
54+
5355
->set('.ux_icons.icon_finder', IconFinder::class)
5456
->args([
5557
service('twig'),

src/Icons/src/IconRenderer.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*
1717
* @internal
1818
*/
19-
final class IconRenderer
19+
final class IconRenderer implements IconRendererInterface
2020
{
2121
public function __construct(
2222
private readonly IconRegistryInterface $registry,
@@ -32,8 +32,6 @@ public function __construct(
3232
*
3333
* Precedence order:
3434
* Icon file < Renderer configuration < Renderer invocation
35-
*
36-
* @param array<string,string|bool> $attributes
3735
*/
3836
public function renderIcon(string $name, array $attributes = []): string
3937
{
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\UX\Icons;
13+
14+
use Symfony\UX\Icons\Exception\IconNotFoundException;
15+
16+
/**
17+
* @author Simon André <smn.andre@gmail.com>
18+
* @author Kevin Bond <kevinbond@gmail.com>
19+
*/
20+
interface IconRendererInterface
21+
{
22+
/**
23+
* Renders an icon by its name and returns the SVG string.
24+
*
25+
* @param string $name the icon name, optionally prefixed with the icon set
26+
* @param array<string, string|bool> $attributes an array of HTML attributes
27+
*
28+
* @throws IconNotFoundException
29+
*
30+
* @example
31+
* $iconRenderer->renderIcon('arrow-right');
32+
* // Renders the "arrow-right" icon from the default icons directory.
33+
*
34+
* $iconRenderer->renderIcon('lucide:heart', ['class' => 'color-red']);
35+
* // Renders the "heart" icon from the "lucide" icon set, with the "color-red" class.
36+
*/
37+
public function renderIcon(string $name, array $attributes = []): string;
38+
}

src/Icons/src/Twig/UXIconComponentListener.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
namespace Symfony\UX\Icons\Twig;
1313

14-
use Symfony\UX\Icons\IconRenderer;
14+
use Symfony\UX\Icons\IconRendererInterface;
1515
use Symfony\UX\TwigComponent\Event\PreCreateForRenderEvent;
1616

1717
/**
@@ -22,7 +22,7 @@
2222
final class UXIconComponentListener
2323
{
2424
public function __construct(
25-
private IconRenderer $iconRenderer,
25+
private IconRendererInterface $iconRenderer,
2626
) {
2727
}
2828

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\UX\Icons\Tests\Integration;
13+
14+
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
15+
use Symfony\UX\Icons\IconRenderer;
16+
use Symfony\UX\Icons\IconRendererInterface;
17+
18+
/**
19+
* @author Simon André <smn.andre@gmail.com>
20+
*/
21+
final class IconRendererTest extends KernelTestCase
22+
{
23+
public function testIconRenderService(): void
24+
{
25+
$this->assertTrue(self::getContainer()->has(IconRendererInterface::class));
26+
}
27+
28+
public function testIconRendererAlias(): void
29+
{
30+
$renderer = self::getContainer()->get(IconRendererInterface::class);
31+
$this->assertInstanceOf(IconRenderer::class, $renderer);
32+
}
33+
34+
public function testIconRendererIsPrivate(): void
35+
{
36+
$this->assertFalse(self::getContainer()->has(IconRenderer::class));
37+
}
38+
}

0 commit comments

Comments
 (0)