feature #1897 deprecated Twig_ExtensionInterface::getGlobals() (fabpot)

This PR was merged into the 1.x branch.

Discussion
----------

deprecated Twig_ExtensionInterface::getGlobals()

Commits
-------

e3a325f deprecated Twig_ExtensionInterface::getGlobals()
This commit is contained in:
Fabien Potencier
2015-10-29 16:18:41 -07:00
9 changed files with 81 additions and 6 deletions
+1
View File
@@ -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()
+3 -1
View File
@@ -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();
+4
View File
@@ -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
----
+8
View File
@@ -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)));
+3 -1
View File
@@ -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()
{
+22
View File
@@ -0,0 +1,22 @@
<?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.
*/
/**
* Enables usage of the deprecated Twig_Extension::getGlobals() method.
*
* Explicitly implement this interface if you really need to implement the
* deprecated getGlobals() method in your extensions.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
interface Twig_Extension_GlobalsInterface
{
}
+2 -2
View File
@@ -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 <fabien@symfony.com>
+3 -1
View File
@@ -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();
+35 -1
View File
@@ -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()
{