diff --git a/composer.json b/composer.json index 2802a450d..f33756238 100644 --- a/composer.json +++ b/composer.json @@ -36,7 +36,8 @@ }, "autoload": { "files": [ - "src/Resources/debug.php" + "src/Resources/debug.php", + "src/Resources/string_loader.php" ], "psr-4" : { "Twig\\" : "src/" diff --git a/src/Extension/StringLoaderExtension.php b/src/Extension/StringLoaderExtension.php index 7b4514710..fde395dd8 100644 --- a/src/Extension/StringLoaderExtension.php +++ b/src/Extension/StringLoaderExtension.php @@ -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); + } } diff --git a/src/Resources/string_loader.php b/src/Resources/string_loader.php new file mode 100644 index 000000000..b074495ca --- /dev/null +++ b/src/Resources/string_loader.php @@ -0,0 +1,25 @@ + + * + * 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); +} diff --git a/tests/Extension/LegacyStringLoaderFunctionsTest.php b/tests/Extension/LegacyStringLoaderFunctionsTest.php new file mode 100644 index 000000000..a6cb31df0 --- /dev/null +++ b/tests/Extension/LegacyStringLoaderFunctionsTest.php @@ -0,0 +1,30 @@ +assertSame(StringLoaderExtension::templateFromString($env, 'Foo')->render(), twig_template_from_string($env, 'Foo')->render()); + } +} diff --git a/tests/Extension/StringLoaderExtensionTest.php b/tests/Extension/StringLoaderExtensionTest.php index 363a0825e..e4fa2e3ba 100644 --- a/tests/Extension/StringLoaderExtensionTest.php +++ b/tests/Extension/StringLoaderExtensionTest.php @@ -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'))); } }