deprecated the possibility to override an extension by registering another one with the same name

This commit is contained in:
Fabien Potencier
2015-10-23 08:37:50 +02:00
parent 847d48e790
commit 72485c2ea0
3 changed files with 29 additions and 2 deletions
+1
View File
@@ -1,5 +1,6 @@
* 1.23.0 (2015-XX-XX)
* deprecated the possibility to override an extension by registering another one with the same name
* deprecated Twig_ExtensionInterface::initRuntime() (added Twig_Extension_InitRuntimeInterface for BC)
* deprecated Twig_Environment::computeAlternatives()
+8 -2
View File
@@ -780,13 +780,19 @@ class Twig_Environment
*/
public function addExtension(Twig_ExtensionInterface $extension)
{
$name = $extension->getName();
if ($this->extensionInitialized) {
throw new LogicException(sprintf('Unable to register extension "%s" as extensions have already been initialized.', $extension->getName()));
throw new LogicException(sprintf('Unable to register extension "%s" as extensions have already been initialized.', $name));
}
if (isset($this->extensions[$name])) {
@trigger_error(sprintf('The possibility to register the same extension twice ("%s") is deprecated and will be removed in Twig 2.0. Use proper PHP inheritance instead.', $name), E_USER_DEPRECATED);
}
$this->lastModifiedExtension = 0;
$this->extensions[$extension->getName()] = $extension;
$this->extensions[$name] = $extension;
}
/**
+20
View File
@@ -342,6 +342,26 @@ class Twig_Tests_EnvironmentTest extends PHPUnit_Framework_TestCase
}
}
/**
* @requires PHP 5.3
*/
public function testOverrideExtenion()
{
$twig = new Twig_Environment($this->getMock('Twig_LoaderInterface'));
$twig->addExtension(new Twig_Tests_EnvironmentTest_ExtensionWithDeprecationInitRuntime());
$this->deprecations = array();
set_error_handler(array($this, 'handleError'));
$twig->addExtension(new Twig_Tests_EnvironmentTest_Extension());
$twig->addExtension(new Twig_Tests_EnvironmentTest_Extension());
$this->assertCount(1, $this->deprecations);
$this->assertContains('The possibility to register the same extension twice', $this->deprecations[0]);
restore_error_handler();
}
protected function getMockLoader($templateName, $templateContent)
{
$loader = $this->getMock('Twig_LoaderInterface');