mirror of
https://github.com/twigphp/Twig.git
synced 2026-09-18 21:37:29 +00:00
Move functions for StringLoaderExtension
This commit is contained in:
+2
-1
@@ -36,7 +36,8 @@
|
|||||||
},
|
},
|
||||||
"autoload": {
|
"autoload": {
|
||||||
"files": [
|
"files": [
|
||||||
"src/Resources/debug.php"
|
"src/Resources/debug.php",
|
||||||
|
"src/Resources/string_loader.php"
|
||||||
],
|
],
|
||||||
"psr-4" : {
|
"psr-4" : {
|
||||||
"Twig\\" : "src/"
|
"Twig\\" : "src/"
|
||||||
|
|||||||
@@ -9,7 +9,10 @@
|
|||||||
* file that was distributed with this source code.
|
* file that was distributed with this source code.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
namespace Twig\Extension {
|
namespace Twig\Extension;
|
||||||
|
|
||||||
|
use Twig\Environment;
|
||||||
|
use Twig\TemplateWrapper;
|
||||||
use Twig\TwigFunction;
|
use Twig\TwigFunction;
|
||||||
|
|
||||||
final class StringLoaderExtension extends AbstractExtension
|
final class StringLoaderExtension extends AbstractExtension
|
||||||
@@ -17,26 +20,22 @@ final class StringLoaderExtension extends AbstractExtension
|
|||||||
public function getFunctions(): array
|
public function getFunctions(): array
|
||||||
{
|
{
|
||||||
return [
|
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;
|
* Loads a template from a string.
|
||||||
use Twig\TemplateWrapper;
|
*
|
||||||
|
* {{ include(template_from_string("Hello {{ name }}")) }}
|
||||||
/**
|
*
|
||||||
* Loads a template from a string.
|
* @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
|
||||||
* {{ include(template_from_string("Hello {{ name }}")) }}
|
*
|
||||||
*
|
* @internal
|
||||||
* @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
|
public static function templateFromString(Environment $env, $template, string $name = null): TemplateWrapper
|
||||||
*/
|
{
|
||||||
function twig_template_from_string(Environment $env, $template, string $name = null): TemplateWrapper
|
return $env->createTemplate((string) $template, $name);
|
||||||
{
|
}
|
||||||
return $env->createTemplate((string) $template, $name);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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 = new Environment($this->createMock('\Twig\Loader\LoaderInterface'));
|
||||||
$twig->addExtension(new StringLoaderExtension());
|
$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')));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user