Skip to content

Commit cc48443

Browse files
committed
Fixed accessing to underscore methods
1 parent 6ed315a commit cc48443

File tree

3 files changed

+15
-5
lines changed

3 files changed

+15
-5
lines changed

ext/twig/twig.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -695,6 +695,7 @@ static char *TWIG_GET_CLASS_NAME(zval *object TSRMLS_DC)
695695

696696
static int twig_add_method_to_class(void *pDest APPLY_TSRMLS_DC, int num_args, va_list args, zend_hash_key *hash_key)
697697
{
698+
int from;
698699
zval *retval;
699700
char *method, *lMethod;
700701
size_t method_len;
@@ -715,11 +716,13 @@ static int twig_add_method_to_class(void *pDest APPLY_TSRMLS_DC, int num_args, v
715716
add_assoc_string(retval, TWIG_DECAMELIZE(method), method, 1);
716717

717718
if (method_len > 3 && 0 == strncmp("get", lMethod, 3)) {
718-
add_assoc_string(retval, estrndup(lMethod+3, method_len-3), method, 1);
719-
add_assoc_string(retval, TWIG_DECAMELIZE(estrndup(method+3, method_len-3)), method, 1);
719+
from = lMethod[3] == '_' ? 4 : 3;
720+
add_assoc_string(retval, estrndup(lMethod+from, method_len-from), method, 1);
721+
add_assoc_string(retval, TWIG_DECAMELIZE(estrndup(method+from, method_len-from)), method, 1);
720722
} else if (method_len > 2 && 0 == strncmp("is", lMethod, 2)) {
721-
add_assoc_string(retval, estrndup(lMethod+2, method_len-2), method, 1);
722-
add_assoc_string(retval, TWIG_DECAMELIZE(estrndup(method+2, method_len-2)), method, 1);
723+
from = lMethod[2] == '_' ? 3 : 2;
724+
add_assoc_string(retval, estrndup(lMethod+from, method_len-from), method, 1);
725+
add_assoc_string(retval, TWIG_DECAMELIZE(estrndup(method+from, method_len-from)), method, 1);
723726
}
724727

725728
return 0;

lib/Twig/Template.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,7 @@ protected function getCacheForClass($class)
504504
}
505505

506506
$cache = array_combine($methods, $methods);
507-
$keys = array_merge(preg_replace('/^(?:get|is)(.++)$/i', '\\1', $methods), $methods);
507+
$keys = array_merge(preg_replace('/^(?:get|is)_?(.++)$/i', '\\1', $methods), $methods);
508508
$keys = array_merge(preg_replace('/((?<=[a-z]|\d)[A-Z]|(?<!^)[A-Z](?=[a-z]))/', '_\\1', $keys), $keys);
509509

510510
return array('methods' => $cache + array_change_key_case(array_combine($keys, array_merge($methods, $methods, $methods, $methods))));

test/Twig/Tests/TemplateTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,8 @@ public function getGetAttributeTests()
376376

377377
array(true, 'httpresponsecode', $methodAndPropObject, 'http_response_code', array(), $anyType),
378378
array(true, 'httpresponsecode', $methodAndPropObject, 'get_http_response_code', array(), $methodType),
379+
380+
array(true, 'http2_response', $methodAndPropObject, 'http2_response', array(), $anyType),
379381
));
380382

381383
// tests when input is not an array or object
@@ -647,6 +649,11 @@ public function GetHTTPResponseCode()
647649
{
648650
return 'httpresponsecode';
649651
}
652+
653+
public function get_http2_response()
654+
{
655+
return 'http2_response';
656+
}
650657
}
651658

652659
class Twig_TemplateMagicMethodObject

0 commit comments

Comments
 (0)