Skip to content

Commit

Permalink
Deprecate internal extension functions in favor of methods on the ext…
Browse files Browse the repository at this point in the history
…ension classes
  • Loading branch information
fabpot committed Oct 8, 2023
1 parent 2d37866 commit c1ac449
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 28 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -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)

Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
54 changes: 28 additions & 26 deletions extra/html-extra/HtmlExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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']),
];
}

Expand All @@ -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
{
Expand Down Expand Up @@ -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));
}
}
22 changes: 22 additions & 0 deletions extra/html-extra/Resources/functions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

/*
* This file is part of Twig.
*
* (c) Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

use Twig\Extra\Html\HtmlExtension;

/**
* @internal
*/
function twig_html_classes(...$args): string
{
trigger_deprecation('twig/html-extra', '3.8.0', 'Using the internal "%s" function is deprecated.', __FUNCTION__);

return HtmlExtension::htmlClasses(...$args);
}
2 changes: 2 additions & 0 deletions extra/html-extra/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@
],
"require": {
"php": ">=7.1.3",
"symfony/deprecation-contracts": "^2.5|^3",
"symfony/mime": "^5.4|^6.0|^7.0",
"twig/twig": "^2.7|^3.0"
},
"require-dev": {
"symfony/phpunit-bridge": "^5.4|^6.3|^7.0"
},
"autoload": {
"files": [ "Resources/functions.php" ],
"psr-4" : { "Twig\\Extra\\Html\\" : "" },
"exclude-from-classmap": [
"/Tests/"
Expand Down

0 comments on commit c1ac449

Please sign in to comment.