Skip to content

Commit 8109a58

Browse files
committed
Log warning if icon is not found
1 parent eb6c4da commit 8109a58

File tree

4 files changed

+15
-4
lines changed

4 files changed

+15
-4
lines changed

src/Icons/composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@
4545
"symfony/http-client": "6.4|^7.0",
4646
"symfony/phpunit-bridge": "^6.3|^7.0",
4747
"symfony/ux-twig-component": "^2.14",
48-
"zenstruck/console-test": "^1.5"
48+
"zenstruck/console-test": "^1.5",
49+
"psr/log": "^2|^3"
4950
},
5051
"config": {
5152
"sort-packages": true

src/Icons/config/services.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
->args([
5050
service('.ux_icons.icon_renderer'),
5151
abstract_arg('ignore_not_found'),
52+
service('logger')->ignoreOnInvalid(),
5253
])
5354
->tag('twig.runtime')
5455

src/Icons/src/Twig/UXIconRuntime.php

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

1212
namespace Symfony\UX\Icons\Twig;
1313

14+
use Psr\Log\LoggerInterface;
1415
use Symfony\UX\Icons\Exception\IconNotFoundException;
1516
use Symfony\UX\Icons\IconRendererInterface;
1617
use Twig\Extension\RuntimeExtensionInterface;
@@ -25,6 +26,7 @@ final class UXIconRuntime implements RuntimeExtensionInterface
2526
public function __construct(
2627
private readonly IconRendererInterface $iconRenderer,
2728
private readonly bool $ignoreNotFound = false,
29+
private readonly ?LoggerInterface $logger = null,
2830
) {
2931
}
3032

@@ -37,6 +39,7 @@ public function renderIcon(string $name, array $attributes = []): string
3739
return $this->iconRenderer->renderIcon($name, $attributes);
3840
} catch (IconNotFoundException $e) {
3941
if ($this->ignoreNotFound) {
42+
$this->logger?->warning($e->getMessage());
4043
return '';
4144
}
4245

src/Icons/tests/Unit/Twig/UXIconRuntimeTest.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\UX\Icons\Tests\Unit\Twig;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Psr\Log\LoggerInterface;
1516
use Symfony\UX\Icons\Exception\IconNotFoundException;
1617
use Symfony\UX\Icons\IconRendererInterface;
1718
use Symfony\UX\Icons\Twig\UXIconRuntime;
@@ -21,13 +22,18 @@
2122
*/
2223
class UXIconRuntimeTest extends TestCase
2324
{
24-
public function testRenderIconIgnoreMissing(): void
25+
public function testRenderIconIgnoreNotFound(): void
2526
{
2627
$renderer = $this->createMock(IconRendererInterface::class);
2728
$renderer->method('renderIcon')
28-
->willThrowException(new IconNotFoundException('not_found'));
29+
->willThrowException(new IconNotFoundException('Icon "foo" not found.'));
2930

30-
$runtime = new UXIconRuntime($renderer, true);
31+
$logger = $this->createMock(LoggerInterface::class);
32+
$logger->expects($this->once())
33+
->method('warning')
34+
->with('Icon "foo" not found.');
35+
36+
$runtime = new UXIconRuntime($renderer, true, $logger);
3137
$this->assertEquals('', $runtime->renderIcon('not_found'));
3238

3339
$runtime = new UXIconRuntime($renderer, false);

0 commit comments

Comments
 (0)