mirror of
https://github.com/twigphp/Twig.git
synced 2026-09-10 01:16:52 +00:00
Fix optimizing non-public named closures
This commit is contained in:
@@ -289,6 +289,7 @@ abstract class CallExpression extends AbstractExpression
|
||||
return $this->reflector = [$r, $callable, $r->class.'::'.$r->name];
|
||||
}
|
||||
|
||||
$checkVisibility = $callable instanceof \Closure;
|
||||
$r = new \ReflectionFunction(\Closure::fromCallable($callable));
|
||||
|
||||
if (false !== strpos($r->name, '{closure}')) {
|
||||
@@ -305,6 +306,10 @@ abstract class CallExpression extends AbstractExpression
|
||||
$callable = $callableName = $r->name;
|
||||
}
|
||||
|
||||
if ($checkVisibility && \is_array($callable) && method_exists(...$callable) && !(new \ReflectionMethod(...$callable))->isPublic()) {
|
||||
$callable = $r->getClosure();
|
||||
}
|
||||
|
||||
return $this->reflector = [$r, $callable, $callableName];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -48,12 +48,17 @@ class FilterTest extends NodeTestCase
|
||||
{
|
||||
return [
|
||||
new TwigFilter('foo', \Closure::fromCallable([$this, 'foo'])),
|
||||
new TwigFilter('foobar', \Closure::fromCallable([$this, 'foobar'])),
|
||||
];
|
||||
}
|
||||
|
||||
public function foo()
|
||||
{
|
||||
}
|
||||
|
||||
protected function foobar()
|
||||
{
|
||||
}
|
||||
};
|
||||
$environment->addExtension($extension);
|
||||
|
||||
@@ -127,6 +132,9 @@ class FilterTest extends NodeTestCase
|
||||
$node = $this->createFilter($string, 'foo');
|
||||
$tests[] = [$node, sprintf('$this->extensions[\'%s\']->foo("abc")', \get_class($extension)), $environment];
|
||||
|
||||
$node = $this->createFilter($string, 'foobar');
|
||||
$tests[] = [$node, '$this->env->getFilter(\'foobar\')->getCallable()("abc")', $environment];
|
||||
|
||||
return $tests;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user