diff --git a/CHANGELOG b/CHANGELOG index a01442025..7b59881cf 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,5 +1,6 @@ * 1.18.1 (2015-XX-XX) + * deprecated Twig_Loader_String * fixed the slice filter when used with a SimpleXMLElement object * fixed filesystem loader when trying to load non-files (like directories) diff --git a/doc/deprecated.rst b/doc/deprecated.rst index 30aaac1ba..bde62ba5f 100644 --- a/doc/deprecated.rst +++ b/doc/deprecated.rst @@ -101,6 +101,12 @@ Interfaces those constants Twig_Template::ANY_CALL, Twig_Template::ARRAY_CALL, Twig_Template::METHOD_CALL) +Loaders +------- + +* As of Twig 1.x, ``Twig_Loader_String`` is deprecated and will be removed in + 2.0. + Globals ------- diff --git a/lib/Twig/Loader/String.php b/lib/Twig/Loader/String.php index 2099c09c9..63d6890a3 100644 --- a/lib/Twig/Loader/String.php +++ b/lib/Twig/Loader/String.php @@ -19,6 +19,8 @@ * source code of the template). If you don't want to see your cache grows out of * control, you need to take care of clearing the old cache file by yourself. * + * @deprecated since 1.18.1 (to be removed in 2.0) + * * @author Fabien Potencier */ class Twig_Loader_String implements Twig_LoaderInterface, Twig_ExistsLoaderInterface diff --git a/test/Twig/Tests/EnvironmentTest.php b/test/Twig/Tests/EnvironmentTest.php index b791e57f5..ee127e01e 100644 --- a/test/Twig/Tests/EnvironmentTest.php +++ b/test/Twig/Tests/EnvironmentTest.php @@ -46,7 +46,7 @@ class Twig_Tests_EnvironmentTest extends PHPUnit_Framework_TestCase public function testGlobals() { // globals can be added after calling getGlobals - $twig = new Twig_Environment(new Twig_Loader_String()); + $twig = new Twig_Environment($this->getMock('Twig_LoaderInterface')); $twig->addGlobal('foo', 'foo'); $twig->getGlobals(); $twig->addGlobal('foo', 'bar'); @@ -54,7 +54,7 @@ class Twig_Tests_EnvironmentTest extends PHPUnit_Framework_TestCase $this->assertEquals('bar', $globals['foo']); // globals can be modified after runtime init - $twig = new Twig_Environment(new Twig_Loader_String()); + $twig = new Twig_Environment($this->getMock('Twig_LoaderInterface')); $twig->addGlobal('foo', 'foo'); $globals = $twig->getGlobals(); $twig->initRuntime(); @@ -63,7 +63,7 @@ class Twig_Tests_EnvironmentTest extends PHPUnit_Framework_TestCase $this->assertEquals('bar', $globals['foo']); // globals can be modified after extensions init - $twig = new Twig_Environment(new Twig_Loader_String()); + $twig = new Twig_Environment($this->getMock('Twig_LoaderInterface')); $twig->addGlobal('foo', 'foo'); $twig->getGlobals(); $twig->getFunctions(); @@ -72,7 +72,7 @@ class Twig_Tests_EnvironmentTest extends PHPUnit_Framework_TestCase $this->assertEquals('bar', $globals['foo']); // globals can be modified after extensions and runtime init - $twig = new Twig_Environment(new Twig_Loader_String()); + $twig = new Twig_Environment($loader = new Twig_Loader_Array(array('index' => '{{foo}}'))); $twig->addGlobal('foo', 'foo'); $twig->getGlobals(); $twig->getFunctions(); @@ -81,15 +81,15 @@ class Twig_Tests_EnvironmentTest extends PHPUnit_Framework_TestCase $globals = $twig->getGlobals(); $this->assertEquals('bar', $globals['foo']); - $twig = new Twig_Environment(new Twig_Loader_String()); + $twig = new Twig_Environment($loader); $twig->getGlobals(); $twig->addGlobal('foo', 'bar'); - $template = $twig->loadTemplate('{{foo}}'); + $template = $twig->loadTemplate('index'); $this->assertEquals('bar', $template->render(array())); /* to be uncomment in Twig 2.0 // globals cannot be added after runtime init - $twig = new Twig_Environment(new Twig_Loader_String()); + $twig = new Twig_Environment($this->getMock('Twig_LoaderInterface')); $twig->addGlobal('foo', 'foo'); $globals = $twig->getGlobals(); $twig->initRuntime(); @@ -101,7 +101,7 @@ class Twig_Tests_EnvironmentTest extends PHPUnit_Framework_TestCase } // globals cannot be added after extensions init - $twig = new Twig_Environment(new Twig_Loader_String()); + $twig = new Twig_Environment($this->getMock('Twig_LoaderInterface')); $twig->addGlobal('foo', 'foo'); $globals = $twig->getGlobals(); $twig->getFunctions(); @@ -113,7 +113,7 @@ class Twig_Tests_EnvironmentTest extends PHPUnit_Framework_TestCase } // globals cannot be added after extensions and runtime init - $twig = new Twig_Environment(new Twig_Loader_String()); + $twig = new Twig_Environment($this->getMock('Twig_LoaderInterface')); $twig->addGlobal('foo', 'foo'); $globals = $twig->getGlobals(); $twig->getFunctions(); @@ -126,7 +126,7 @@ class Twig_Tests_EnvironmentTest extends PHPUnit_Framework_TestCase } // test adding globals after initRuntime without call to getGlobals - $twig = new Twig_Environment(new Twig_Loader_String()); + $twig = new Twig_Environment($this->getMock('Twig_LoaderInterface')); $twig->initRuntime(); try { $twig->addGlobal('bar', 'bar'); @@ -142,17 +142,17 @@ class Twig_Tests_EnvironmentTest extends PHPUnit_Framework_TestCase $options = array('cache' => sys_get_temp_dir().'/twig', 'auto_reload' => false, 'debug' => false); // force compilation - $twig = new Twig_Environment(new Twig_Loader_String(), $options); - $cache = $twig->getCacheFilename('{{ foo }}'); + $twig = new Twig_Environment($loader = new Twig_Loader_Array(array('index' => '{{ foo }}')), $options); + $cache = $twig->getCacheFilename('index'); if (!is_dir(dirname($cache))) { mkdir(dirname($cache), 0777, true); } - file_put_contents($cache, $twig->compileSource('{{ foo }}', '{{ foo }}')); + file_put_contents($cache, $twig->compileSource('{{ foo }}', 'index')); // check that extensions won't be initialized when rendering a template that is already in the cache $twig = $this ->getMockBuilder('Twig_Environment') - ->setConstructorArgs(array(new Twig_Loader_String(), $options)) + ->setConstructorArgs(array($loader, $options)) ->setMethods(array('initExtensions')) ->getMock() ; @@ -160,7 +160,7 @@ class Twig_Tests_EnvironmentTest extends PHPUnit_Framework_TestCase $twig->expects($this->never())->method('initExtensions'); // render template - $output = $twig->render('{{ foo }}', array('foo' => 'bar')); + $output = $twig->render('index', array('foo' => 'bar')); $this->assertEquals('bar', $output); unlink($cache); @@ -168,7 +168,7 @@ class Twig_Tests_EnvironmentTest extends PHPUnit_Framework_TestCase public function testAddExtension() { - $twig = new Twig_Environment(new Twig_Loader_String()); + $twig = new Twig_Environment($this->getMock('Twig_LoaderInterface')); $twig->addExtension(new Twig_Tests_EnvironmentTest_Extension()); $this->assertArrayHasKey('test', $twig->getTags()); @@ -184,7 +184,7 @@ class Twig_Tests_EnvironmentTest extends PHPUnit_Framework_TestCase public function testRemoveExtension() { - $twig = new Twig_Environment(new Twig_Loader_String()); + $twig = new Twig_Environment($this->getMock('Twig_LoaderInterface')); $twig->addExtension(new Twig_Tests_EnvironmentTest_Extension()); $twig->removeExtension('environment_test'); diff --git a/test/Twig/Tests/ExpressionParserTest.php b/test/Twig/Tests/ExpressionParserTest.php index 983849ea1..ff263cf28 100644 --- a/test/Twig/Tests/ExpressionParserTest.php +++ b/test/Twig/Tests/ExpressionParserTest.php @@ -17,7 +17,7 @@ class Twig_Tests_ExpressionParserTest extends PHPUnit_Framework_TestCase */ public function testCanOnlyAssignToNames($template) { - $env = new Twig_Environment(new Twig_Loader_String(), array('cache' => false, 'autoescape' => false)); + $env = new Twig_Environment($this->getMock('Twig_LoaderInterface'), array('cache' => false, 'autoescape' => false)); $parser = new Twig_Parser($env); $parser->parse($env->tokenize($template, 'index')); @@ -41,7 +41,7 @@ class Twig_Tests_ExpressionParserTest extends PHPUnit_Framework_TestCase */ public function testArrayExpression($template, $expected) { - $env = new Twig_Environment(new Twig_Loader_String(), array('cache' => false, 'autoescape' => false)); + $env = new Twig_Environment($this->getMock('Twig_LoaderInterface'), array('cache' => false, 'autoescape' => false)); $stream = $env->tokenize($template, 'index'); $parser = new Twig_Parser($env); @@ -54,7 +54,7 @@ class Twig_Tests_ExpressionParserTest extends PHPUnit_Framework_TestCase */ public function testArraySyntaxError($template) { - $env = new Twig_Environment(new Twig_Loader_String(), array('cache' => false, 'autoescape' => false)); + $env = new Twig_Environment($this->getMock('Twig_LoaderInterface'), array('cache' => false, 'autoescape' => false)); $parser = new Twig_Parser($env); $parser->parse($env->tokenize($template, 'index')); @@ -149,7 +149,7 @@ class Twig_Tests_ExpressionParserTest extends PHPUnit_Framework_TestCase */ public function testStringExpressionDoesNotConcatenateTwoConsecutiveStrings() { - $env = new Twig_Environment(new Twig_Loader_String(), array('cache' => false, 'autoescape' => false, 'optimizations' => 0)); + $env = new Twig_Environment($this->getMock('Twig_LoaderInterface'), array('cache' => false, 'autoescape' => false, 'optimizations' => 0)); $stream = $env->tokenize('{{ "a" "b" }}', 'index'); $parser = new Twig_Parser($env); @@ -161,7 +161,7 @@ class Twig_Tests_ExpressionParserTest extends PHPUnit_Framework_TestCase */ public function testStringExpression($template, $expected) { - $env = new Twig_Environment(new Twig_Loader_String(), array('cache' => false, 'autoescape' => false, 'optimizations' => 0)); + $env = new Twig_Environment($this->getMock('Twig_LoaderInterface'), array('cache' => false, 'autoescape' => false, 'optimizations' => 0)); $stream = $env->tokenize($template, 'index'); $parser = new Twig_Parser($env); @@ -220,7 +220,7 @@ class Twig_Tests_ExpressionParserTest extends PHPUnit_Framework_TestCase */ public function testAttributeCallDoesNotSupportNamedArguments() { - $env = new Twig_Environment(new Twig_Loader_String(), array('cache' => false, 'autoescape' => false)); + $env = new Twig_Environment($this->getMock('Twig_LoaderInterface'), array('cache' => false, 'autoescape' => false)); $parser = new Twig_Parser($env); $parser->parse($env->tokenize('{{ foo.bar(name="Foo") }}', 'index')); @@ -231,7 +231,7 @@ class Twig_Tests_ExpressionParserTest extends PHPUnit_Framework_TestCase */ public function testMacroCallDoesNotSupportNamedArguments() { - $env = new Twig_Environment(new Twig_Loader_String(), array('cache' => false, 'autoescape' => false)); + $env = new Twig_Environment($this->getMock('Twig_LoaderInterface'), array('cache' => false, 'autoescape' => false)); $parser = new Twig_Parser($env); $parser->parse($env->tokenize('{% from _self import foo %}{% macro foo() %}{% endmacro %}{{ foo(name="Foo") }}', 'index')); @@ -243,7 +243,7 @@ class Twig_Tests_ExpressionParserTest extends PHPUnit_Framework_TestCase */ public function testMacroDefinitionDoesNotSupportNonNameVariableName() { - $env = new Twig_Environment(new Twig_Loader_String(), array('cache' => false, 'autoescape' => false)); + $env = new Twig_Environment($this->getMock('Twig_LoaderInterface'), array('cache' => false, 'autoescape' => false)); $parser = new Twig_Parser($env); $parser->parse($env->tokenize('{% macro foo("a") %}{% endmacro %}', 'index')); @@ -256,7 +256,7 @@ class Twig_Tests_ExpressionParserTest extends PHPUnit_Framework_TestCase */ public function testMacroDefinitionDoesNotSupportNonConstantDefaultValues($template) { - $env = new Twig_Environment(new Twig_Loader_String(), array('cache' => false, 'autoescape' => false)); + $env = new Twig_Environment($this->getMock('Twig_LoaderInterface'), array('cache' => false, 'autoescape' => false)); $parser = new Twig_Parser($env); $parser->parse($env->tokenize($template, 'index')); @@ -275,7 +275,7 @@ class Twig_Tests_ExpressionParserTest extends PHPUnit_Framework_TestCase */ public function testMacroDefinitionSupportsConstantDefaultValues($template) { - $env = new Twig_Environment(new Twig_Loader_String(), array('cache' => false, 'autoescape' => false)); + $env = new Twig_Environment($this->getMock('Twig_LoaderInterface'), array('cache' => false, 'autoescape' => false)); $parser = new Twig_Parser($env); $parser->parse($env->tokenize($template, 'index')); @@ -300,7 +300,7 @@ class Twig_Tests_ExpressionParserTest extends PHPUnit_Framework_TestCase */ public function testUnknownFunction() { - $env = new Twig_Environment(new Twig_Loader_String(), array('cache' => false, 'autoescape' => false)); + $env = new Twig_Environment($this->getMock('Twig_LoaderInterface'), array('cache' => false, 'autoescape' => false)); $parser = new Twig_Parser($env); $parser->parse($env->tokenize('{{ cycl() }}', 'index')); @@ -312,7 +312,7 @@ class Twig_Tests_ExpressionParserTest extends PHPUnit_Framework_TestCase */ public function testUnknownFilter() { - $env = new Twig_Environment(new Twig_Loader_String(), array('cache' => false, 'autoescape' => false)); + $env = new Twig_Environment($this->getMock('Twig_LoaderInterface'), array('cache' => false, 'autoescape' => false)); $parser = new Twig_Parser($env); $parser->parse($env->tokenize('{{ 1|lowe }}', 'index')); @@ -324,7 +324,7 @@ class Twig_Tests_ExpressionParserTest extends PHPUnit_Framework_TestCase */ public function testUnknownTest() { - $env = new Twig_Environment(new Twig_Loader_String(), array('cache' => false, 'autoescape' => false)); + $env = new Twig_Environment($this->getMock('Twig_LoaderInterface'), array('cache' => false, 'autoescape' => false)); $parser = new Twig_Parser($env); $parser->parse($env->tokenize('{{ 1 is nul }}', 'index')); diff --git a/test/Twig/Tests/FileCachingTest.php b/test/Twig/Tests/FileCachingTest.php index 3eaee593b..6f5bfee41 100644 --- a/test/Twig/Tests/FileCachingTest.php +++ b/test/Twig/Tests/FileCachingTest.php @@ -26,7 +26,7 @@ class Twig_Tests_FileCachingTest extends PHPUnit_Framework_TestCase $this->markTestSkipped(sprintf('Unable to run the tests as "%s" is not writable.', $this->tmpDir)); } - $this->env = new Twig_Environment(new Twig_Loader_String(), array('cache' => $this->tmpDir)); + $this->env = new Twig_Environment(new Twig_Loader_Array(array('index' => 'index', 'index2' => 'index2')), array('cache' => $this->tmpDir)); } public function tearDown() @@ -40,7 +40,7 @@ class Twig_Tests_FileCachingTest extends PHPUnit_Framework_TestCase public function testWritingCacheFiles() { - $name = 'This is just text.'; + $name = 'index'; $this->env->loadTemplate($name); $cacheFileName = $this->env->getCacheFilename($name); @@ -50,7 +50,7 @@ class Twig_Tests_FileCachingTest extends PHPUnit_Framework_TestCase public function testClearingCacheFiles() { - $name = 'I will be deleted.'; + $name = 'index2'; $this->env->loadTemplate($name); $cacheFileName = $this->env->getCacheFilename($name); diff --git a/test/Twig/Tests/NativeExtensionTest.php b/test/Twig/Tests/NativeExtensionTest.php index 9bc6079ff..36b632984 100644 --- a/test/Twig/Tests/NativeExtensionTest.php +++ b/test/Twig/Tests/NativeExtensionTest.php @@ -17,7 +17,7 @@ class Twig_Tests_NativeExtensionTest extends PHPUnit_Framework_TestCase $this->markTestSkipped('Skip under HHVM as the behavior is not the same as plain PHP (which is an edge case anyway)'); } - $twig = new Twig_Environment(new Twig_Loader_String(), array( + $twig = new Twig_Environment(new Twig_Loader_Array(array('index' => '{{ d1.date }}{{ d2.date }}')), array( 'debug' => true, 'cache' => false, 'autoescape' => false, @@ -25,7 +25,7 @@ class Twig_Tests_NativeExtensionTest extends PHPUnit_Framework_TestCase $d1 = new DateTime(); $d2 = new DateTime(); - $output = $twig->render('{{ d1.date }}{{ d2.date }}', compact('d1', 'd2')); + $output = $twig->render('index', compact('d1', 'd2')); // If it fails, PHP will crash. $this->assertEquals($output, $d1->date.$d2->date); diff --git a/test/Twig/Tests/Node/ModuleTest.php b/test/Twig/Tests/Node/ModuleTest.php index e38d86d63..5facc55a4 100644 --- a/test/Twig/Tests/Node/ModuleTest.php +++ b/test/Twig/Tests/Node/ModuleTest.php @@ -30,7 +30,7 @@ class Twig_Tests_Node_ModuleTest extends Twig_Test_NodeTestCase public function getTests() { - $twig = new Twig_Environment(new Twig_Loader_String()); + $twig = new Twig_Environment($this->getMock('Twig_LoaderInterface')); $tests = array(); @@ -46,7 +46,7 @@ class Twig_Tests_Node_ModuleTest extends Twig_Test_NodeTestCase false, 'autoescape' => false)); + $env = new Twig_Environment($this->getMock('Twig_LoaderInterface'), array('cache' => false, 'autoescape' => false)); $stream = $env->parse($env->tokenize('{{ block("foo") }}', 'index')); @@ -24,7 +24,7 @@ class Twig_Tests_NodeVisitor_OptimizerTest extends PHPUnit_Framework_TestCase public function testRenderParentBlockOptimizer() { - $env = new Twig_Environment(new Twig_Loader_String(), array('cache' => false, 'autoescape' => false)); + $env = new Twig_Environment($this->getMock('Twig_LoaderInterface'), array('cache' => false, 'autoescape' => false)); $stream = $env->parse($env->tokenize('{% extends "foo" %}{% block content %}{{ parent() }}{% endblock %}', 'index')); @@ -40,7 +40,7 @@ class Twig_Tests_NodeVisitor_OptimizerTest extends PHPUnit_Framework_TestCase return; } - $env = new Twig_Environment(new Twig_Loader_String(), array('cache' => false, 'autoescape' => false)); + $env = new Twig_Environment($this->getMock('Twig_LoaderInterface'), array('cache' => false, 'autoescape' => false)); $stream = $env->parse($env->tokenize('{{ block(name|lower) }}', 'index')); $node = $stream->getNode('body')->getNode(0)->getNode(1); @@ -54,7 +54,7 @@ class Twig_Tests_NodeVisitor_OptimizerTest extends PHPUnit_Framework_TestCase */ public function testForOptimizer($template, $expected) { - $env = new Twig_Environment(new Twig_Loader_String(), array('cache' => false)); + $env = new Twig_Environment($this->getMock('Twig_LoaderInterface'), array('cache' => false)); $stream = $env->parse($env->tokenize($template, 'index'));