From 6a18cda5aaad6fff27ebe32c6f753358837a4faa Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Sat, 9 Dec 2023 18:04:05 +0100 Subject: [PATCH] Move functions for HtmlExtension --- extra/html-extra/HtmlExtension.php | 54 ++++++++++--------- extra/html-extra/Resources/functions.php | 23 ++++++++ .../html-extra/Tests/LegacyFunctionsTest.php | 26 +++++++++ extra/html-extra/composer.json | 2 + 4 files changed, 79 insertions(+), 26 deletions(-) create mode 100644 extra/html-extra/Resources/functions.php create mode 100644 extra/html-extra/Tests/LegacyFunctionsTest.php diff --git a/extra/html-extra/HtmlExtension.php b/extra/html-extra/HtmlExtension.php index ed740b471..d5842bf50 100644 --- a/extra/html-extra/HtmlExtension.php +++ b/extra/html-extra/HtmlExtension.php @@ -9,8 +9,10 @@ * file that was distributed with this source code. */ -namespace Twig\Extra\Html { +namespace Twig\Extra\Html; + use Symfony\Component\Mime\MimeTypes; +use Twig\Error\RuntimeError; use Twig\Extension\AbstractExtension; use Twig\TwigFilter; use Twig\TwigFunction; @@ -34,7 +36,7 @@ final class HtmlExtension extends AbstractExtension public function getFunctions(): array { return [ - new TwigFunction('html_classes', 'twig_html_classes'), + new TwigFunction('html_classes', [self::class, 'htmlClasses']), ]; } @@ -45,6 +47,8 @@ final class HtmlExtension extends AbstractExtension * be done before calling this filter. * * @return string The generated data URI + * + * @internal */ public function dataUri(string $data, string $mime = null, array $parameters = []): string { @@ -79,33 +83,31 @@ final class HtmlExtension extends AbstractExtension return $repr; } -} -} -namespace { -use Twig\Error\RuntimeError; - -function twig_html_classes(...$args): string -{ - $classes = []; - foreach ($args as $i => $arg) { - if (\is_string($arg)) { - $classes[] = $arg; - } elseif (\is_array($arg)) { - foreach ($arg as $class => $condition) { - if (!\is_string($class)) { - throw new RuntimeError(sprintf('The html_classes function argument %d (key %d) should be a string, got "%s".', $i, $class, \gettype($class))); + /** + * @internal + */ + public static function htmlClasses(...$args): string + { + $classes = []; + foreach ($args as $i => $arg) { + if (\is_string($arg)) { + $classes[] = $arg; + } elseif (\is_array($arg)) { + foreach ($arg as $class => $condition) { + if (!\is_string($class)) { + throw new RuntimeError(sprintf('The html_classes function argument %d (key %d) should be a string, got "%s".', $i, $class, \gettype($class))); + } + if (!$condition) { + continue; + } + $classes[] = $class; } - if (!$condition) { - continue; - } - $classes[] = $class; + } else { + throw new RuntimeError(sprintf('The html_classes function argument %d should be either a string or an array, got "%s".', $i, \gettype($arg))); } - } else { - throw new RuntimeError(sprintf('The html_classes function argument %d should be either a string or an array, got "%s".', $i, \gettype($arg))); } - } - return implode(' ', array_unique($classes)); -} + return implode(' ', array_unique($classes)); + } } diff --git a/extra/html-extra/Resources/functions.php b/extra/html-extra/Resources/functions.php new file mode 100644 index 000000000..4458f3bf8 --- /dev/null +++ b/extra/html-extra/Resources/functions.php @@ -0,0 +1,23 @@ +assertSame(HtmlExtension::htmlClasses(['charset' => 'utf-8']), \twig_html_classes(['charset' => 'utf-8'])); + } +} diff --git a/extra/html-extra/composer.json b/extra/html-extra/composer.json index e67e53e5a..ca7b2f62c 100644 --- a/extra/html-extra/composer.json +++ b/extra/html-extra/composer.json @@ -16,6 +16,7 @@ ], "require": { "php": ">=7.2.5", + "symfony/deprecation-contracts": "^2.5|^3", "symfony/mime": "^5.4|^6.0|^7.0", "twig/twig": "^3.0" }, @@ -23,6 +24,7 @@ "symfony/phpunit-bridge": "^6.4|^7.0" }, "autoload": { + "files": [ "Resources/functions.php" ], "psr-4" : { "Twig\\Extra\\Html\\" : "" }, "exclude-from-classmap": [ "/Tests/"