Skip to content

Commit bf0b82b

Browse files
committed
Move functions for HtmlExtension
1 parent c04603b commit bf0b82b

4 files changed

Lines changed: 79 additions & 26 deletions

File tree

HtmlExtension.php

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@
99
* file that was distributed with this source code.
1010
*/
1111

12-
namespace Twig\Extra\Html {
12+
namespace Twig\Extra\Html;
13+
1314
use Symfony\Component\Mime\MimeTypes;
15+
use Twig\Error\RuntimeError;
1416
use Twig\Extension\AbstractExtension;
1517
use Twig\TwigFilter;
1618
use Twig\TwigFunction;
@@ -34,7 +36,7 @@ public function getFilters(): array
3436
public function getFunctions(): array
3537
{
3638
return [
37-
new TwigFunction('html_classes', 'twig_html_classes'),
39+
new TwigFunction('html_classes', [self::class, 'htmlClasses']),
3840
];
3941
}
4042

@@ -45,6 +47,8 @@ public function getFunctions(): array
4547
* be done before calling this filter.
4648
*
4749
* @return string The generated data URI
50+
*
51+
* @internal
4852
*/
4953
public function dataUri(string $data, string $mime = null, array $parameters = []): string
5054
{
@@ -79,33 +83,31 @@ public function dataUri(string $data, string $mime = null, array $parameters = [
7983

8084
return $repr;
8185
}
82-
}
83-
}
84-
85-
namespace {
86-
use Twig\Error\RuntimeError;
8786

88-
function twig_html_classes(...$args): string
89-
{
90-
$classes = [];
91-
foreach ($args as $i => $arg) {
92-
if (\is_string($arg)) {
93-
$classes[] = $arg;
94-
} elseif (\is_array($arg)) {
95-
foreach ($arg as $class => $condition) {
96-
if (!\is_string($class)) {
97-
throw new RuntimeError(sprintf('The html_classes function argument %d (key %d) should be a string, got "%s".', $i, $class, \gettype($class)));
98-
}
99-
if (!$condition) {
100-
continue;
87+
/**
88+
* @internal
89+
*/
90+
public static function htmlClasses(...$args): string
91+
{
92+
$classes = [];
93+
foreach ($args as $i => $arg) {
94+
if (\is_string($arg)) {
95+
$classes[] = $arg;
96+
} elseif (\is_array($arg)) {
97+
foreach ($arg as $class => $condition) {
98+
if (!\is_string($class)) {
99+
throw new RuntimeError(sprintf('The html_classes function argument %d (key %d) should be a string, got "%s".', $i, $class, \gettype($class)));
100+
}
101+
if (!$condition) {
102+
continue;
103+
}
104+
$classes[] = $class;
101105
}
102-
$classes[] = $class;
106+
} else {
107+
throw new RuntimeError(sprintf('The html_classes function argument %d should be either a string or an array, got "%s".', $i, \gettype($arg)));
103108
}
104-
} else {
105-
throw new RuntimeError(sprintf('The html_classes function argument %d should be either a string or an array, got "%s".', $i, \gettype($arg)));
106109
}
107-
}
108110

109-
return implode(' ', array_unique($classes));
110-
}
111+
return implode(' ', array_unique($classes));
112+
}
111113
}

Resources/functions.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
/*
4+
* This file is part of Twig.
5+
*
6+
* (c) Fabien Potencier
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
use Twig\Extra\Html\HtmlExtension;
13+
14+
/**
15+
* @internal
16+
* @deprecated since Twig 3.9.0
17+
*/
18+
function twig_html_classes(...$args): string
19+
{
20+
trigger_deprecation('twig/html-extra', '3.9.0', 'Using the internal "%s" function is deprecated.', __FUNCTION__);
21+
22+
return HtmlExtension::htmlClasses(...$args);
23+
}

Tests/LegacyFunctionsTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
/*
4+
* This file is part of Twig.
5+
*
6+
* (c) Fabien Potencier
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Twig\Extra\Html\Tests;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Twig\Extra\Html\HtmlExtension;
16+
17+
/**
18+
* @group legacy
19+
*/
20+
class LegacyFunctionsTest extends TestCase
21+
{
22+
public function testHtmlToMarkdown()
23+
{
24+
$this->assertSame(HtmlExtension::htmlClasses(['charset' => 'utf-8']), \twig_html_classes(['charset' => 'utf-8']));
25+
}
26+
}

composer.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@
1616
],
1717
"require": {
1818
"php": ">=7.2.5",
19+
"symfony/deprecation-contracts": "^2.5|^3",
1920
"symfony/mime": "^5.4|^6.0|^7.0",
2021
"twig/twig": "^3.0"
2122
},
2223
"require-dev": {
2324
"symfony/phpunit-bridge": "^6.4|^7.0"
2425
},
2526
"autoload": {
27+
"files": [ "Resources/functions.php" ],
2628
"psr-4" : { "Twig\\Extra\\Html\\" : "" },
2729
"exclude-from-classmap": [
2830
"/Tests/"

0 commit comments

Comments
 (0)