-
-
Notifications
You must be signed in to change notification settings - Fork 299
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Icons] Add
ignore_not_found
config option
- Loading branch information
Showing
8 changed files
with
139 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | ||
$this->logger?->warning($e->getMessage()); | ||
return ''; | ||
} | ||
|
||
throw $e; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} |