Skip to content

[Icons] Expose IconRendererInterface #1889

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

Merged
merged 1 commit into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions src/Icons/config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@
])
->tag('twig.runtime')

->alias('Symfony\UX\Icons\IconRendererInterface', '.ux_icons.icon_renderer')

->set('.ux_icons.icon_finder', IconFinder::class)
->args([
service('twig'),
Expand Down
4 changes: 1 addition & 3 deletions src/Icons/src/IconRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*
* @internal
*/
final class IconRenderer
final class IconRenderer implements IconRendererInterface
{
public function __construct(
private readonly IconRegistryInterface $registry,
Expand All @@ -32,8 +32,6 @@ public function __construct(
*
* Precedence order:
* Icon file < Renderer configuration < Renderer invocation
*
* @param array<string,string|bool> $attributes
*/
public function renderIcon(string $name, array $attributes = []): string
{
Expand Down
38 changes: 38 additions & 0 deletions src/Icons/src/IconRendererInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\UX\Icons;

use Symfony\UX\Icons\Exception\IconNotFoundException;

/**
* @author Simon André <smn.andre@gmail.com>
* @author Kevin Bond <kevinbond@gmail.com>
*/
interface IconRendererInterface
{
/**
* Renders an icon by its name and returns the SVG string.
*
* @param string $name the icon name, optionally prefixed with the icon set
* @param array<string, string|bool> $attributes an array of HTML attributes
*
* @throws IconNotFoundException
*
* @example
* $iconRenderer->renderIcon('arrow-right');
* // Renders the "arrow-right" icon from the default icons directory.
*
* $iconRenderer->renderIcon('lucide:heart', ['class' => 'color-red']);
* // Renders the "heart" icon from the "lucide" icon set, with the "color-red" class.
*/
public function renderIcon(string $name, array $attributes = []): string;
}
4 changes: 2 additions & 2 deletions src/Icons/src/Twig/UXIconComponentListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace Symfony\UX\Icons\Twig;

use Symfony\UX\Icons\IconRenderer;
use Symfony\UX\Icons\IconRendererInterface;
use Symfony\UX\TwigComponent\Event\PreCreateForRenderEvent;

/**
Expand All @@ -22,7 +22,7 @@
final class UXIconComponentListener
{
public function __construct(
private IconRenderer $iconRenderer,
private IconRendererInterface $iconRenderer,
) {
}

Expand Down
38 changes: 38 additions & 0 deletions src/Icons/tests/Integration/IconRendererTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\UX\Icons\Tests\Integration;

use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\UX\Icons\IconRenderer;
use Symfony\UX\Icons\IconRendererInterface;

/**
* @author Simon André <smn.andre@gmail.com>
*/
final class IconRendererTest extends KernelTestCase
{
public function testIconRenderService(): void
{
$this->assertTrue(self::getContainer()->has(IconRendererInterface::class));
}

public function testIconRendererAlias(): void
{
$renderer = self::getContainer()->get(IconRendererInterface::class);
$this->assertInstanceOf(IconRenderer::class, $renderer);
}

public function testIconRendererIsPrivate(): void
{
$this->assertFalse(self::getContainer()->has(IconRenderer::class));
}
}