From d53fef09a80122f616f0befefb701cc497c51f93 Mon Sep 17 00:00:00 2001 From: "Angelo D. Moura" Date: Wed, 2 Dec 2020 14:44:04 +0000 Subject: [PATCH] [TWIG] Moves the SVG custom function to an extension and change the test regex --- src/Twig/IconsExtension.php | 78 ------------------- ...onsExtensionTest.php => ExtensionTest.php} | 6 +- 2 files changed, 3 insertions(+), 81 deletions(-) delete mode 100644 src/Twig/IconsExtension.php rename tests/Twig/{IconsExtensionTest.php => ExtensionTest.php} (92%) diff --git a/src/Twig/IconsExtension.php b/src/Twig/IconsExtension.php deleted file mode 100644 index 8e0178595a..0000000000 --- a/src/Twig/IconsExtension.php +++ /dev/null @@ -1,78 +0,0 @@ -. -// }}} - -/** - * GNU social Twig extensions - * - * @package GNUsocial - * @category Twig - * - * @author Ângelo D. Moura - * @copyright 2020 Free Software Foundation, Inc http://www.fsf.org - * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later - */ - -namespace App\Twig; - -use Twig\Environment; -use Twig\Error\LoaderError; -use Twig\Error\RuntimeError; -use Twig\Error\SyntaxError; -use Twig\Extension\AbstractExtension; -use Twig\TwigFunction; - -class IconsExtension extends AbstractExtension -{ - public function getFunctions() - { - return [ - new TwigFunction('icon', - [$this, 'embedSvgIcon'], - ['needs_environment' => true] - ), - ]; - } - - /** - * Renders the Svg Icon template and returns it. - * - * @param Environment $twig - * @param string $icon_name - * @param string $icon_css_class - * - * @return string - * - * @author Ângelo D. Moura - */ - public function embedSvgIcon(Environment $twig, string $icon_name = '', string $icon_css_class = '') - { - try { - return $twig->render('@public_path/assets/icons/' . $icon_name . '.svg.twig', ['iconClass' => $icon_css_class]); - } catch (LoaderError $e) { - //return an empty string (a missing icon is not that important of an error) - return ''; - } catch (RuntimeError $e) { - //return an empty string (a missing icon is not that important of an error) - return ''; - } catch (SyntaxError $e) { - //return an empty string (a missing icon is not that important of an error) - return ''; - } - } -} \ No newline at end of file diff --git a/tests/Twig/IconsExtensionTest.php b/tests/Twig/ExtensionTest.php similarity index 92% rename from tests/Twig/IconsExtensionTest.php rename to tests/Twig/ExtensionTest.php index 10d31a886b..f402340fee 100644 --- a/tests/Twig/IconsExtensionTest.php +++ b/tests/Twig/ExtensionTest.php @@ -36,7 +36,7 @@ use App\Twig\Runtime; use DirectoryIterator; use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; -class IconsExtensionTest extends KernelTestCase +class ExtensionTest extends KernelTestCase { public function testIconsExtension() { @@ -51,7 +51,7 @@ class IconsExtensionTest extends KernelTestCase //Check if every icon file as a ".svg.twig" extension foreach ($icon_file_names as $icon_file_name) { - static::assertRegExp('#([a-zA-Z0-9\s_\\.\-\(\):])+(.svg.twig)$#', $icon_file_name); + static::assertRegExp('/.svg.twig/', $icon_file_name); } //Check if the function gives a valid HTML with a class attribute equal to the one passed @@ -63,7 +63,7 @@ class IconsExtensionTest extends KernelTestCase $icon_template_render = $twig->render('@public_path/assets/icons/' . $icon_file_name, ['iconClass' => 'icon icon-' . $icon_name]); - $icons_extension = new IconsExtension(); + $icons_extension = new Extension(); $icon_extension_render = $icons_extension->embedSvgIcon($twig, $icon_name, 'icon icon-' . $icon_name); static::assertSame($icon_template_render, $icon_extension_render);