[apd][patch] Two new functions: override_constant() and rename_constant()
| From: | Johannes Schlueter | Date: | Sat, 11 Sep 2004 23:37:27 +0000 |
| Subject: | [apd][patch] Two new functions: override_constant() and rename_constant() | ||
| Groups: | php.pecl.dev | ||
| Request: | Send a blank email to pecl-dev+get-1314@lists.php.net to get a copy of this message | ||
Hi George,
while trying to understand an application I got a bit frustrated that I can
rename functions using apd and change classes using classkit but that I can't
change constants. So I wrote a small patch for apd adding two small functions
which can change constants.
The protos are quite similar to override_function and rename_function:
/* {{{ proto bool override_consant(string name, mixed value)
overrides the content of the constant name with value */
/* {{{ proto bool rename_constant(string oldname, string newname)
renames the constant oldname to newname */
A small example looks like
<?php
define('FOO', 'asdf');
rename_constant('FOO', 'BAR');
override_constant('BAR', 'qwerty');
echo BAR;
?>
This echos "qwerty".
Maybe you like the idea and maybe even (parts of) the implementation it. ;-)
johannes
Index: php_apd.c =================================================================== RCS file: /repository/pecl/apd/php_apd.c,v retrieving revision 1.58 diff -u -r1.58 php_apd.c --- php_apd.c 11 Mar 2004 22:04:46 -0000 1.58 +++ php_apd.c 11 Sep 2004 22:34:45 -0000 @@ -73,6 +73,8 @@ function_entry apd_functions[] = { PHP_FE(override_function, NULL) PHP_FE(rename_function, NULL) + PHP_FE(override_constant, NULL) + PHP_FE(rename_constant, NULL) PHP_FE(apd_set_pprof_trace, NULL) PHP_FE(apd_set_browser_trace, NULL) PHP_FE(apd_set_session_trace_socket, NULL) @@ -714,6 +716,120 @@ RETURN_TRUE; } +/* {{{ proto bool override_consant(string name, mixed value) + overrides the content of the constant name with value */ +PHP_FUNCTION(override_constant) +{ + char *const_name; + int const_name_len; + zval *z_value; + zend_constant *orig_const, new_const; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s/z", &const_name, &const_name_len, &z_value) == FAILURE) + { + return; + } + + if(zend_hash_find(EG(zend_constants), const_name, + const_name_len + 1, (void **) &orig_const) == FAILURE) + { + zend_error(E_WARNING, "%s failed: %s does not exist", + get_active_function_name(TSRMLS_C), + const_name); + RETURN_FALSE; + } + + new_const.value = *z_value; + zval_copy_ctor(&new_const.value); + new_const.flags = orig_const->flags; + new_const.name = zend_strndup(const_name, const_name_len); + new_const.name_len = const_name_len+1; + new_const.module_number = orig_const->module_number; + + if(zend_hash_del(EG(zend_constants), const_name, + const_name_len + 1) == FAILURE) + { + zend_error(E_WARNING, "%s() failed to remove %s from constants table", + get_active_function_name(TSRMLS_C), + const_name); + RETURN_FALSE; + } + if (zend_register_constant(&new_const TSRMLS_CC) == FAILURE) + { + zend_error(E_WARNING, "%s() failed to register %s constant", + get_active_function_name(TSRMLS_C), + const_name); + RETURN_FALSE; + } + + RETURN_TRUE; +} +/* }}} */ + +/* {{{ proto bool rename_constant(string oldname, string newname) + renames the constant oldname to newname */ +PHP_FUNCTION(rename_constant) +{ + zval **z_orig_cname, **z_new_cname; + zend_constant *orig_const, *new_const, *dummy_const; + + if( ZEND_NUM_ARGS() != 2 || + zend_get_parameters_ex(2, &z_orig_cname, &z_new_cname) == FAILURE ) + { + ZEND_WRONG_PARAM_COUNT(); + } + + convert_to_string_ex(z_orig_cname); + convert_to_string_ex(z_new_cname); + + if(zend_hash_find(EG(zend_constants), Z_STRVAL_PP(z_orig_cname), + Z_STRLEN_PP(z_orig_cname) + 1, (void **) &orig_const) == FAILURE) + { + zend_error(E_WARNING, "%s(%s, %s) failed: %s does not exist!", + get_active_function_name(TSRMLS_C), + Z_STRVAL_PP(z_orig_cname), Z_STRVAL_PP(z_new_cname), + Z_STRVAL_PP(z_orig_cname)); + RETURN_FALSE; + } + if(zend_hash_find(EG(zend_constants), Z_STRVAL_PP(z_new_cname), + Z_STRLEN_PP(z_new_cname) + 1, (void **) &dummy_const) == SUCCESS) + { + zend_error(E_WARNING, "%s(%s, %s) failed: %s already exists!", + get_active_function_name(TSRMLS_C), + Z_STRVAL_PP(z_orig_cname), Z_STRVAL_PP(z_new_cname), + Z_STRVAL_PP(z_new_cname)); + RETURN_FALSE; + } + + new_const = (zend_constant *) emalloc(sizeof(zend_constant)); + new_const->value = orig_const->value; + zval_copy_ctor(&new_const->value); + new_const->flags = orig_const->flags; + new_const->name = Z_STRVAL_PP(z_new_cname); + new_const->name_len = Z_STRLEN_PP(z_new_cname) + 1; + copy_zend_constant(new_const); + + if (zend_register_constant(new_const TSRMLS_CC) == FAILURE) + { + zend_error(E_WARNING, "%s() failed to register %s constant", + get_active_function_name(TSRMLS_C), + Z_STRVAL_PP(z_new_cname)); + RETURN_FALSE; + } + if(zend_hash_del(EG(zend_constants), Z_STRVAL_PP(z_orig_cname), + Z_STRLEN_PP(z_orig_cname) + 1) == FAILURE) + { + zend_error(E_WARNING, "%s() failed to remove %s from constants table", + get_active_function_name(TSRMLS_C), + Z_STRVAL_PP(z_orig_cname)); + zend_hash_del(EG(function_table), Z_STRVAL_PP(z_new_cname), + Z_STRLEN_PP(z_new_cname) + 1); + RETURN_FALSE; + } + RETURN_TRUE; +} +/* }}} */ + void apd_pprof_header(char *ent_fname TSRMLS_DC) { char *fname = "main"; char *filename; Index: php_apd.h =================================================================== RCS file: /repository/pecl/apd/php_apd.h,v retrieving revision 1.21 diff -u -r1.21 php_apd.h --- php_apd.h 10 Mar 2004 16:40:55 -0000 1.21 +++ php_apd.h 11 Sep 2004 22:34:45 -0000 @@ -71,6 +71,10 @@ /* Declarations of functions to be exported. */ PHP_FUNCTION(override_function); PHP_FUNCTION(rename_function); + +PHP_FUNCTION(override_constant); +PHP_FUNCTION(rename_constant); + PHP_FUNCTION(apd_set_pprof_trace); PHP_FUNCTION(apd_set_browser_trace); PHP_FUNCTION(apd_set_session_trace_socket);
Index: php_apd.c =================================================================== RCS file: /repository/pecl/apd/php_apd.c,v retrieving revision 1.58 diff -u -r1.58 php_apd.c --- php_apd.c 11 Mar 2004 22:04:46 -0000 1.58 +++ php_apd.c 11 Sep 2004 22:34:45 -0000 @@ -73,6 +73,8 @@ function_entry apd_functions[] = { PHP_FE(override_function, NULL) PHP_FE(rename_function, NULL) + PHP_FE(override_constant, NULL) + PHP_FE(rename_constant, NULL) PHP_FE(apd_set_pprof_trace, NULL) PHP_FE(apd_set_browser_trace, NULL) PHP_FE(apd_set_session_trace_socket, NULL) @@ -714,6 +716,120 @@ RETURN_TRUE; } +/* {{{ proto bool override_consant(string name, mixed value) + overrides the content of the constant name with value */ +PHP_FUNCTION(override_constant) +{ + char *const_name; + int const_name_len; + zval *z_value; + zend_constant *orig_const, new_const; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s/z", &const_name, &const_name_len, &z_value) == FAILURE) + { + return; + } + + if(zend_hash_find(EG(zend_constants), const_name, + const_name_len + 1, (void **) &orig_const) == FAILURE) + { + zend_error(E_WARNING, "%s failed: %s does not exist", + get_active_function_name(TSRMLS_C), + const_name); + RETURN_FALSE; + } + + new_const.value = *z_value; + zval_copy_ctor(&new_const.value); + new_const.flags = orig_const->flags; + new_const.name = zend_strndup(const_name, const_name_len); + new_const.name_len = const_name_len+1; + new_const.module_number = orig_const->module_number; + + if(zend_hash_del(EG(zend_constants), const_name, + const_name_len + 1) == FAILURE) + { + zend_error(E_WARNING, "%s() failed to remove %s from constants table", + get_active_function_name(TSRMLS_C), + const_name); + RETURN_FALSE; + } + if (zend_register_constant(&new_const TSRMLS_CC) == FAILURE) + { + zend_error(E_WARNING, "%s() failed to register %s constant", + get_active_function_name(TSRMLS_C), + const_name); + RETURN_FALSE; + } + + RETURN_TRUE; +} +/* }}} */ + +/* {{{ proto bool rename_constant(string oldname, string newname) + renames the constant oldname to newname */ +PHP_FUNCTION(rename_constant) +{ + zval **z_orig_cname, **z_new_cname; + zend_constant *orig_const, *new_const, *dummy_const; + + if( ZEND_NUM_ARGS() != 2 || + zend_get_parameters_ex(2, &z_orig_cname, &z_new_cname) == FAILURE ) + { + ZEND_WRONG_PARAM_COUNT(); + } + + convert_to_string_ex(z_orig_cname); + convert_to_string_ex(z_new_cname); + + if(zend_hash_find(EG(zend_constants), Z_STRVAL_PP(z_orig_cname), + Z_STRLEN_PP(z_orig_cname) + 1, (void **) &orig_const) == FAILURE) + { + zend_error(E_WARNING, "%s(%s, %s) failed: %s does not exist!", + get_active_function_name(TSRMLS_C), + Z_STRVAL_PP(z_orig_cname), Z_STRVAL_PP(z_new_cname), + Z_STRVAL_PP(z_orig_cname)); + RETURN_FALSE; + } + if(zend_hash_find(EG(zend_constants), Z_STRVAL_PP(z_new_cname), + Z_STRLEN_PP(z_new_cname) + 1, (void **) &dummy_const) == SUCCESS) + { + zend_error(E_WARNING, "%s(%s, %s) failed: %s already exists!", + get_active_function_name(TSRMLS_C), + Z_STRVAL_PP(z_orig_cname), Z_STRVAL_PP(z_new_cname), + Z_STRVAL_PP(z_new_cname)); + RETURN_FALSE; + } + + new_const = (zend_constant *) emalloc(sizeof(zend_constant)); + new_const->value = orig_const->value; + zval_copy_ctor(&new_const->value); + new_const->flags = orig_const->flags; + new_const->name = Z_STRVAL_PP(z_new_cname); + new_const->name_len = Z_STRLEN_PP(z_new_cname) + 1; + copy_zend_constant(new_const); + + if (zend_register_constant(new_const TSRMLS_CC) == FAILURE) + { + zend_error(E_WARNING, "%s() failed to register %s constant", + get_active_function_name(TSRMLS_C), + Z_STRVAL_PP(z_new_cname)); + RETURN_FALSE; + } + if(zend_hash_del(EG(zend_constants), Z_STRVAL_PP(z_orig_cname), + Z_STRLEN_PP(z_orig_cname) + 1) == FAILURE) + { + zend_error(E_WARNING, "%s() failed to remove %s from constants table", + get_active_function_name(TSRMLS_C), + Z_STRVAL_PP(z_orig_cname)); + zend_hash_del(EG(function_table), Z_STRVAL_PP(z_new_cname), + Z_STRLEN_PP(z_new_cname) + 1); + RETURN_FALSE; + } + RETURN_TRUE; +} +/* }}} */ + void apd_pprof_header(char *ent_fname TSRMLS_DC) { char *fname = "main"; char *filename; Index: php_apd.h =================================================================== RCS file: /repository/pecl/apd/php_apd.h,v retrieving revision 1.21 diff -u -r1.21 php_apd.h --- php_apd.h 10 Mar 2004 16:40:55 -0000 1.21 +++ php_apd.h 11 Sep 2004 22:34:45 -0000 @@ -71,6 +71,10 @@ /* Declarations of functions to be exported. */ PHP_FUNCTION(override_function); PHP_FUNCTION(rename_function); + +PHP_FUNCTION(override_constant); +PHP_FUNCTION(rename_constant); + PHP_FUNCTION(apd_set_pprof_trace); PHP_FUNCTION(apd_set_browser_trace); PHP_FUNCTION(apd_set_session_trace_socket);