fixed regression on static calls for functions/filters/tests

This commit is contained in:
Fabien Potencier
2016-10-04 19:06:18 -07:00
parent f00e7b8c14
commit e01c29be32
6 changed files with 67 additions and 13 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
* 1.26.1 (2016-XX-XX) * 1.26.1 (2016-XX-XX)
* n/a * fixed regression on static calls for functions/filters/tests
* 1.26.0 (2016-10-02) * 1.26.0 (2016-10-02)
+14 -1
View File
@@ -219,6 +219,9 @@ abstract class Twig_Node_Expression_Call extends Twig_Node_Expression
private function getCallableParameters($callable, $isVariadic) private function getCallableParameters($callable, $isVariadic)
{ {
list($r, $_) = $this->reflectCallable($callable); list($r, $_) = $this->reflectCallable($callable);
if (null === $r) {
return array();
}
$parameters = $r->getParameters(); $parameters = $r->getParameters();
if ($this->hasNode('node')) { if ($this->hasNode('node')) {
@@ -259,14 +262,24 @@ abstract class Twig_Node_Expression_Call extends Twig_Node_Expression
} }
if (is_array($callable)) { if (is_array($callable)) {
if (!method_exists($callable[0], $callable[1])) {
// __call()
return array(null, array());
}
$r = new ReflectionMethod($callable[0], $callable[1]); $r = new ReflectionMethod($callable[0], $callable[1]);
} elseif (is_object($callable) && !$callable instanceof Closure) { } elseif (is_object($callable) && !$callable instanceof Closure) {
$r = new ReflectionObject($callable); $r = new ReflectionObject($callable);
$r = $r->getMethod('__invoke'); $r = $r->getMethod('__invoke');
$callable = array($callable, '__invoke'); $callable = array($callable, '__invoke');
} elseif (is_string($callable) && false !== $pos = strpos($callable, '::')) { } elseif (is_string($callable) && false !== $pos = strpos($callable, '::')) {
$class = substr($callable, 0, $pos);
$method = substr($callable, $pos + 2);
if (!method_exists($class, $method)) {
// __staticCall()
return array(null, array());
}
$r = new ReflectionMethod($callable); $r = new ReflectionMethod($callable);
$callable = array(substr($callable, 0, $pos), substr($callable, $pos + 2)); $callable = array($class, $method);
} else { } else {
$r = new ReflectionFunction($callable); $r = new ReflectionFunction($callable);
} }
@@ -6,21 +6,21 @@ Twig supports __call() for attributes
--DATA-- --DATA--
class TestClassForMagicCallAttributes class TestClassForMagicCallAttributes
{ {
public function getBar() public function getBar()
{
return 'bar_from_getbar';
}
public function __call($method, $arguments)
{
if ('foo' === $method)
{ {
return 'foo_from_call'; return 'bar_from_getbar';
} }
return false; public function __call($method, $arguments)
} {
if ('foo' === $method) {
return 'foo_from_call';
}
return false;
}
} }
return array('foo' => new TestClassForMagicCallAttributes()) return array('foo' => new TestClassForMagicCallAttributes())
--EXPECT-- --EXPECT--
foo_from_call foo_from_call
@@ -0,0 +1,8 @@
--TEST--
__call calls
--TEMPLATE--
{{ 'foo'|magic_call }}
--DATA--
return array()
--EXPECT--
magic_foo
@@ -0,0 +1,12 @@
--TEST--
__staticCall calls
--CONDITION--
version_compare(phpversion(), '5.3.0', '>=')
--TEMPLATE--
{{ 'foo'|magic_call_string }}
{{ 'foo'|magic_call_array }}
--DATA--
return array()
--EXPECT--
static_magic_foo
static_magic_foo
+21
View File
@@ -143,6 +143,9 @@ class TwigTestExtension extends Twig_Extension
new Twig_SimpleFilter('preserves_safety', array($this, 'preserves_safety'), array('preserves_safety' => array('html'))), new Twig_SimpleFilter('preserves_safety', array($this, 'preserves_safety'), array('preserves_safety' => array('html'))),
new Twig_SimpleFilter('static_call_string', 'TwigTestExtension::staticCall'), new Twig_SimpleFilter('static_call_string', 'TwigTestExtension::staticCall'),
new Twig_SimpleFilter('static_call_array', array('TwigTestExtension', 'staticCall')), new Twig_SimpleFilter('static_call_array', array('TwigTestExtension', 'staticCall')),
new Twig_SimpleFilter('magic_call', array($this, 'magicCall')),
new Twig_SimpleFilter('magic_call_string', 'TwigTestExtension::magicStaticCall'),
new Twig_SimpleFilter('magic_call_array', array('TwigTestExtension', 'magicStaticCall')),
new Twig_SimpleFilter('*_path', array($this, 'dynamic_path')), new Twig_SimpleFilter('*_path', array($this, 'dynamic_path')),
new Twig_SimpleFilter('*_foo_*_bar', array($this, 'dynamic_foo')), new Twig_SimpleFilter('*_foo_*_bar', array($this, 'dynamic_foo')),
); );
@@ -230,4 +233,22 @@ class TwigTestExtension extends Twig_Extension
{ {
return false !== strpos($value, ' '); return false !== strpos($value, ' ');
} }
public function __call($method, $arguments)
{
if ('magicCall' !== $method) {
throw new BadMethodCallException('Unexpected call to __call');
}
return 'magic_'.$arguments[0];
}
public static function __callStatic($method, $arguments)
{
if ('magicStaticCall' !== $method) {
throw new BadMethodCallException('Unexpected call to __callStatic');
}
return 'static_magic_'.$arguments[0];
}
} }