forked from symfony/ux
-
Notifications
You must be signed in to change notification settings - Fork 0
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
Allow to silence error during rendering when an icon is not found
- Loading branch information
Showing
7 changed files
with
127 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?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 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, | ||
) { | ||
} | ||
|
||
/** | ||
* @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) { | ||
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,37 @@ | ||
<?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 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 testRenderIconIgnoreMissing(): void | ||
{ | ||
$renderer = $this->createMock(IconRendererInterface::class); | ||
$renderer->method('renderIcon') | ||
->willThrowException(new IconNotFoundException('not_found')); | ||
|
||
$runtime = new UXIconRuntime($renderer, true); | ||
$this->assertEquals('', $runtime->renderIcon('not_found')); | ||
|
||
$runtime = new UXIconRuntime($renderer, false); | ||
$this->expectException(IconNotFoundException::class); | ||
$runtime->renderIcon('not_found'); | ||
} | ||
} |