mirror of
https://github.com/twigphp/Twig.git
synced 2026-09-12 18:36:53 +00:00
fixed regression on static calls for functions/filters/tests
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
* 1.26.1 (2016-XX-XX)
|
||||
|
||||
* n/a
|
||||
* fixed regression on static calls for functions/filters/tests
|
||||
|
||||
* 1.26.0 (2016-10-02)
|
||||
|
||||
|
||||
@@ -219,6 +219,9 @@ abstract class Twig_Node_Expression_Call extends Twig_Node_Expression
|
||||
private function getCallableParameters($callable, $isVariadic)
|
||||
{
|
||||
list($r, $_) = $this->reflectCallable($callable);
|
||||
if (null === $r) {
|
||||
return array();
|
||||
}
|
||||
|
||||
$parameters = $r->getParameters();
|
||||
if ($this->hasNode('node')) {
|
||||
@@ -259,14 +262,24 @@ abstract class Twig_Node_Expression_Call extends Twig_Node_Expression
|
||||
}
|
||||
|
||||
if (is_array($callable)) {
|
||||
if (!method_exists($callable[0], $callable[1])) {
|
||||
// __call()
|
||||
return array(null, array());
|
||||
}
|
||||
$r = new ReflectionMethod($callable[0], $callable[1]);
|
||||
} elseif (is_object($callable) && !$callable instanceof Closure) {
|
||||
$r = new ReflectionObject($callable);
|
||||
$r = $r->getMethod('__invoke');
|
||||
$callable = array($callable, '__invoke');
|
||||
} 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);
|
||||
$callable = array(substr($callable, 0, $pos), substr($callable, $pos + 2));
|
||||
$callable = array($class, $method);
|
||||
} else {
|
||||
$r = new ReflectionFunction($callable);
|
||||
}
|
||||
|
||||
@@ -6,21 +6,21 @@ Twig supports __call() for attributes
|
||||
--DATA--
|
||||
class TestClassForMagicCallAttributes
|
||||
{
|
||||
public function getBar()
|
||||
{
|
||||
return 'bar_from_getbar';
|
||||
}
|
||||
|
||||
public function __call($method, $arguments)
|
||||
{
|
||||
if ('foo' === $method)
|
||||
public function getBar()
|
||||
{
|
||||
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())
|
||||
--EXPECT--
|
||||
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
|
||||
@@ -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('static_call_string', '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('*_foo_*_bar', array($this, 'dynamic_foo')),
|
||||
);
|
||||
@@ -230,4 +233,22 @@ class TwigTestExtension extends Twig_Extension
|
||||
{
|
||||
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];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user