cvs: pecl /classkit README classkit.c config.m4 config.w32 package.xml php_classkit.h
| From: | Sara Golemon | Date: | Fri, 20 Aug 2004 20:16:46 +0000 |
| Subject: | cvs: pecl /classkit README classkit.c config.m4 config.w32 package.xml php_classkit.h | ||
| Groups: | php.pecl.cvs | ||
| Request: | Send a blank email to pecl-cvs+get-1695@lists.php.net to get a copy of this message | ||
pollita Fri Aug 20 16:16:46 2004 EDT
Added files:
/pecl/classkit README classkit.c config.m4 config.w32 package.xml
php_classkit.h
Log:
Initial Release
http://cvs.php.net/co.php/pecl/classkit/classkit.c?r=1.1&p=1 Index: pecl/classkit/classkit.c +++ pecl/classkit/classkit.c /* +----------------------------------------------------------------------+ | PHP Version 5 | +----------------------------------------------------------------------+ | Copyright (c) 1997-2004 The PHP Group | +----------------------------------------------------------------------+ | This source file is subject to version 3.0 of the PHP license, | | that is bundled with this package in the file LICENSE, and is | | available through the world-wide-web at the following url: | | http://www.php.net/license/3_0.txt. | | If you did not receive a copy of the PHP license and are unable to | | obtain it through the world-wide-web, please send a note to | | license@php.net so we can mail you a copy immediately. | +----------------------------------------------------------------------+ | Author: Sara Golemon <pollita@php.net> | +----------------------------------------------------------------------+ */ /* $Id: classkit.c,v 1.1 2004/08/20 20:16:46 pollita Exp $ */ #ifdef HAVE_CONFIG_H #include "config.h" #endif #include "php.h" #include "ext/standard/info.h" #include "ext/standard/php_string.h" #include "php_classkit.h" /* {{{ classkit_functions[] * * Every user visible function must have an entry in classkit_functions[]. */ function_entry classkit_functions[] = { PHP_FE(classkit_method_add, NULL) PHP_FE(classkit_method_redefine, NULL) PHP_FE(classkit_method_remove, NULL) PHP_FE(classkit_method_rename, NULL) {NULL, NULL, NULL} }; /* }}} */ /* {{{ classkit_module_entry */ zend_module_entry classkit_module_entry = { #if ZEND_MODULE_API_NO >= 20010901 STANDARD_MODULE_HEADER, #endif "classkit", classkit_functions, PHP_MINIT(classkit), NULL, /* MSHUTDOWN */ NULL, /* RINIT */ NULL, /* RSHUTDOWN */ PHP_MINFO(classkit), #if ZEND_MODULE_API_NO >= 20010901 "0.1", #endif STANDARD_MODULE_PROPERTIES }; /* }}} */ #ifdef COMPILE_DL_CLASSKIT ZEND_GET_MODULE(classkit) #endif /* {{{ PHP_MINFO_FUNCTION */ PHP_MINFO_FUNCTION(classkit) { php_info_print_table_start(); php_info_print_table_header(2, "classkit support", "enabled"); php_info_print_table_end(); } /* }}} */ /* {{{ php_classkit_fetch_class */ static int php_classkit_fetch_class(char *classname, int classname_len, zend_class_entry **pce TSRMLS_DC) { zend_class_entry *ce; #ifdef ZEND_ENGINE_2 zend_class_entry **ze; if (zend_hash_find(EG(class_table), classname, classname_len + 1, (void**)&ze) == FAILURE || !ze || !*ze) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "class %s not found", classname); return FAILURE; } ce = *ze; #else if (zend_hash_find(EG(class_table), classname, classname_len + 1, (void**)&ce) == FAILURE || !ce) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "class %s not found", classname); return FAILURE; } #endif if (ce->type != ZEND_USER_CLASS) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "class %s is not a user-defined class", classname); return FAILURE; } *pce = ce; return SUCCESS; } /* }}} */ /* {{{ php_classkit_fetch_class_method */ static int php_classkit_fetch_class_method(char *classname, int classname_len, char *methodname, int methodname_len, zend_class_entry **pce, zend_function **pfe TSRMLS_DC) { zend_class_entry *ce; zend_function *fe; if (php_classkit_fetch_class(classname, classname_len, &ce TSRMLS_CC) == FAILURE) { return FAILURE; } if (zend_hash_find(&ce->function_table, methodname, methodname_len + 1, (void**)&fe) == FAILURE || !fe) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Method %s::%s not found", classname, methodname); return FAILURE; } if (fe->type != ZEND_USER_FUNCTION) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s::%s is not a user defined method", classname, methodname); return FAILURE; } *pce = ce; *pfe = fe; return SUCCESS; } /* }}} */ #define CLASSKIT_TEMP_FUNCNAME "__classkit_temporary_function__" /* {{{ php_classkit_generate_lambda_method Heavily borrowed from ZEND_FUNCTION(create_function) */ static int php_classkit_generate_lambda_method(char *arguments, int arguments_len, char *phpcode, int phpcode_len, zend_function **pfe TSRMLS_DC) { char *eval_code, *eval_name; int eval_code_length; eval_code_length = sizeof("function " CLASSKIT_TEMP_FUNCNAME) + arguments_len + 4 + phpcode_len; eval_code = (char*)emalloc(eval_code_length); snprintf(eval_code, eval_code_length, "function " CLASSKIT_TEMP_FUNCNAME "(%s){%s}", arguments, phpcode); eval_name = zend_make_compiled_string_description("classkit runtime-created function" TSRMLS_CC); if (zend_eval_string(eval_code, NULL, eval_name TSRMLS_CC) == FAILURE) { efree(eval_code); efree(eval_name); return FAILURE; } efree(eval_code); efree(eval_name); if (zend_hash_find(EG(function_table), CLASSKIT_TEMP_FUNCNAME, sizeof(CLASSKIT_TEMP_FUNCNAME), (void **)pfe) == FAILURE) { php_error_docref(NULL TSRMLS_CC, E_ERROR, "Unexpected inconsistency during create_function"); return FAILURE; } return SUCCESS; } /* }}} */ #ifdef ZEND_ENGINE_2 /* Why doesn't ZE2 define this? */ #define zend_hash_add_or_update(ht, arKey, nKeyLength, pData, pDataSize, pDest, flag) \ _zend_hash_add_or_update((ht), (arKey), (nKeyLength), (pData), (pDataSize), (pDest), (flag) ZEND_FILE_LINE_CC) #endif /* {{{ php_classkit_method_add_or_update */ static void php_classkit_method_add_or_update(INTERNAL_FUNCTION_PARAMETERS, int add_or_update) { char *classname, *methodname, *arguments, *phpcode; int classname_len, methodname_len, arguments_len, phpcode_len; zend_class_entry *ce; zend_function func, *fe; #ifdef ZEND_ENGINE_2 long flags = ZEND_ACC_PUBLIC; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ssss|l", &classname, &classname_len, &methodname, &methodname_len, &arguments, &arguments_len, &phpcode, &phpcode_len, &flags) == FAILURE) { #else if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ssss", &classname, &classname_len, &methodname, &methodname_len, &arguments, &arguments_len, &phpcode, &phpcode_len) == FAILURE) { #endif RETURN_FALSE; } if (!classname_len || !methodname_len) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Empty parameter given"); RETURN_FALSE; } php_strtolower(classname, classname_len); php_strtolower(methodname, methodname_len); if (add_or_update == HASH_UPDATE) { if (php_classkit_fetch_class_method(classname, classname_len, methodname, methodname_len, &ce, &fe TSRMLS_CC) == FAILURE) { RETURN_FALSE; } if (&fe->op_array == EG(active_op_array)) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot redefine a method from inside itself!"); RETURN_FALSE; } } else { if (php_classkit_fetch_class(classname, classname_len, &ce TSRMLS_CC) == FAILURE) { RETURN_FALSE; } } if (php_classkit_generate_lambda_method(arguments, arguments_len, phpcode, phpcode_len, &fe TSRMLS_CC) == FAILURE) { RETURN_FALSE; } func = *fe; function_add_ref(&func); efree(func.common.function_name); func.common.function_name = estrndup(methodname, methodname_len); #ifdef ZEND_ENGINE_2 func.common.scope = ce; if (flags & ZEND_ACC_PRIVATE) { func.common.fn_flags &= ~ZEND_ACC_PPP_MASK; func.common.fn_flags |= ZEND_ACC_PRIVATE; } else if (flags & ZEND_ACC_PROTECTED) { func.common.fn_flags &= ~ZEND_ACC_PPP_MASK; func.common.fn_flags |= ZEND_ACC_PROTECTED; } func.common.fn_flags |= ZEND_ACC_ALLOW_STATIC; #endif if (zend_hash_add_or_update(&ce->function_table, methodname, methodname_len + 1, &func, sizeof(zend_function), NULL, add_or_update) == FAILURE) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to add method to class"); RETURN_FALSE; } if (zend_hash_del(EG(function_table), CLASSKIT_TEMP_FUNCNAME, sizeof(CLASSKIT_TEMP_FUNCNAME)) == FAILURE) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to remove temporary function entry"); RETURN_FALSE; } if (zend_hash_find(&ce->function_table, methodname, methodname_len + 1, (void**)&fe) == FAILURE || !fe) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to locate newly added method"); RETURN_FALSE; } #ifdef ZEND_ENGINE_2 if (strcmp(methodname, ce->name) == 0 || strcmp(methodname, "construct") == 0) ce->constructor = fe; else if (strcmp(methodname, "destruct") == 0) ce->destructor = fe; else if (strcmp(methodname, "__clone") == 0) ce->clone = fe; else if (strcmp(methodname, "__get") == 0) ce->__get = fe; else if (strcmp(methodname, "__set") == 0) ce->__set = fe; else if (strcmp(methodname, "__call") == 0) ce->__call = fe; if (strcmp(methodname, ce->name) == 0 || strcmp(methodname, "construct") == 0) { ce->constructor = fe; fe->common.fn_flags |= ZEND_ACC_CTOR; } else if (strcmp(methodname, "destruct") == 0) { ce->destructor = fe; fe->common.fn_flags |= ZEND_ACC_DTOR; } else if (strcmp(methodname, "__clone") == 0) { ce->clone = fe; fe->common.fn_flags |= ZEND_ACC_CLONE; } else if (strcmp(methodname, "__get") == 0) ce->__get = fe; else if (strcmp(methodname, "__set") == 0) ce->__set = fe; else if (strcmp(methodname, "__call") == 0) ce->__call = fe; #endif RETURN_TRUE; } /* }}} */ /* {{{ proto bool classkit_method_add(string classname, string methodname, string args, string code[, long flags]) Add a method to an already defined class */ PHP_FUNCTION(classkit_method_add) { php_classkit_method_add_or_update(INTERNAL_FUNCTION_PARAM_PASSTHRU, HASH_ADD); } /* }}} */ /* {{{ proto bool classkit_method_redefine(string classname, string methodname, string args, string code[, long flags]) Redefine an already defined class method */ PHP_FUNCTION(classkit_method_redefine) { php_classkit_method_add_or_update(INTERNAL_FUNCTION_PARAM_PASSTHRU, HASH_UPDATE); } /* }}} */ /* {{{ proto bool classkit_method_remove(string classname, string methodname) Remove a class method */ PHP_FUNCTION(classkit_method_remove) { char *classname, *methodname; int classname_len, methodname_len; zend_class_entry *ce; zend_function *fe; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &classname, &classname_len, &methodname, &methodname_len) == FAILURE) { RETURN_FALSE; } if (!classname_len || !methodname_len) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Empty parameter given"); RETURN_FALSE; } php_strtolower(classname, classname_len); php_strtolower(methodname, methodname_len); if (php_classkit_fetch_class_method(classname, classname_len, methodname, methodname_len, &ce, &fe TSRMLS_CC) == FAILURE) { RETURN_FALSE; } if (&fe->op_array == EG(active_op_array)) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot remove a method while inside it!"); RETURN_FALSE; } if (zend_hash_del(&ce->function_table, methodname, methodname_len + 1) == FAILURE) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Error dereferencing old method"); RETURN_FALSE; } RETURN_TRUE; } /* }}} */ /* {{{ proto bool classkit_method_rename(string classname, string methodname, string newname) Rename a class method */ PHP_FUNCTION(classkit_method_rename) { char *classname, *methodname, *newname; int classname_len, methodname_len, newname_len; zend_class_entry *ce; zend_function *fe, tmp; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sss", &classname, &classname_len, &methodname, &methodname_len, &newname, &newname_len) == FAILURE) { RETURN_FALSE; } if (!classname_len || !methodname_len || !newname_len) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Empty parameter given"); RETURN_FALSE; } php_strtolower(classname, classname_len); php_strtolower(methodname, methodname_len); php_strtolower(newname, newname_len); if (strcmp(methodname, newname) == 0) { /* Nothing to do */ RETURN_FALSE; } if (php_classkit_fetch_class_method(classname, classname_len, methodname, methodname_len, &ce, &fe TSRMLS_CC) == FAILURE) { RETURN_FALSE; } if (&fe->op_array == EG(active_op_array)) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot rename a method while inside it!"); RETURN_FALSE; } if (zend_hash_exists(&ce->function_table, newname, newname_len + 1)) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "method %s::%s already exists", classname, newname); RETURN_FALSE; } tmp = *fe; /* Fool the op_array into not destructing */ fe->type = ZEND_INTERNAL_FUNCTION; if (zend_hash_del(&ce->function_table, methodname, methodname_len + 1) == FAILURE) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Error dereferencing old method"); RETURN_FALSE; } efree(tmp.common.function_name); tmp.common.function_name = estrndup(newname, newname_len); if (zend_hash_add(&ce->function_table, newname, newname_len + 1, &tmp, sizeof(zend_function), NULL) == FAILURE) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Error adding new method name"); RETURN_FALSE; } #ifdef ZEND_ENGINE_2 if (fe == ce->constructor) { ce->constructor = NULL; fe->common.fn_flags &= ~ZEND_ACC_CTOR; } if (fe == ce->destructor) { ce->destructor = NULL; fe->common.fn_flags &= ~ZEND_ACC_DTOR; } if (fe == ce->clone) { ce->clone = NULL; fe->common.fn_flags &= ~ZEND_ACC_CLONE; } if (fe == ce->__get) ce->__get = NULL; if (fe == ce->__set) ce->__set = NULL; if (fe == ce->__call) ce->__call = NULL; if (strcmp(newname, ce->name) == 0 || strcmp(newname, "construct") == 0) { ce->constructor = fe; fe->common.fn_flags |= ZEND_ACC_CTOR; } else if (strcmp(newname, "destruct") == 0) { ce->destructor = fe; fe->common.fn_flags |= ZEND_ACC_DTOR; } else if (strcmp(newname, "__clone") == 0) { ce->clone = fe; fe->common.fn_flags |= ZEND_ACC_CLONE; } else if (strcmp(newname, "__get") == 0) ce->__get = fe; else if (strcmp(newname, "__set") == 0) ce->__set = fe; else if (strcmp(newname, "__call") == 0) ce->__call = fe; #endif RETURN_TRUE; } /* }}} */ /* {{{ PHP_MINIT_FUNCTION */ PHP_MINIT_FUNCTION(classkit) { #ifdef ZEND_ENGINE_2 REGISTER_LONG_CONSTANT("CLASSKIT_ACC_PUBLIC", ZEND_ACC_PUBLIC, CONST_CS | CONST_PERSISTENT); REGISTER_LONG_CONSTANT("CLASSKIT_ACC_PROTECTED", ZEND_ACC_PROTECTED, CONST_CS | CONST_PERSISTENT); REGISTER_LONG_CONSTANT("CLASSKIT_ACC_PRIVATE", ZEND_ACC_PRIVATE, CONST_CS | CONST_PERSISTENT); #endif return SUCCESS; } /* }}} */ /* * Local variables: * tab-width: 4 * c-basic-offset: 4 * End: * vim600: noet sw=4 ts=4 fdm=marker * vim<600: noet sw=4 ts=4 */ http://cvs.php.net/co.php/pecl/classkit/config.m4?r=1.1&p=1 Index: pecl/classkit/config.m4 +++ pecl/classkit/config.m4 dnl $Id: config.m4,v 1.1 2004/08/20 20:16:46 pollita Exp $ dnl config.m4 for extension classkit PHP_ARG_ENABLE(classkit, whether to enable classkit support, [ --enable-classkit Enable classkit support]) if test "$PHP_CLASSKIT" != "no"; then AC_DEFINE(WITH_CLASSKIT,1,[ Classkit support ]) PHP_NEW_EXTENSION(classkit, classkit.c, $ext_shared) fi http://cvs.php.net/co.php/pecl/classkit/config.w32?r=1.1&p=1 Index: pecl/classkit/config.w32 +++ pecl/classkit/config.w32 // $Id: config.w32,v 1.1 2004/08/20 20:16:46 pollita Exp $ // vim:ft=javascript ARG_ENABLE("classkit", "enable classkit support", "no"); if (PHP_CLASSKIT != "no") { AC_DEFINE('WITH_CLASSKIT', 1); EXTENSION("classkit", "classkit.c"); } http://cvs.php.net/co.php/pecl/classkit/package.xml?r=1.1&p=1 Index: pecl/classkit/package.xml +++ pecl/classkit/package.xml <?xml version="1.0" encoding="ISO-8859-1" ?> <package version="1.0"> <name>classkit</name> <summary>Runtime redefinition of class methods</summary> <description> Allows a running script to add, remove, rename, and redefine class methods without reloading. </description> <license>PHP</license> <maintainers> <maintainer> <user>pollita</user> <name>Sara Golemon</name> <email>pollita@php.net</email> <role>lead</role> </maintainer> </maintainers> <release> <version>0.1</version> <state>beta</state> <date>2004-08-20</date> <notes>Initial Release</notes> <filelist> <file role="src" name="config.m4"/> <file role="src" name="config.w32"/> <file role="src" name="classkit.c"/> <file role="src" name="php_classkit.h"/> <file role="doc" name="README"/> </filelist> </release> </package> http://cvs.php.net/co.php/pecl/classkit/php_classkit.h?r=1.1&p=1 Index: pecl/classkit/php_classkit.h +++ pecl/classkit/php_classkit.h /* +----------------------------------------------------------------------+ | PHP Version 5 | +----------------------------------------------------------------------+ | Copyright (c) 1997-2004 The PHP Group | +----------------------------------------------------------------------+ | This source file is subject to version 3.0 of the PHP license, | | that is bundled with this package in the file LICENSE, and is | | available through the world-wide-web at the following url: | | http://www.php.net/license/3_0.txt. | | If you did not receive a copy of the PHP license and are unable to | | obtain it through the world-wide-web, please send a note to | | license@php.net so we can mail you a copy immediately. | +----------------------------------------------------------------------+ | Author: Sara Golemon <pollita@php.net> | +----------------------------------------------------------------------+ */ /* $Id: php_classkit.h,v 1.1 2004/08/20 20:16:46 pollita Exp $ */ #ifndef PHP_CLASSKIT_H #define PHP_CLASSKIT_H extern zend_module_entry classkit_module_entry; #define phpext_classkit_ptr &classkit_module_entry PHP_MINIT_FUNCTION(classkit); PHP_MINFO_FUNCTION(classkit); PHP_FUNCTION(classkit_method_add); PHP_FUNCTION(classkit_method_redefine); PHP_FUNCTION(classkit_method_remove); PHP_FUNCTION(classkit_method_rename); #endif /* PHP_CLASSKIT_H */ /* * Local variables: * tab-width: 4 * c-basic-offset: 4 * End: * vim600: noet sw=4 ts=4 fdm=marker * vim<600: noet sw=4 ts=4 */
http://cvs.php.net/co.php/pecl/classkit/classkit.c?r=1.1&p=1 Index: pecl/classkit/classkit.c +++ pecl/classkit/classkit.c /* +----------------------------------------------------------------------+ | PHP Version 5 | +----------------------------------------------------------------------+ | Copyright (c) 1997-2004 The PHP Group | +----------------------------------------------------------------------+ | This source file is subject to version 3.0 of the PHP license, | | that is bundled with this package in the file LICENSE, and is | | available through the world-wide-web at the following url: | | http://www.php.net/license/3_0.txt. | | If you did not receive a copy of the PHP license and are unable to | | obtain it through the world-wide-web, please send a note to | | license@php.net so we can mail you a copy immediately. | +----------------------------------------------------------------------+ | Author: Sara Golemon <pollita@php.net> | +----------------------------------------------------------------------+ */ /* $Id: classkit.c,v 1.1 2004/08/20 20:16:46 pollita Exp $ */ #ifdef HAVE_CONFIG_H #include "config.h" #endif #include "php.h" #include "ext/standard/info.h" #include "ext/standard/php_string.h" #include "php_classkit.h" /* {{{ classkit_functions[] * * Every user visible function must have an entry in classkit_functions[]. */ function_entry classkit_functions[] = { PHP_FE(classkit_method_add, NULL) PHP_FE(classkit_method_redefine, NULL) PHP_FE(classkit_method_remove, NULL) PHP_FE(classkit_method_rename, NULL) {NULL, NULL, NULL} }; /* }}} */ /* {{{ classkit_module_entry */ zend_module_entry classkit_module_entry = { #if ZEND_MODULE_API_NO >= 20010901 STANDARD_MODULE_HEADER, #endif "classkit", classkit_functions, PHP_MINIT(classkit), NULL, /* MSHUTDOWN */ NULL, /* RINIT */ NULL, /* RSHUTDOWN */ PHP_MINFO(classkit), #if ZEND_MODULE_API_NO >= 20010901 "0.1", #endif STANDARD_MODULE_PROPERTIES }; /* }}} */ #ifdef COMPILE_DL_CLASSKIT ZEND_GET_MODULE(classkit) #endif /* {{{ PHP_MINFO_FUNCTION */ PHP_MINFO_FUNCTION(classkit) { php_info_print_table_start(); php_info_print_table_header(2, "classkit support", "enabled"); php_info_print_table_end(); } /* }}} */ /* {{{ php_classkit_fetch_class */ static int php_classkit_fetch_class(char *classname, int classname_len, zend_class_entry **pce TSRMLS_DC) { zend_class_entry *ce; #ifdef ZEND_ENGINE_2 zend_class_entry **ze; if (zend_hash_find(EG(class_table), classname, classname_len + 1, (void**)&ze) == FAILURE || !ze || !*ze) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "class %s not found", classname); return FAILURE; } ce = *ze; #else if (zend_hash_find(EG(class_table), classname, classname_len + 1, (void**)&ce) == FAILURE || !ce) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "class %s not found", classname); return FAILURE; } #endif if (ce->type != ZEND_USER_CLASS) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "class %s is not a user-defined class", classname); return FAILURE; } *pce = ce; return SUCCESS; } /* }}} */ /* {{{ php_classkit_fetch_class_method */ static int php_classkit_fetch_class_method(char *classname, int classname_len, char *methodname, int methodname_len, zend_class_entry **pce, zend_function **pfe TSRMLS_DC) { zend_class_entry *ce; zend_function *fe; if (php_classkit_fetch_class(classname, classname_len, &ce TSRMLS_CC) == FAILURE) { return FAILURE; } if (zend_hash_find(&ce->function_table, methodname, methodname_len + 1, (void**)&fe) == FAILURE || !fe) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Method %s::%s not found", classname, methodname); return FAILURE; } if (fe->type != ZEND_USER_FUNCTION) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s::%s is not a user defined method", classname, methodname); return FAILURE; } *pce = ce; *pfe = fe; return SUCCESS; } /* }}} */ #define CLASSKIT_TEMP_FUNCNAME "__classkit_temporary_function__" /* {{{ php_classkit_generate_lambda_method Heavily borrowed from ZEND_FUNCTION(create_function) */ static int php_classkit_generate_lambda_method(char *arguments, int arguments_len, char *phpcode, int phpcode_len, zend_function **pfe TSRMLS_DC) { char *eval_code, *eval_name; int eval_code_length; eval_code_length = sizeof("function " CLASSKIT_TEMP_FUNCNAME) + arguments_len + 4 + phpcode_len; eval_code = (char*)emalloc(eval_code_length); snprintf(eval_code, eval_code_length, "function " CLASSKIT_TEMP_FUNCNAME "(%s){%s}", arguments, phpcode); eval_name = zend_make_compiled_string_description("classkit runtime-created function" TSRMLS_CC); if (zend_eval_string(eval_code, NULL, eval_name TSRMLS_CC) == FAILURE) { efree(eval_code); efree(eval_name); return FAILURE; } efree(eval_code); efree(eval_name); if (zend_hash_find(EG(function_table), CLASSKIT_TEMP_FUNCNAME, sizeof(CLASSKIT_TEMP_FUNCNAME), (void **)pfe) == FAILURE) { php_error_docref(NULL TSRMLS_CC, E_ERROR, "Unexpected inconsistency during create_function"); return FAILURE; } return SUCCESS; } /* }}} */ #ifdef ZEND_ENGINE_2 /* Why doesn't ZE2 define this? */ #define zend_hash_add_or_update(ht, arKey, nKeyLength, pData, pDataSize, pDest, flag) \ _zend_hash_add_or_update((ht), (arKey), (nKeyLength), (pData), (pDataSize), (pDest), (flag) ZEND_FILE_LINE_CC) #endif /* {{{ php_classkit_method_add_or_update */ static void php_classkit_method_add_or_update(INTERNAL_FUNCTION_PARAMETERS, int add_or_update) { char *classname, *methodname, *arguments, *phpcode; int classname_len, methodname_len, arguments_len, phpcode_len; zend_class_entry *ce; zend_function func, *fe; #ifdef ZEND_ENGINE_2 long flags = ZEND_ACC_PUBLIC; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ssss|l", &classname, &classname_len, &methodname, &methodname_len, &arguments, &arguments_len, &phpcode, &phpcode_len, &flags) == FAILURE) { #else if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ssss", &classname, &classname_len, &methodname, &methodname_len, &arguments, &arguments_len, &phpcode, &phpcode_len) == FAILURE) { #endif RETURN_FALSE; } if (!classname_len || !methodname_len) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Empty parameter given"); RETURN_FALSE; } php_strtolower(classname, classname_len); php_strtolower(methodname, methodname_len); if (add_or_update == HASH_UPDATE) { if (php_classkit_fetch_class_method(classname, classname_len, methodname, methodname_len, &ce, &fe TSRMLS_CC) == FAILURE) { RETURN_FALSE; } if (&fe->op_array == EG(active_op_array)) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot redefine a method from inside itself!"); RETURN_FALSE; } } else { if (php_classkit_fetch_class(classname, classname_len, &ce TSRMLS_CC) == FAILURE) { RETURN_FALSE; } } if (php_classkit_generate_lambda_method(arguments, arguments_len, phpcode, phpcode_len, &fe TSRMLS_CC) == FAILURE) { RETURN_FALSE; } func = *fe; function_add_ref(&func); efree(func.common.function_name); func.common.function_name = estrndup(methodname, methodname_len); #ifdef ZEND_ENGINE_2 func.common.scope = ce; if (flags & ZEND_ACC_PRIVATE) { func.common.fn_flags &= ~ZEND_ACC_PPP_MASK; func.common.fn_flags |= ZEND_ACC_PRIVATE; } else if (flags & ZEND_ACC_PROTECTED) { func.common.fn_flags &= ~ZEND_ACC_PPP_MASK; func.common.fn_flags |= ZEND_ACC_PROTECTED; } func.common.fn_flags |= ZEND_ACC_ALLOW_STATIC; #endif if (zend_hash_add_or_update(&ce->function_table, methodname, methodname_len + 1, &func, sizeof(zend_function), NULL, add_or_update) == FAILURE) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to add method to class"); RETURN_FALSE; } if (zend_hash_del(EG(function_table), CLASSKIT_TEMP_FUNCNAME, sizeof(CLASSKIT_TEMP_FUNCNAME)) == FAILURE) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to remove temporary function entry"); RETURN_FALSE; } if (zend_hash_find(&ce->function_table, methodname, methodname_len + 1, (void**)&fe) == FAILURE || !fe) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to locate newly added method"); RETURN_FALSE; } #ifdef ZEND_ENGINE_2 if (strcmp(methodname, ce->name) == 0 || strcmp(methodname, "construct") == 0) ce->constructor = fe; else if (strcmp(methodname, "destruct") == 0) ce->destructor = fe; else if (strcmp(methodname, "__clone") == 0) ce->clone = fe; else if (strcmp(methodname, "__get") == 0) ce->__get = fe; else if (strcmp(methodname, "__set") == 0) ce->__set = fe; else if (strcmp(methodname, "__call") == 0) ce->__call = fe; if (strcmp(methodname, ce->name) == 0 || strcmp(methodname, "construct") == 0) { ce->constructor = fe; fe->common.fn_flags |= ZEND_ACC_CTOR; } else if (strcmp(methodname, "destruct") == 0) { ce->destructor = fe; fe->common.fn_flags |= ZEND_ACC_DTOR; } else if (strcmp(methodname, "__clone") == 0) { ce->clone = fe; fe->common.fn_flags |= ZEND_ACC_CLONE; } else if (strcmp(methodname, "__get") == 0) ce->__get = fe; else if (strcmp(methodname, "__set") == 0) ce->__set = fe; else if (strcmp(methodname, "__call") == 0) ce->__call = fe; #endif RETURN_TRUE; } /* }}} */ /* {{{ proto bool classkit_method_add(string classname, string methodname, string args, string code[, long flags]) Add a method to an already defined class */ PHP_FUNCTION(classkit_method_add) { php_classkit_method_add_or_update(INTERNAL_FUNCTION_PARAM_PASSTHRU, HASH_ADD); } /* }}} */ /* {{{ proto bool classkit_method_redefine(string classname, string methodname, string args, string code[, long flags]) Redefine an already defined class method */ PHP_FUNCTION(classkit_method_redefine) { php_classkit_method_add_or_update(INTERNAL_FUNCTION_PARAM_PASSTHRU, HASH_UPDATE); } /* }}} */ /* {{{ proto bool classkit_method_remove(string classname, string methodname) Remove a class method */ PHP_FUNCTION(classkit_method_remove) { char *classname, *methodname; int classname_len, methodname_len; zend_class_entry *ce; zend_function *fe; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &classname, &classname_len, &methodname, &methodname_len) == FAILURE) { RETURN_FALSE; } if (!classname_len || !methodname_len) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Empty parameter given"); RETURN_FALSE; } php_strtolower(classname, classname_len); php_strtolower(methodname, methodname_len); if (php_classkit_fetch_class_method(classname, classname_len, methodname, methodname_len, &ce, &fe TSRMLS_CC) == FAILURE) { RETURN_FALSE; } if (&fe->op_array == EG(active_op_array)) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot remove a method while inside it!"); RETURN_FALSE; } if (zend_hash_del(&ce->function_table, methodname, methodname_len + 1) == FAILURE) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Error dereferencing old method"); RETURN_FALSE; } RETURN_TRUE; } /* }}} */ /* {{{ proto bool classkit_method_rename(string classname, string methodname, string newname) Rename a class method */ PHP_FUNCTION(classkit_method_rename) { char *classname, *methodname, *newname; int classname_len, methodname_len, newname_len; zend_class_entry *ce; zend_function *fe, tmp; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sss", &classname, &classname_len, &methodname, &methodname_len, &newname, &newname_len) == FAILURE) { RETURN_FALSE; } if (!classname_len || !methodname_len || !newname_len) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Empty parameter given"); RETURN_FALSE; } php_strtolower(classname, classname_len); php_strtolower(methodname, methodname_len); php_strtolower(newname, newname_len); if (strcmp(methodname, newname) == 0) { /* Nothing to do */ RETURN_FALSE; } if (php_classkit_fetch_class_method(classname, classname_len, methodname, methodname_len, &ce, &fe TSRMLS_CC) == FAILURE) { RETURN_FALSE; } if (&fe->op_array == EG(active_op_array)) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot rename a method while inside it!"); RETURN_FALSE; } if (zend_hash_exists(&ce->function_table, newname, newname_len + 1)) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "method %s::%s already exists", classname, newname); RETURN_FALSE; } tmp = *fe; /* Fool the op_array into not destructing */ fe->type = ZEND_INTERNAL_FUNCTION; if (zend_hash_del(&ce->function_table, methodname, methodname_len + 1) == FAILURE) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Error dereferencing old method"); RETURN_FALSE; } efree(tmp.common.function_name); tmp.common.function_name = estrndup(newname, newname_len); if (zend_hash_add(&ce->function_table, newname, newname_len + 1, &tmp, sizeof(zend_function), NULL) == FAILURE) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Error adding new method name"); RETURN_FALSE; } #ifdef ZEND_ENGINE_2 if (fe == ce->constructor) { ce->constructor = NULL; fe->common.fn_flags &= ~ZEND_ACC_CTOR; } if (fe == ce->destructor) { ce->destructor = NULL; fe->common.fn_flags &= ~ZEND_ACC_DTOR; } if (fe == ce->clone) { ce->clone = NULL; fe->common.fn_flags &= ~ZEND_ACC_CLONE; } if (fe == ce->__get) ce->__get = NULL; if (fe == ce->__set) ce->__set = NULL; if (fe == ce->__call) ce->__call = NULL; if (strcmp(newname, ce->name) == 0 || strcmp(newname, "construct") == 0) { ce->constructor = fe; fe->common.fn_flags |= ZEND_ACC_CTOR; } else if (strcmp(newname, "destruct") == 0) { ce->destructor = fe; fe->common.fn_flags |= ZEND_ACC_DTOR; } else if (strcmp(newname, "__clone") == 0) { ce->clone = fe; fe->common.fn_flags |= ZEND_ACC_CLONE; } else if (strcmp(newname, "__get") == 0) ce->__get = fe; else if (strcmp(newname, "__set") == 0) ce->__set = fe; else if (strcmp(newname, "__call") == 0) ce->__call = fe; #endif RETURN_TRUE; } /* }}} */ /* {{{ PHP_MINIT_FUNCTION */ PHP_MINIT_FUNCTION(classkit) { #ifdef ZEND_ENGINE_2 REGISTER_LONG_CONSTANT("CLASSKIT_ACC_PUBLIC", ZEND_ACC_PUBLIC, CONST_CS | CONST_PERSISTENT); REGISTER_LONG_CONSTANT("CLASSKIT_ACC_PROTECTED", ZEND_ACC_PROTECTED, CONST_CS | CONST_PERSISTENT); REGISTER_LONG_CONSTANT("CLASSKIT_ACC_PRIVATE", ZEND_ACC_PRIVATE, CONST_CS | CONST_PERSISTENT); #endif return SUCCESS; } /* }}} */ /* * Local variables: * tab-width: 4 * c-basic-offset: 4 * End: * vim600: noet sw=4 ts=4 fdm=marker * vim<600: noet sw=4 ts=4 */ http://cvs.php.net/co.php/pecl/classkit/config.m4?r=1.1&p=1 Index: pecl/classkit/config.m4 +++ pecl/classkit/config.m4 dnl $Id: config.m4,v 1.1 2004/08/20 20:16:46 pollita Exp $ dnl config.m4 for extension classkit PHP_ARG_ENABLE(classkit, whether to enable classkit support, [ --enable-classkit Enable classkit support]) if test "$PHP_CLASSKIT" != "no"; then AC_DEFINE(WITH_CLASSKIT,1,[ Classkit support ]) PHP_NEW_EXTENSION(classkit, classkit.c, $ext_shared) fi http://cvs.php.net/co.php/pecl/classkit/config.w32?r=1.1&p=1 Index: pecl/classkit/config.w32 +++ pecl/classkit/config.w32 // $Id: config.w32,v 1.1 2004/08/20 20:16:46 pollita Exp $ // vim:ft=javascript ARG_ENABLE("classkit", "enable classkit support", "no"); if (PHP_CLASSKIT != "no") { AC_DEFINE('WITH_CLASSKIT', 1); EXTENSION("classkit", "classkit.c"); } http://cvs.php.net/co.php/pecl/classkit/package.xml?r=1.1&p=1 Index: pecl/classkit/package.xml +++ pecl/classkit/package.xml <?xml version="1.0" encoding="ISO-8859-1" ?> <package version="1.0"> <name>classkit</name> <summary>Runtime redefinition of class methods</summary> <description> Allows a running script to add, remove, rename, and redefine class methods without reloading. </description> <license>PHP</license> <maintainers> <maintainer> <user>pollita</user> <name>Sara Golemon</name> <email>pollita@php.net</email> <role>lead</role> </maintainer> </maintainers> <release> <version>0.1</version> <state>beta</state> <date>2004-08-20</date> <notes>Initial Release</notes> <filelist> <file role="src" name="config.m4"/> <file role="src" name="config.w32"/> <file role="src" name="classkit.c"/> <file role="src" name="php_classkit.h"/> <file role="doc" name="README"/> </filelist> </release> </package> http://cvs.php.net/co.php/pecl/classkit/php_classkit.h?r=1.1&p=1 Index: pecl/classkit/php_classkit.h +++ pecl/classkit/php_classkit.h /* +----------------------------------------------------------------------+ | PHP Version 5 | +----------------------------------------------------------------------+ | Copyright (c) 1997-2004 The PHP Group | +----------------------------------------------------------------------+ | This source file is subject to version 3.0 of the PHP license, | | that is bundled with this package in the file LICENSE, and is | | available through the world-wide-web at the following url: | | http://www.php.net/license/3_0.txt. | | If you did not receive a copy of the PHP license and are unable to | | obtain it through the world-wide-web, please send a note to | | license@php.net so we can mail you a copy immediately. | +----------------------------------------------------------------------+ | Author: Sara Golemon <pollita@php.net> | +----------------------------------------------------------------------+ */ /* $Id: php_classkit.h,v 1.1 2004/08/20 20:16:46 pollita Exp $ */ #ifndef PHP_CLASSKIT_H #define PHP_CLASSKIT_H extern zend_module_entry classkit_module_entry; #define phpext_classkit_ptr &classkit_module_entry PHP_MINIT_FUNCTION(classkit); PHP_MINFO_FUNCTION(classkit); PHP_FUNCTION(classkit_method_add); PHP_FUNCTION(classkit_method_redefine); PHP_FUNCTION(classkit_method_remove); PHP_FUNCTION(classkit_method_rename); #endif /* PHP_CLASSKIT_H */ /* * Local variables: * tab-width: 4 * c-basic-offset: 4 * End: * vim600: noet sw=4 ts=4 fdm=marker * vim<600: noet sw=4 ts=4 */