Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding ignoreStrictCheck option for call_user_func_array, if throwing exception #1286

Merged
merged 3 commits into from
Dec 3, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
* 1.15.0 (2013-XX-XX)

* made ignoreStrictCheck in Template::getAttribute() works with __call() methods throwing BadMethodCallException
* added the round filter
* fixed a bug that prevented the optimizers to be enabled/disabled selectively
* fixed first and last filters for UTF-8 strings
Expand Down
23 changes: 22 additions & 1 deletion ext/twig/twig.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@

#include "php.h"
#include "php_twig.h"
#include "ext/standard/php_var.h"
#include "ext/standard/php_string.h"
#include "ext/standard/php_smart_str.h"
#include "ext/spl/spl_exceptions.h"

#include "Zend/zend_object_handlers.h"
#include "Zend/zend_interfaces.h"
Expand Down Expand Up @@ -945,6 +947,7 @@ PHP_FUNCTION(twig_template_get_attributes)
self::$cache[$class]['methods'] = array_change_key_case(array_flip(get_class_methods($object)));
}

$call = false;
$lcItem = strtolower($item);
if (isset(self::$cache[$class]['methods'][$lcItem])) {
$method = (string) $item;
Expand All @@ -954,8 +957,10 @@ PHP_FUNCTION(twig_template_get_attributes)
$method = 'is'.$item;
} elseif (isset(self::$cache[$class]['methods']['__call'])) {
$method = (string) $item;
$call = true;
*/
{
int call = 0;
char *lcItem = TWIG_STRTOLOWER(item, item_len);
int lcItem_length;
char *method = NULL;
Expand All @@ -981,6 +986,7 @@ PHP_FUNCTION(twig_template_get_attributes)
method = tmp_method_name_is;
} else if (TWIG_GET_ARRAY_ELEMENT(tmp_methods, "__call", 6 TSRMLS_CC)) {
method = item;
call = 1;
/*
} else {
if ($isDefinedTest) {
Expand Down Expand Up @@ -1037,9 +1043,24 @@ PHP_FUNCTION(twig_template_get_attributes)
return;
}
/*
$ret = call_user_func_array(array($object, $method), $arguments);
// Some objects throw exceptions when they have __call, and the method we try
// to call is not supported. If ignoreStrictCheck is true, we should return null.
try {
$ret = call_user_func_array(array($object, $method), $arguments);
} catch (BadMethodCallException $e) {
if ($call && ($ignoreStrictCheck || !$this->env->isStrictVariables())) {
return null;
}
throw $e;
}
*/
ret = TWIG_CALL_USER_FUNC_ARRAY(object, method, arguments TSRMLS_CC);
if (EG(exception) && TWIG_INSTANCE_OF(EG(exception), spl_ce_BadMethodCallException TSRMLS_CC)) {
if (ignoreStrictCheck || !TWIG_CALL_BOOLEAN(TWIG_PROPERTY_CHAR(template, "env" TSRMLS_CC), "isStrictVariables" TSRMLS_CC)) {
zend_clear_exception(TSRMLS_C);
return;
}
}
free_ret = 1;
efree(tmp_method_name_get);
efree(tmp_method_name_is);
Expand Down
13 changes: 12 additions & 1 deletion lib/Twig/Template.php
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,7 @@ protected function getAttribute($object, $item, array $arguments = array(), $typ
self::$cache[$class]['methods'] = array_change_key_case(array_flip(get_class_methods($object)));
}

$call = false;
$lcItem = strtolower($item);
if (isset(self::$cache[$class]['methods'][$lcItem])) {
$method = (string) $item;
Expand All @@ -426,6 +427,7 @@ protected function getAttribute($object, $item, array $arguments = array(), $typ
$method = 'is'.$item;
} elseif (isset(self::$cache[$class]['methods']['__call'])) {
$method = (string) $item;
$call = true;
} else {
if ($isDefinedTest) {
return false;
Expand All @@ -446,7 +448,16 @@ protected function getAttribute($object, $item, array $arguments = array(), $typ
$this->env->getExtension('sandbox')->checkMethodAllowed($object, $method);
}

$ret = call_user_func_array(array($object, $method), $arguments);
// Some objects throw exceptions when they have __call, and the method we try
// to call is not supported. If ignoreStrictCheck is true, we should return null.
try {
$ret = call_user_func_array(array($object, $method), $arguments);
} catch (BadMethodCallException $e) {
if ($call && ($ignoreStrictCheck || !$this->env->isStrictVariables())) {
return null;
}
throw $e;
}

// useful when calling a template method from a template
// this is not supported but unfortunately heavily used in the Symfony profiler
Expand Down
20 changes: 20 additions & 0 deletions test/Twig/Tests/TemplateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,18 @@ public function testGetAttributeDefinedStrict($defined, $value, $object, $item,
$this->assertEquals($defined, $template->getAttribute($object, $item, $arguments, $type, true));
}

/**
* @dataProvider getTestsDependingOnExtensionAvailability
*/
public function testGetAttributeCallExceptions($useExt = false)
{
$template = new Twig_TemplateTest(new Twig_Environment(), $useExt);

$object = new Twig_TemplateMagicMethodExceptionObject();

$this->assertEquals(null, $template->getAttribute($object, 'foo'));
}

public function getGetAttributeTests()
{
$array = array(
Expand Down Expand Up @@ -603,6 +615,14 @@ public function __call($method, $arguments)
}
}

class Twig_TemplateMagicMethodExceptionObject
{
public function __call($method, $arguments)
{
throw new BadMethodCallException(sprintf('Unkown method %s', $method));
}
}

class CExtDisablingNodeVisitor implements Twig_NodeVisitorInterface
{
public function enterNode(Twig_NodeInterface $node, Twig_Environment $env)
Expand Down