diff --git a/CHANGELOG b/CHANGELOG index a0b9de2b713..c6b35ba5b56 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,7 @@ -# 3.7.2 (2023-XX-XX) +# 3.8.0 (2023-XX-XX) - * n/a + * Deprecate all internal extension functions in favor of methods on the extension classes + * Mark all extension functions as @internal # 3.7.1 (2023-08-28) diff --git a/composer.json b/composer.json index 04dc93ffccf..7d35967b2b2 100644 --- a/composer.json +++ b/composer.json @@ -25,6 +25,7 @@ ], "require": { "php": ">=7.2.5", + "symfony/deprecation-contracts": "^2.5|^3", "symfony/polyfill-mbstring": "^1.3", "symfony/polyfill-ctype": "^1.8" }, diff --git a/extra/html-extra/HtmlExtension.php b/extra/html-extra/HtmlExtension.php index ed740b47187..9704ebb6efb 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 @@ public function getFilters(): array public function getFunctions(): array { return [ - new TwigFunction('html_classes', 'twig_html_classes'), + new TwigFunction('html_classes', [get_class($this), 'htmlClasses']), ]; } @@ -45,6 +47,8 @@ public function getFunctions(): array * 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 @@ public function dataUri(string $data, string $mime = null, array $parameters = [ 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))); - } - if (!$condition) { - continue; + /** + * @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; } - $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 00000000000..619789b6378 --- /dev/null +++ b/extra/html-extra/Resources/functions.php @@ -0,0 +1,22 @@ +=7.1.3", + "symfony/deprecation-contracts": "^2.5|^3", "symfony/mime": "^5.4|^6.0|^7.0", "twig/twig": "^2.7|^3.0" }, @@ -23,6 +24,7 @@ "symfony/phpunit-bridge": "^5.4|^6.3|^7.0" }, "autoload": { + "files": [ "Resources/functions.php" ], "psr-4" : { "Twig\\Extra\\Html\\" : "" }, "exclude-from-classmap": [ "/Tests/"