Skip to content

Commit

Permalink
fixed regression when registering two extensions having the same clas…
Browse files Browse the repository at this point in the history
…s name
  • Loading branch information
fabpot committed Oct 23, 2016
1 parent d6e6074 commit 47432f3
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 31 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
* 1.27.0 (2016-XX-XX)

* fixed regression when registering two extensions having the same class name
* deprecated Twig_LoaderInterface::getSource() (implement Twig_SourceContextLoaderInterface instead)
* fixed the filesystem loader with relative paths
* deprecated Twig_Node::getLine() in favor of Twig_Node::getTemplateLine()
Expand Down
69 changes: 38 additions & 31 deletions lib/Twig/Environment.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class Twig_Environment
private $bcWriteCacheFile = false;
private $bcGetCacheFilename = false;
private $lastModifiedExtension = 0;
private $legacyExtensionNames = array();
private $extensionsByClass = array();
private $runtimeLoaders = array();
private $runtimes = array();
private $optionsHash;
Expand Down Expand Up @@ -816,12 +816,16 @@ public function initRuntime()
*/
public function hasExtension($class)
{
if (isset($this->legacyExtensionNames[$class])) {
$class = $this->legacyExtensionNames[$class];
@trigger_error(sprintf('Referencing the "%s" extension by its name (defined by getName()) is deprecated since 1.26 and will be removed in Twig 2.0. Use the Fully Qualified Extension Class Name instead.', $class), E_USER_DEPRECATED);
$class = ltrim($class, '\\');
if (isset($this->extensions[$class])) {
if ($class !== get_class($this->extensions[$class])) {
@trigger_error(sprintf('Referencing the "%s" extension by its name (defined by getName()) is deprecated since 1.26 and will be removed in Twig 2.0. Use the Fully Qualified Extension Class Name instead.', $class), E_USER_DEPRECATED);
}

return true;
}

return isset($this->extensions[ltrim($class, '\\')]);
return isset($this->extensionsByClass[ltrim($class, '\\')]);
}

/**
Expand All @@ -841,18 +845,21 @@ public function addRuntimeLoader(Twig_RuntimeLoaderInterface $loader)
*/
public function getExtension($class)
{
if (isset($this->legacyExtensionNames[$class])) {
$class = $this->legacyExtensionNames[$class];
@trigger_error(sprintf('Referencing the "%s" extension by its name (defined by getName()) is deprecated since 1.26 and will be removed in Twig 2.0. Use the Fully Qualified Extension Class Name instead.', $class), E_USER_DEPRECATED);
}

$class = ltrim($class, '\\');

if (!isset($this->extensions[$class])) {
if (isset($this->extensions[$class])) {
if ($class !== get_class($this->extensions[$class])) {
@trigger_error(sprintf('Referencing the "%s" extension by its name (defined by getName()) is deprecated since 1.26 and will be removed in Twig 2.0. Use the Fully Qualified Extension Class Name instead.', $class), E_USER_DEPRECATED);
}

return $this->extensions[$class];
}

if (!isset($this->extensionsByClass[$class])) {
throw new Twig_Error_Runtime(sprintf('The "%s" extension is not enabled.', $class));
}

return $this->extensions[$class];
return $this->extensionsByClass[$class];
}

/**
Expand Down Expand Up @@ -886,25 +893,21 @@ public function getRuntime($class)
*/
public function addExtension(Twig_ExtensionInterface $extension)
{
$class = get_class($extension);

if ($this->extensionInitialized) {
throw new LogicException(sprintf('Unable to register extension "%s" as extensions have already been initialized.', $class));
throw new LogicException(sprintf('Unable to register extension "%s" as extensions have already been initialized.', $extension->getName()));
}

$m = new ReflectionMethod($extension, 'getName');
$legacyName = 'Twig_Extension' !== $m->getDeclaringClass()->getName() ? $extension->getName() : null;

if (isset($this->extensions[$class]) || (null !== $legacyName && isset($this->legacyExtensionNames[$legacyName]))) {
unset($this->extensions[$this->legacyExtensionNames[$legacyName]], $this->legacyExtensionNames[$legacyName]);
@trigger_error(sprintf('The possibility to register the same extension twice ("%s") is deprecated since version 1.23 and will be removed in Twig 2.0. Use proper PHP inheritance instead.', $class), E_USER_DEPRECATED);
$class = get_class($extension);
if ($class !== $extension->getName()) {
if (isset($this->extensions[$extension->getName()])) {
unset($this->extensions[$extension->getName()], $this->extensionsByClass[$class]);
@trigger_error(sprintf('The possibility to register the same extension twice ("%s") is deprecated since version 1.23 and will be removed in Twig 2.0. Use proper PHP inheritance instead.', $extension->getName()), E_USER_DEPRECATED);
}
}

$this->lastModifiedExtension = 0;
if ($legacyName !== $class) {
$this->legacyExtensionNames[$legacyName] = $class;
}
$this->extensions[$class] = $extension;
$this->extensionsByClass[$class] = $extension;
$this->extensions[$extension->getName()] = $extension;
$this->updateOptionsHash();
}

Expand All @@ -921,16 +924,20 @@ public function removeExtension($name)
{
@trigger_error(sprintf('The %s method is deprecated since version 1.12 and will be removed in Twig 2.0.', __METHOD__), E_USER_DEPRECATED);

if (isset($this->legacyExtensionNames[$name])) {
$name = $this->legacyExtensionNames[$name];
@trigger_error(sprintf('Referencing the "%s" extension by its name (defined by getName()) is deprecated since 1.26 and will be removed in Twig 2.0. Use the Fully Qualified Extension Class Name instead.', $name), E_USER_DEPRECATED);
}

if ($this->extensionInitialized) {
throw new LogicException(sprintf('Unable to remove extension "%s" as extensions have already been initialized.', $name));
}

unset($this->extensions[ltrim($name, '\\')]);
$class = ltrim($name, '\\');
if (isset($this->extensions[$class])) {
if ($class !== get_class($this->extensions[$class])) {
@trigger_error(sprintf('Referencing the "%s" extension by its name (defined by getName()) is deprecated since 1.26 and will be removed in Twig 2.0. Use the Fully Qualified Extension Class Name instead.', $class), E_USER_DEPRECATED);
}

unset($this->extensions[$class]);
}

unset($this->extensions[$class]);
$this->updateOptionsHash();
}

Expand Down
36 changes: 36 additions & 0 deletions test/Twig/Tests/EnvironmentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,27 @@ public function testAutoReloadOutdatedCacheHit()
$twig->loadTemplate($templateName);
}

/**
* @group legacy
*/
public function testHasGetExtensionWithDynamicName()
{
$twig = new Twig_Environment($this->getMockBuilder('Twig_LoaderInterface')->getMock());

$ext1 = new Twig_Tests_EnvironmentTest_Extension_DynamicWithDeprecatedName('ext1');
$ext2 = new Twig_Tests_EnvironmentTest_Extension_DynamicWithDeprecatedName('ext2');
$twig->addExtension($ext1);
$twig->addExtension($ext2);

$this->assertTrue($twig->hasExtension('ext1'));
$this->assertTrue($twig->hasExtension('ext2'));

$this->assertTrue($twig->hasExtension('Twig_Tests_EnvironmentTest_Extension_DynamicWithDeprecatedName'));

$this->assertSame($ext1, $twig->getExtension('ext1'));
$this->assertSame($ext2, $twig->getExtension('ext2'));
}

public function testHasGetExtensionByClassName()
{
$twig = new Twig_Environment($this->getMockBuilder('Twig_LoaderInterface')->getMock());
Expand Down Expand Up @@ -538,6 +559,21 @@ public function getName()
}
}

class Twig_Tests_EnvironmentTest_Extension_DynamicWithDeprecatedName extends Twig_Extension
{
private $name;

public function __construct($name)
{
$this->name = $name;
}

public function getName()
{
return $this->name;
}
}

class Twig_Tests_EnvironmentTest_TokenParser extends Twig_TokenParser
{
public function parse(Twig_Token $token)
Expand Down

0 comments on commit 47432f3

Please sign in to comment.