diff --git a/CHANGELOG b/CHANGELOG index 8a33184e5..f7da14318 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,7 @@ * 1.23.0 (2015-XX-XX) * deprecated the possibility to override an extension by registering another one with the same name + * deprecated Twig_ExtensionInterface::getGlobals() (added Twig_Extension_GlobalsInterface for BC) * deprecated Twig_ExtensionInterface::initRuntime() (added Twig_Extension_InitRuntimeInterface for BC) * deprecated Twig_Environment::computeAlternatives() diff --git a/doc/advanced.rst b/doc/advanced.rst index 65dd2cc86..e4085925f 100644 --- a/doc/advanced.rst +++ b/doc/advanced.rst @@ -554,7 +554,7 @@ An extension is a class that implements the following interface:: * * @param Twig_Environment $environment The current Twig_Environment instance * - * @deprecated since 1.23 (to be removed in 2.0) + * @deprecated since 1.23 (to be removed in 2.0), implement Twig_Extension_InitRuntimeInterace instead */ function initRuntime(Twig_Environment $environment); @@ -604,6 +604,8 @@ An extension is a class that implements the following interface:: * Returns a list of global variables to add to the existing list. * * @return array An array of global variables + * + * @deprecated since 1.23 (to be removed in 2.0), implement Twig_Extension_GlobalsProviderInterace instead */ function getGlobals(); diff --git a/doc/deprecated.rst b/doc/deprecated.rst index 3d9c16749..9df411472 100644 --- a/doc/deprecated.rst +++ b/doc/deprecated.rst @@ -33,6 +33,10 @@ Extensions have more complex needs, explicitly implement ``Twig_Extension_InitRuntimeInterface`` (not recommended). +* As of Twig 1.23, the ``Twig_ExtensionInterface::getGlobals()`` method is + deprecated. Implement ``Twig_Extension_GlobalsInterface`` to avoid + deprecation notices. + PEAR ---- diff --git a/lib/Twig/Environment.php b/lib/Twig/Environment.php index 9a0367ea8..0cb2ae208 100644 --- a/lib/Twig/Environment.php +++ b/lib/Twig/Environment.php @@ -1265,6 +1265,14 @@ class Twig_Environment { $globals = array(); foreach ($this->extensions as $extension) { + if (!$extension instanceof Twig_Extension_GlobalsInterface) { + $m = new ReflectionMethod($extension, 'getGlobals'); + + if ('Twig_Extension' !== $m->getDeclaringClass()->getName()) { + @trigger_error(sprintf('Defining the getGlobals() method in an extension is deprecated without explicitly implementing Twig_Extension_GlobalsInterface.', $name), E_USER_DEPRECATED); + } + } + $extGlob = $extension->getGlobals(); if (!is_array($extGlob)) { throw new UnexpectedValueException(sprintf('"%s::getGlobals()" must return an array of globals.', get_class($extension))); diff --git a/lib/Twig/Extension.php b/lib/Twig/Extension.php index b7ab67488..fb09be76b 100644 --- a/lib/Twig/Extension.php +++ b/lib/Twig/Extension.php @@ -13,7 +13,7 @@ abstract class Twig_Extension implements Twig_ExtensionInterface /** * {@inheritdoc} * - * @deprecated since 1.23 (to be removed in 2.0) + * @deprecated since 1.23 (to be removed in 2.0), implement Twig_Extension_InitRuntimeInterace instead */ public function initRuntime(Twig_Environment $environment) { @@ -69,6 +69,8 @@ abstract class Twig_Extension implements Twig_ExtensionInterface /** * {@inheritdoc} + * + * @deprecated since 1.23 (to be removed in 2.0), implement Twig_Extension_GlobalsProviderInterace instead */ public function getGlobals() { diff --git a/lib/Twig/Extension/GlobalsInterface.php b/lib/Twig/Extension/GlobalsInterface.php new file mode 100644 index 000000000..5370b8e2a --- /dev/null +++ b/lib/Twig/Extension/GlobalsInterface.php @@ -0,0 +1,22 @@ + + */ +interface Twig_Extension_GlobalsInterface +{ +} diff --git a/lib/Twig/Extension/InitRuntimeInterface.php b/lib/Twig/Extension/InitRuntimeInterface.php index f16555e36..7a075822f 100644 --- a/lib/Twig/Extension/InitRuntimeInterface.php +++ b/lib/Twig/Extension/InitRuntimeInterface.php @@ -10,9 +10,9 @@ */ /** - * Declares the deprecated Twig_Extension::initRuntime() method. + * Enables usage of the deprecated Twig_Extension::initRuntime() method. * - * Explicitely implement this interface if you really need to implement the + * Explicitly implement this interface if you really need to implement the * deprecated initRuntime() method in your extensions. * * @author Fabien Potencier diff --git a/lib/Twig/ExtensionInterface.php b/lib/Twig/ExtensionInterface.php index d7ebed329..af4d081f3 100644 --- a/lib/Twig/ExtensionInterface.php +++ b/lib/Twig/ExtensionInterface.php @@ -23,7 +23,7 @@ interface Twig_ExtensionInterface * * @param Twig_Environment $environment The current Twig_Environment instance * - * @deprecated since 1.23 (to be removed in 2.0) + * @deprecated since 1.23 (to be removed in 2.0), implement Twig_Extension_InitRuntimeInterace instead */ public function initRuntime(Twig_Environment $environment); @@ -73,6 +73,8 @@ interface Twig_ExtensionInterface * Returns a list of global variables to add to the existing list. * * @return array An array of global variables + * + * @deprecated since 1.23 (to be removed in 2.0), implement Twig_Extension_GlobalsProviderInterace instead */ public function getGlobals(); diff --git a/test/Twig/Tests/EnvironmentTest.php b/test/Twig/Tests/EnvironmentTest.php index 1b0dae0d6..b8e49c941 100644 --- a/test/Twig/Tests/EnvironmentTest.php +++ b/test/Twig/Tests/EnvironmentTest.php @@ -273,6 +273,25 @@ class Twig_Tests_EnvironmentTest extends PHPUnit_Framework_TestCase $this->assertEquals('Twig_Tests_EnvironmentTest_NodeVisitor', get_class($visitors[2])); } + /** + * @requires PHP 5.3 + */ + public function testAddExtensionWithDeprecatedGetGlobals() + { + $twig = new Twig_Environment($this->getMock('Twig_LoaderInterface')); + $twig->addExtension(new Twig_Tests_EnvironmentTest_Extension_WithGlobals()); + + $this->deprecations = array(); + set_error_handler(array($this, 'handleError')); + + $this->assertArrayHasKey('foo_global', $twig->getGlobals()); + + $this->assertCount(1, $this->deprecations); + $this->assertContains('Defining the getGlobals() method in an extension is deprecated', $this->deprecations[0]); + + restore_error_handler(); + } + /** * @group legacy */ @@ -378,7 +397,22 @@ class Twig_Tests_EnvironmentTest extends PHPUnit_Framework_TestCase } } -class Twig_Tests_EnvironmentTest_Extension extends Twig_Extension +class Twig_Tests_EnvironmentTest_Extension_WithGlobals extends Twig_Extension +{ + public function getGlobals() + { + return array( + 'foo_global' => 'foo_global', + ); + } + + public function getName() + { + return 'environment_test'; + } +} + +class Twig_Tests_EnvironmentTest_Extension extends Twig_Extension implements Twig_Extension_GlobalsInterface { public function getTokenParsers() {