Move functions for StringLoaderExtension

This commit is contained in:
Fabien Potencier
2023-12-10 11:38:36 +01:00
parent d4d016034c
commit 196e91dd72
5 changed files with 77 additions and 22 deletions
+2 -1
View File
@@ -36,7 +36,8 @@
},
"autoload": {
"files": [
"src/Resources/debug.php"
"src/Resources/debug.php",
"src/Resources/string_loader.php"
],
"psr-4" : {
"Twig\\" : "src/"
+19 -20
View File
@@ -9,7 +9,10 @@
* file that was distributed with this source code.
*/
namespace Twig\Extension {
namespace Twig\Extension;
use Twig\Environment;
use Twig\TemplateWrapper;
use Twig\TwigFunction;
final class StringLoaderExtension extends AbstractExtension
@@ -17,26 +20,22 @@ final class StringLoaderExtension extends AbstractExtension
public function getFunctions(): array
{
return [
new TwigFunction('template_from_string', 'twig_template_from_string', ['needs_environment' => true]),
new TwigFunction('template_from_string', [self::class, 'templateFromString'], ['needs_environment' => true]),
];
}
}
}
namespace {
use Twig\Environment;
use Twig\TemplateWrapper;
/**
* Loads a template from a string.
*
* {{ include(template_from_string("Hello {{ name }}")) }}
*
* @param string $template A template as a string or object implementing __toString()
* @param string $name An optional name of the template to be used in error messages
*/
function twig_template_from_string(Environment $env, $template, string $name = null): TemplateWrapper
{
return $env->createTemplate((string) $template, $name);
}
/**
* Loads a template from a string.
*
* {{ include(template_from_string("Hello {{ name }}")) }}
*
* @param string $template A template as a string or object implementing __toString()
* @param string $name An optional name of the template to be used in error messages
*
* @internal
*/
public static function templateFromString(Environment $env, $template, string $name = null): TemplateWrapper
{
return $env->createTemplate((string) $template, $name);
}
}
+25
View File
@@ -0,0 +1,25 @@
<?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.
*/
use Twig\Environment;
use Twig\Extension\StringLoaderExtension;
use Twig\TemplateWrapper;
/**
* @internal
* @deprecated since Twig 3.9.0
*/
function twig_template_from_string(Environment $env, $template, string $name = null): TemplateWrapper
{
trigger_deprecation('twig/twig', '3.9.0', 'Using the internal "%s" function is deprecated.', __FUNCTION__);
return StringLoaderExtension::templateFromString($env, $template, $name);
}
@@ -0,0 +1,30 @@
<?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\Tests\Extension;
use PHPUnit\Framework\TestCase;
use Twig\Environment;
use Twig\Extension\StringLoaderExtension;
use Twig\Loader\ArrayLoader;
/**
* @group legacy
*/
class LegacyStringLoaderFunctionsTest extends TestCase
{
public function testTemplateFromString()
{
$env = new Environment(new ArrayLoader());
$this->assertSame(StringLoaderExtension::templateFromString($env, 'Foo')->render(), twig_template_from_string($env, 'Foo')->render());
}
}
@@ -21,6 +21,6 @@ class StringLoaderExtensionTest extends TestCase
{
$twig = new Environment($this->createMock('\Twig\Loader\LoaderInterface'));
$twig->addExtension(new StringLoaderExtension());
$this->assertSame('something', twig_include($twig, [], twig_template_from_string($twig, 'something')));
$this->assertSame('something', twig_include($twig, [], StringLoaderExtension::templateFromString($twig, 'something')));
}
}