Move functions for CssInlinerExtension

This commit is contained in:
Fabien Potencier
2023-12-09 17:59:49 +01:00
parent 6a18cda5aa
commit bff189f19a
4 changed files with 64 additions and 9 deletions
+12 -9
View File
@@ -20,17 +20,20 @@ class CssInlinerExtension extends AbstractExtension
public function getFilters()
{
return [
new TwigFilter('inline_css', 'Twig\\Extra\\CssInliner\\twig_inline_css', ['is_safe' => ['all']]),
new TwigFilter('inline_css', [self::class, 'inlineCss'], ['is_safe' => ['all']]),
];
}
}
function twig_inline_css(string $body, string ...$css): string
{
static $inliner;
if (null === $inliner) {
$inliner = new CssToInlineStyles();
/**
* @internal
*/
public static function inlineCss(string $body, string ...$css): string
{
static $inliner;
if (null === $inliner) {
$inliner = new CssToInlineStyles();
}
return $inliner->convert($body, implode("\n", $css));
}
return $inliner->convert($body, implode("\n", $css));
}
@@ -0,0 +1,23 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Twig\Extra\CssInliner;
/**
* @internal
* @deprecated since Twig 3.9.0
*/
function twig_inline_css(string $body, string ...$css): string
{
trigger_deprecation('twig/cssinliner-extra', '3.9.0', 'Using the internal "%s" function is deprecated.', __FUNCTION__);
return CssInlinerExtension::inlineCss($body, ...$css);
}
@@ -0,0 +1,27 @@
<?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.
*/
namespace Twig\Extra\CssInliner\Tests;
use PHPUnit\Framework\TestCase;
use Twig\Extra\CssInliner\CssInlinerExtension;
use function Twig\Extra\CssInliner\twig_inline_css;
/**
* @group legacy
*/
class LegacyFunctionsTest extends TestCase
{
public function testInlineCss()
{
$this->assertSame(CssInlinerExtension::inlineCss('<p>body</p>', 'p { color: red }'), twig_inline_css('<p>body</p>', 'p { color: red }'));
}
}
+2
View File
@@ -16,6 +16,7 @@
],
"require": {
"php": ">=7.2.5",
"symfony/deprecation-contracts": "^2.5|^3",
"tijsverkoyen/css-to-inline-styles": "^2.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\\CssInliner\\" : "" },
"exclude-from-classmap": [
"/Tests/"