-
-
Notifications
You must be signed in to change notification settings - Fork 364
[Icons] Add ignore_not_found
config option
#2023
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# CHANGELOG | ||
|
||
## 2.19.0 | ||
|
||
- Add `ignore_not_found` option to silence error during rendering if the | ||
icon is not found. | ||
|
||
## 2.17.0 | ||
|
||
- Add component |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\UX\Icons\Twig; | ||
|
||
use Psr\Log\LoggerInterface; | ||
use Symfony\UX\Icons\Exception\IconNotFoundException; | ||
use Symfony\UX\Icons\IconRendererInterface; | ||
use Twig\Extension\RuntimeExtensionInterface; | ||
|
||
/** | ||
* @author Simon André <[email protected]> | ||
* | ||
* @internal | ||
*/ | ||
final class UXIconRuntime implements RuntimeExtensionInterface | ||
{ | ||
public function __construct( | ||
private readonly IconRendererInterface $iconRenderer, | ||
private readonly bool $ignoreNotFound = false, | ||
private readonly ?LoggerInterface $logger = null, | ||
) { | ||
} | ||
|
||
/** | ||
* @param array<string, bool|string> $attributes | ||
*/ | ||
public function renderIcon(string $name, array $attributes = []): string | ||
{ | ||
try { | ||
return $this->iconRenderer->renderIcon($name, $attributes); | ||
} catch (IconNotFoundException $e) { | ||
if ($this->ignoreNotFound) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because it's a Twig rendering context, I'm wondering if we should use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm thinking this also, at least until we're asked for a separate config option. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If by "using Environment::isStrictVariables()" you mean "do not throw if strictVariable = false", this would be a BC break, no ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not throwing the exception in the prod env doesn't seem like a critical behavior change to me. In fact, that's exactly what the reporter is requesting (aligned with how Twig behaves). As suggested #2023 (comment) we could log the error instead. The configs ( |
||
$this->logger?->warning($e->getMessage()); | ||
return ''; | ||
smnandre marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
throw $e; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\UX\Icons\Tests\Unit\Twig; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Psr\Log\LoggerInterface; | ||
use Symfony\UX\Icons\Exception\IconNotFoundException; | ||
use Symfony\UX\Icons\IconRendererInterface; | ||
use Symfony\UX\Icons\Twig\UXIconRuntime; | ||
|
||
/** | ||
* @author Simon André <[email protected]> | ||
*/ | ||
class UXIconRuntimeTest extends TestCase | ||
{ | ||
public function testRenderIconIgnoreNotFound(): void | ||
{ | ||
$renderer = $this->createMock(IconRendererInterface::class); | ||
$renderer->method('renderIcon') | ||
->willThrowException(new IconNotFoundException('Icon "foo" not found.')); | ||
|
||
$logger = $this->createMock(LoggerInterface::class); | ||
$logger->expects($this->once()) | ||
->method('warning') | ||
->with('Icon "foo" not found.'); | ||
|
||
$runtime = new UXIconRuntime($renderer, true, $logger); | ||
$this->assertEquals('', $runtime->renderIcon('not_found')); | ||
|
||
$runtime = new UXIconRuntime($renderer, false); | ||
$this->expectException(IconNotFoundException::class); | ||
$runtime->renderIcon('not_found'); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It could default to
%kernel.debug%
here or in the recipeThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the idea in #2008 was to default to "not kernel.debug".
I this change anything in prod, in would be a BC break, so maybe the recipe is a good solution, indeed