On Tue, 2003-07-01 at 20:27, Timm Friebe wrote: > Hello, > I went ahead and spent some time on the new reflection API. A patch (778 > lines:)) and a demo script are attached.
More to come:
---------------------------------------------------------------------
- Added some necessary efree()'s before an exception is thrown (as
ZEND_DO_THROW returns from the function immediately.
- Added Reflection_Method class
TBI: getDeclaringClass()
- Added Reflection_Class::getModifiers which returns a long
consisting of the bitmask of modifiers which are registered
as constants with the following names:
STATIC
ABSTRACT
FINAL
INTERFACE
ABSTRACT_CLASS
FINAL_CLASS
PUBLIC
PROTECTED
PRIVATE
PPP_MASK
CHANGED
IMPLICIT_PUBLIC
TBDiscussed: Would something like
$modifiers= array('public', 'static')
be cooler? Currently, one would be checking with
$modifiers & STATIC
etc.
- Added Reflection_Class::getConstructor
- Added Reflection_Class::getMethod(string name)
- Added Reflection_Class::getMethods()
---------------------------------------------------------------------
> I couldn't figure out how to remove the following memleaks which
> occur when the reflection_class_factory is called:
I found and eliminated them:)
Btw, the Reflection_Method->invoke() will not work without the patch to
zend_execute_API.c in my previous mail to [EMAIL PROTECTED]
- Timm
Index: Zend/zend_reflection_api.c
===================================================================
RCS file: /repository/ZendEngine2/zend_reflection_api.c,v
retrieving revision 1.6
diff -u -r1.6 zend_reflection_api.c
--- Zend/zend_reflection_api.c 1 Jul 2003 04:25:29 -0000 1.6
+++ Zend/zend_reflection_api.c 1 Jul 2003 21:06:57 -0000
@@ -23,10 +23,28 @@
#include "zend_API.h"
#include "zend_default_classes.h"
#include "zend_operators.h"
+#include "zend_constants.h"
+
+#define METHOD_NOTSTATIC \
+ if (!this_ptr) { \
+ zend_error(E_ERROR, "%s() cannot be called statically", get_active_function_name(TSRMLS_C)); \
+ return; \
+ } \
+
+#define METHOD_NOTSTATIC_NUMPARAMS(c) METHOD_NOTSTATIC \
+ if (ZEND_NUM_ARGS() > c) { \
+ ZEND_WRONG_PARAM_COUNT(); \
+ } \
+
+#define GET_REFLECTION_OBJECT_PTR(target) \
+ intern = (reflection_object *) zend_object_store_get_object(getThis() TSRMLS_CC); \
+ if (intern == NULL || (target = intern->ptr) == NULL) { \
+ zend_error(E_ERROR, "Internal error: Failed to retrieve the reflection object"); \
+ } \
-extern zend_class_entry *default_exception_ptr;
zend_class_entry *reflection_function_ptr;
zend_class_entry *reflection_class_ptr;
+zend_class_entry *reflection_method_ptr;
static zend_object_handlers reflection_object_handlers;
@@ -78,7 +96,7 @@
ALLOC_HASHTABLE(intern->zo.properties);
zend_hash_init(intern->zo.properties, 0, NULL, ZVAL_PTR_DTOR, 0);
retval.handle = zend_objects_store_put(intern, reflection_objects_dtor, reflection_objects_clone TSRMLS_CC);
- retval.handlers = &reflection_object_handlers;
+ retval.handlers = &reflection_object_handlers;
return retval;
}
@@ -94,6 +112,8 @@
return object;
}
+/* {{{ proto Reflection_Function Reflection_Function::__construct(string name)
+ Constructor. Throws an Exception in case the given function does not exist */
ZEND_FUNCTION(reflection_function)
{
zval **name;
@@ -104,226 +124,164 @@
int argc = ZEND_NUM_ARGS();
if (zend_get_parameters_ex(argc, &name) == FAILURE) {
- ZEND_WRONG_PARAM_COUNT();
+ ZEND_WRONG_PARAM_COUNT();
}
object = getThis();
intern = (reflection_object *) zend_object_store_get_object(object TSRMLS_CC);
if (intern == NULL) {
- return;
+ return;
}
convert_to_string_ex(name);
zval_add_ref(name);
zend_hash_update(Z_OBJPROP_P(object), "name", sizeof("name"), (void **) name, sizeof(zval *), NULL);
lcname = zend_str_tolower_dup((const char *)Z_STRVAL_PP(name), (int) Z_STRLEN_PP(name));
if (zend_hash_find(EG(function_table), lcname, (int)(Z_STRLEN_PP(name) + 1), (void **)&fptr) == FAILURE) {
- zval *ex;
- zval *tmp;
- MAKE_STD_ZVAL(ex);
- object_init_ex(ex, default_exception_ptr);
-
- MAKE_STD_ZVAL(tmp);
- ZVAL_STRING(tmp, "Function does not exist", 1);
- zend_hash_update(Z_OBJPROP_P(ex), "message", sizeof("message"), (void **) &tmp, sizeof(zval *), NULL);
- tmp = NULL;
-
- MAKE_STD_ZVAL(tmp);
- ZVAL_STRING(tmp, zend_get_executed_filename(TSRMLS_C), 1);
- zend_hash_update(Z_OBJPROP_P(ex), "file", sizeof("file"), (void **) &tmp, sizeof(zval *), NULL);
- tmp = NULL;
-
- MAKE_STD_ZVAL(tmp);
- ZVAL_LONG(tmp, zend_get_executed_lineno(TSRMLS_C));
- zend_hash_update(Z_OBJPROP_P(ex), "line", sizeof("line"), (void **) &tmp, sizeof(zval *), NULL);
- tmp = NULL;
-
- EG(exception) = ex;
+ efree(lcname);
+ ZEND_DO_THROW("Function does not exist");
}
efree(lcname);
intern->ptr = fptr;
}
+/* }}} */
+/* {{{ proto string Reflection_Function::getName()
+ Returns this function's name */
ZEND_FUNCTION(reflection_function_getname)
{
- if (ZEND_NUM_ARGS() > 0) {
- ZEND_WRONG_PARAM_COUNT();
- }
+ METHOD_NOTSTATIC_NUMPARAMS(0);
_default_get_entry(getThis(), "name", sizeof("name"), return_value TSRMLS_CC);
}
+/* }}} */
+/* {{{ proto bool Reflection_Function::isInternal()
+ Returns whether this is an internal function */
ZEND_FUNCTION(reflection_function_isinternal)
{
- zval *object;
reflection_object *intern;
zend_function *fptr;
- if (ZEND_NUM_ARGS() > 0) {
- ZEND_WRONG_PARAM_COUNT();
- }
- object = getThis();
- intern = (reflection_object *) zend_object_store_get_object(object TSRMLS_CC);
- if (intern == NULL) {
- RETURN_FALSE;
- }
- if ((fptr = intern->ptr) == NULL) {
- RETURN_FALSE;
- }
+ METHOD_NOTSTATIC_NUMPARAMS(0);
+ GET_REFLECTION_OBJECT_PTR(fptr);
RETURN_BOOL(fptr->type == ZEND_INTERNAL_FUNCTION);
}
+/* }}} */
+/* {{{ proto bool Reflection_Function::isUserDefined()
+ Returns whether this is an user-defined function */
ZEND_FUNCTION(reflection_function_isuserdefined)
{
- zval *object;
reflection_object *intern;
zend_function *fptr;
- if (ZEND_NUM_ARGS() > 0) {
- ZEND_WRONG_PARAM_COUNT();
- }
- object = getThis();
- intern = (reflection_object *) zend_object_store_get_object(object TSRMLS_CC);
- if (intern == NULL) {
- return;
- }
- if ((fptr = intern->ptr) == NULL) {
- return;
- }
+ METHOD_NOTSTATIC_NUMPARAMS(0);
+ GET_REFLECTION_OBJECT_PTR(fptr);
RETURN_BOOL(fptr->type == ZEND_USER_FUNCTION);
}
+/* }}} */
+/* {{{ proto string Reflection_Function::getFileName()
+ Returns the filename of the file this function was declared in */
ZEND_FUNCTION(reflection_function_getfilename)
{
- zval *object;
reflection_object *intern;
zend_function *fptr;
- if (ZEND_NUM_ARGS() > 0) {
- ZEND_WRONG_PARAM_COUNT();
- }
- object = getThis();
- intern = (reflection_object *) zend_object_store_get_object(object TSRMLS_CC);
- if (intern == NULL) {
- return;
- }
- if ((fptr = intern->ptr) == NULL) {
- return;
- }
+ METHOD_NOTSTATIC_NUMPARAMS(0);
+ GET_REFLECTION_OBJECT_PTR(fptr);
if (fptr->type == ZEND_USER_FUNCTION) {
RETURN_STRING(fptr->op_array.filename, 1);
}
RETURN_FALSE;
}
+/* }}} */
+/* {{{ proto int Reflection_Function::getStartLine()
+ Returns the line this function's declaration starts at */
ZEND_FUNCTION(reflection_function_getstartline)
{
- zval *object;
reflection_object *intern;
zend_function *fptr;
- if (ZEND_NUM_ARGS() > 0) {
- ZEND_WRONG_PARAM_COUNT();
- }
- object = getThis();
- intern = (reflection_object *) zend_object_store_get_object(object TSRMLS_CC);
- if (intern == NULL) {
- return;
- }
- if ((fptr = intern->ptr) == NULL) {
- return;
- }
+ METHOD_NOTSTATIC_NUMPARAMS(0);
+ GET_REFLECTION_OBJECT_PTR(fptr);
if (fptr->type == ZEND_USER_FUNCTION) {
RETURN_LONG(fptr->op_array.line_start);
}
RETURN_FALSE;
}
+/* }}} */
+/* {{{ proto int Reflection_Function::getEndLine()
+ Returns the line this function's declaration ends at */
ZEND_FUNCTION(reflection_function_getendline)
{
- zval *object;
reflection_object *intern;
zend_function *fptr;
- if (ZEND_NUM_ARGS() > 0) {
- ZEND_WRONG_PARAM_COUNT();
- }
- object = getThis();
- intern = (reflection_object *) zend_object_store_get_object(object TSRMLS_CC);
- if (intern == NULL) {
- RETURN_FALSE;
- }
- if ((fptr = intern->ptr) == NULL) {
- RETURN_FALSE;
- }
+ METHOD_NOTSTATIC_NUMPARAMS(0);
+ GET_REFLECTION_OBJECT_PTR(fptr);
if (fptr->type == ZEND_USER_FUNCTION) {
RETURN_LONG(fptr->op_array.line_end);
}
RETURN_FALSE;
}
+/* }}} */
+/* {{{ proto string Reflection_Function::getDocComment()
+ Returns the doc comment for this function */
ZEND_FUNCTION(reflection_function_getdoccomment)
{
- zval *object;
reflection_object *intern;
zend_function *fptr;
- if (ZEND_NUM_ARGS() > 0) {
- ZEND_WRONG_PARAM_COUNT();
- }
- object = getThis();
- intern = (reflection_object *) zend_object_store_get_object(object TSRMLS_CC);
- if (intern == NULL) {
- RETURN_FALSE;
- }
- if ((fptr = intern->ptr) == NULL) {
- RETURN_FALSE;
- }
+ METHOD_NOTSTATIC_NUMPARAMS(0);
+ GET_REFLECTION_OBJECT_PTR(fptr);
if (fptr->type == ZEND_USER_FUNCTION && fptr->op_array.doc_comment) {
RETURN_STRINGL(fptr->op_array.doc_comment, fptr->op_array.doc_comment_len, 1);
}
RETURN_FALSE;
}
+/* }}} */
+/* {{{ proto array Reflection_Function::getStaticVariables()
+ Returns an associative array containing this function's static variables and their values */
ZEND_FUNCTION(reflection_function_getstaticvariables)
{
- zval *object, *tmp_copy;
+ zval *tmp_copy;
reflection_object *intern;
zend_function *fptr;
- if (ZEND_NUM_ARGS() > 0) {
- ZEND_WRONG_PARAM_COUNT();
- }
- object = getThis();
- intern = (reflection_object *) zend_object_store_get_object(object TSRMLS_CC);
- if (intern == NULL) {
- RETURN_FALSE;
- }
- if ((fptr = intern->ptr) == NULL || fptr->op_array.static_variables == NULL) {
- RETURN_FALSE;
- }
+ METHOD_NOTSTATIC_NUMPARAMS(0);
+ GET_REFLECTION_OBJECT_PTR(fptr);
+
+ /* Return an empty array in case no static variables exist */
array_init(return_value);
- zend_hash_copy(Z_ARRVAL_P(return_value), fptr->op_array.static_variables, (copy_ctor_func_t) zval_add_ref, (void *) &tmp_copy, sizeof(zval *));
+ if (fptr->op_array.static_variables != NULL) {
+ zend_hash_copy(Z_ARRVAL_P(return_value), fptr->op_array.static_variables, (copy_ctor_func_t) zval_add_ref, (void *) &tmp_copy, sizeof(zval *));
+ }
}
+/* }}} */
+/* {{{ proto mixed Reflection_Function::invoke(mixed* args)
+ Invokes the function */
ZEND_FUNCTION(reflection_function_invoke)
{
- zval *object, *retval_ptr;
+ zval *retval_ptr;
zval ***params;
reflection_object *intern;
zend_function *fptr;
int argc = ZEND_NUM_ARGS();
- object = getThis();
+ METHOD_NOTSTATIC;
+ GET_REFLECTION_OBJECT_PTR(fptr);
+
params = safe_emalloc(sizeof(zval **), argc, 0);
if (zend_get_parameters_array_ex(argc, params) == FAILURE) {
efree(params);
RETURN_FALSE;
}
- intern = (reflection_object *) zend_object_store_get_object(object TSRMLS_CC);
- if (intern == NULL || (fptr = intern->ptr) == NULL) {
- efree(params);
- RETURN_FALSE;
- }
if (fast_call_user_function(EG(function_table), NULL, NULL,
&retval_ptr, argc, params,
1, NULL, &fptr TSRMLS_CC) == SUCCESS && retval_ptr) {
@@ -331,12 +289,14 @@
}
efree(params);
}
+/* }}} */
void reflection_class_factory(zend_class_entry *ce, zval *object TSRMLS_DC)
{
reflection_object *intern;
zval *name;
- ALLOC_ZVAL(name);
+
+ MAKE_STD_ZVAL(name);
ZVAL_STRINGL(name, ce->name, ce->name_length, 1);
reflection_instanciate(reflection_class_ptr, object TSRMLS_CC);
intern = (reflection_object *) zend_object_store_get_object(object TSRMLS_CC);
@@ -344,6 +304,209 @@
zend_hash_update(Z_OBJPROP_P(object), "name", sizeof("name"), (void **) &name, sizeof(zval *), NULL);
}
+void reflection_method_factory(zend_class_entry *ce, zend_function *method, zval *object TSRMLS_DC)
+{
+ reflection_object *intern;
+ zval *name;
+ zval *classname;
+
+ MAKE_STD_ZVAL(name);
+ ZVAL_STRING(name, method->common.function_name, 1);
+ MAKE_STD_ZVAL(classname);
+ ZVAL_STRINGL(classname, ce->name, ce->name_length, 1);
+ reflection_instanciate(reflection_method_ptr, object TSRMLS_CC);
+ intern = (reflection_object *) zend_object_store_get_object(object TSRMLS_CC);
+ intern->ptr = method;
+ zend_hash_update(Z_OBJPROP_P(object), "name", sizeof("name"), (void **) &name, sizeof(zval *), NULL);
+ zend_hash_update(Z_OBJPROP_P(object), "class", sizeof("class"), (void **) &classname, sizeof(zval *), NULL);
+}
+
+/* {{{ proto Reflection_Class Reflection_Method::__construct(mixed class, string name)
+ Constructor. Throws an Exception in case the given method does not exist */
+ZEND_FUNCTION(reflection_method)
+{
+ zval **name, **class;
+ zval *classname;
+ zval *object;
+ reflection_object *intern;
+ char *lcname;
+ zend_class_entry **pce;
+ zend_class_entry *ce;
+ zend_function *mptr;
+
+ int argc = ZEND_NUM_ARGS();
+ if (zend_get_parameters_ex(argc, &class, &name) == FAILURE) {
+ ZEND_WRONG_PARAM_COUNT();
+ }
+
+ object = getThis();
+ intern = (reflection_object *) zend_object_store_get_object(object TSRMLS_CC);
+ if (intern == NULL) {
+ return;
+ }
+
+ /* Find the class entry */
+ switch (Z_TYPE_PP(class)) {
+ case IS_STRING:
+ convert_to_string_ex(class);
+ lcname = zend_str_tolower_dup((const char *)Z_STRVAL_PP(class), (int) Z_STRLEN_PP(class));
+ if (zend_hash_find(EG(class_table), lcname, (int)(Z_STRLEN_PP(class) + 1), (void **) &pce) == FAILURE) {
+ efree(lcname);
+ ZEND_DO_THROW("Class does not exist");
+ /* returns out of this function */
+ }
+ ce = *pce;
+ efree(lcname);
+ break;
+
+ case IS_OBJECT:
+ ce = Z_OBJCE_PP(class);
+ break;
+
+ default:
+ ZEND_DO_THROW("The parameter class is expected to be either a string or an object");
+ /* returns out of this function */
+ }
+
+ MAKE_STD_ZVAL(classname);
+ ZVAL_STRINGL(classname, ce->name, ce->name_length, 1);
+ zend_hash_update(Z_OBJPROP_P(object), "class", sizeof("class"), (void **) &classname, sizeof(zval *), NULL);
+
+ convert_to_string_ex(name);
+ zval_add_ref(name);
+ zend_hash_update(Z_OBJPROP_P(object), "name", sizeof("name"), (void **) name, sizeof(zval *), NULL);
+ lcname = zend_str_tolower_dup((const char *)Z_STRVAL_PP(name), (int) Z_STRLEN_PP(name));
+
+ if (zend_hash_find(&ce->function_table, lcname, (int)(Z_STRLEN_PP(name) + 1), (void **) &mptr) == FAILURE) {
+ efree(lcname);
+ ZEND_DO_THROW("Method does not exist");
+ /* returns out of this function */
+ }
+ efree(lcname);
+ intern->ptr = mptr;
+}
+/* }}} */
+
+/* {{{ proto mixed Reflection_Method::invoke(stdclass object, mixed* args)
+ Invokes the function */
+ZEND_FUNCTION(reflection_method_invoke)
+{
+ zval *retval_ptr;
+ zval ***params;
+ reflection_object *intern;
+ zend_function *mptr;
+ int argc = ZEND_NUM_ARGS();
+
+ METHOD_NOTSTATIC;
+ GET_REFLECTION_OBJECT_PTR(mptr);
+
+ params = safe_emalloc(sizeof(zval **), argc, 0);
+ if (zend_get_parameters_array_ex(argc, params) == FAILURE) {
+ efree(params);
+ RETURN_FALSE;
+ }
+
+ if (argc < 1 || Z_TYPE_PP(params[0]) != IS_OBJECT) {
+ zend_error(E_WARNING, "First parameter is expected to be an object");
+ efree(params);
+ RETURN_FALSE;
+ }
+
+ if (fast_call_user_function(&mptr->common.scope->function_table, params[0], NULL,
+ &retval_ptr, argc - 1, params+ 1,
+ 1, NULL, &mptr TSRMLS_CC) == SUCCESS && retval_ptr) {
+ COPY_PZVAL_TO_ZVAL(*return_value, retval_ptr);
+ }
+ efree(params);
+}
+/* }}} */
+
+static void _method_check_flag(INTERNAL_FUNCTION_PARAMETERS, int mask)
+{
+ reflection_object *intern;
+ zend_function *mptr;
+
+ METHOD_NOTSTATIC_NUMPARAMS(0);
+ GET_REFLECTION_OBJECT_PTR(mptr);
+ RETURN_BOOL(mptr->common.fn_flags & mask);
+}
+
+/* {{{ proto bool Reflection_Method::isFinal()
+ Returns whether this method is final */
+ZEND_FUNCTION(reflection_method_isfinal)
+{
+ _method_check_flag(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_ACC_FINAL);
+}
+/* }}} */
+
+/* {{{ proto bool Reflection_Method::isAbstract()
+ Returns whether this method is abstract */
+ZEND_FUNCTION(reflection_method_isabstract)
+{
+ _method_check_flag(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_ACC_ABSTRACT);
+}
+/* }}} */
+
+/* {{{ proto bool Reflection_Method::isPublic()
+ Returns whether this method is public */
+ZEND_FUNCTION(reflection_method_ispublic)
+{
+ _method_check_flag(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_ACC_PUBLIC);
+}
+/* }}} */
+
+/* {{{ proto bool Reflection_Method::isPrivate()
+ Returns whether this method is private */
+ZEND_FUNCTION(reflection_method_isprivate)
+{
+ _method_check_flag(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_ACC_PRIVATE);
+}
+/* }}} */
+
+/* {{{ proto bool Reflection_Method::isProtected()
+ Returns whether this method is protected */
+ZEND_FUNCTION(reflection_method_isprotected)
+{
+ _method_check_flag(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_ACC_PROTECTED);
+}
+/* }}} */
+
+/* {{{ proto bool Reflection_Method::isStatic()
+ Returns whether this method is static */
+ZEND_FUNCTION(reflection_method_isstatic)
+{
+ _method_check_flag(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_ACC_STATIC);
+}
+/* }}} */
+
+/* {{{ proto bool Reflection_Method::isConstructor()
+ Returns whether this method is the constructor */
+ZEND_FUNCTION(reflection_method_isconstructor)
+{
+ reflection_object *intern;
+ zend_function *mptr;
+
+ METHOD_NOTSTATIC_NUMPARAMS(0);
+ GET_REFLECTION_OBJECT_PTR(mptr);
+ RETURN_BOOL(mptr->common.scope->constructor == mptr);
+}
+/* }}} */
+
+/* {{{ proto bool Reflection_Method::getModifiers()
+ Returns the access modifiers for this method */
+ZEND_FUNCTION(reflection_method_getmodifiers)
+{
+ reflection_object *intern;
+ zend_function *mptr;
+
+ METHOD_NOTSTATIC_NUMPARAMS(0);
+ GET_REFLECTION_OBJECT_PTR(mptr);
+ RETURN_LONG(mptr->common.fn_flags);
+}
+/* }}} */
+
+/* {{{ proto Reflection_Class Reflection_Class::__construct(string name)
+ Constructor. Throws an Exception in case the given class does not exist */
ZEND_FUNCTION(reflection_class)
{
zval **name;
@@ -354,244 +517,359 @@
int argc = ZEND_NUM_ARGS();
if (zend_get_parameters_ex(argc, &name) == FAILURE) {
- ZEND_WRONG_PARAM_COUNT();
+ ZEND_WRONG_PARAM_COUNT();
}
object = getThis();
intern = (reflection_object *) zend_object_store_get_object(object TSRMLS_CC);
if (intern == NULL) {
- return;
+ return;
}
convert_to_string_ex(name);
zval_add_ref(name);
zend_hash_update(Z_OBJPROP_P(object), "name", sizeof("name"), (void **) name, sizeof(zval *), NULL);
lcname = zend_str_tolower_dup((const char *)Z_STRVAL_PP(name), (int) Z_STRLEN_PP(name));
if (zend_hash_find(EG(class_table), lcname, (int)(Z_STRLEN_PP(name) + 1), (void **)&ce) == FAILURE) {
- zval *ex;
- zval *tmp;
- MAKE_STD_ZVAL(ex);
- object_init_ex(ex, default_exception_ptr);
-
- MAKE_STD_ZVAL(tmp);
- ZVAL_STRING(tmp, "Function does not exist", 1);
- zend_hash_update(Z_OBJPROP_P(ex), "message", sizeof("message"), (void **) &tmp, sizeof(zval *), NULL);
- tmp = NULL;
-
- MAKE_STD_ZVAL(tmp);
- ZVAL_STRING(tmp, zend_get_executed_filename(TSRMLS_C), 1);
- zend_hash_update(Z_OBJPROP_P(ex), "file", sizeof("file"), (void **) &tmp, sizeof(zval *), NULL);
- tmp = NULL;
-
- MAKE_STD_ZVAL(tmp);
- ZVAL_LONG(tmp, zend_get_executed_lineno(TSRMLS_C));
- zend_hash_update(Z_OBJPROP_P(ex), "line", sizeof("line"), (void **) &tmp, sizeof(zval *), NULL);
- tmp = NULL;
-
- EG(exception) = ex;
+ efree(lcname);
+ ZEND_DO_THROW("Class does not exist");
}
efree(lcname);
intern->ptr = *ce;
}
+/* }}} */
+/* {{{ proto string Reflection_Class::getName()
+ Returns the class' name */
ZEND_FUNCTION(reflection_class_getname)
{
- if (ZEND_NUM_ARGS() > 0) {
- ZEND_WRONG_PARAM_COUNT();
- }
+ METHOD_NOTSTATIC_NUMPARAMS(0);
_default_get_entry(getThis(), "name", sizeof("name"), return_value TSRMLS_CC);
}
+/* }}} */
+/* {{{ proto bool Reflection_Class::isInternal()
+ Returns whether this class is an internal class */
ZEND_FUNCTION(reflection_class_isinternal)
{
- zval *object;
reflection_object *intern;
zend_class_entry *ce;
- if (ZEND_NUM_ARGS() > 0) {
- ZEND_WRONG_PARAM_COUNT();
- }
- object = getThis();
- intern = (reflection_object *) zend_object_store_get_object(object TSRMLS_CC);
- if (intern == NULL || (ce = intern->ptr) == NULL) {
- RETURN_FALSE;
- }
+ METHOD_NOTSTATIC_NUMPARAMS(0);
+ GET_REFLECTION_OBJECT_PTR(ce);
RETURN_BOOL(ce->type == ZEND_INTERNAL_CLASS);
}
+/* }}} */
+/* {{{ proto bool Reflection_Class::isUserDefined()
+ Returns whether this class is user-defined */
ZEND_FUNCTION(reflection_class_isuserdefined)
{
- zval *object;
reflection_object *intern;
zend_class_entry *ce;
- if (ZEND_NUM_ARGS() > 0) {
- ZEND_WRONG_PARAM_COUNT();
- }
- object = getThis();
- intern = (reflection_object *) zend_object_store_get_object(object TSRMLS_CC);
- if (intern == NULL || (ce = intern->ptr) == NULL) {
- RETURN_FALSE;
- }
+ METHOD_NOTSTATIC_NUMPARAMS(0);
+ GET_REFLECTION_OBJECT_PTR(ce);
RETURN_BOOL(ce->type == ZEND_USER_CLASS);
}
+/* }}} */
+/* {{{ proto string Reflection_Class::getFileName()
+ Returns the filename of the file this class was declared in */
ZEND_FUNCTION(reflection_class_getfilename)
{
- zval *object;
reflection_object *intern;
zend_class_entry *ce;
- if (ZEND_NUM_ARGS() > 0) {
- ZEND_WRONG_PARAM_COUNT();
- }
- object = getThis();
- intern = (reflection_object *) zend_object_store_get_object(object TSRMLS_CC);
- if (intern == NULL || (ce = intern->ptr) == NULL) {
- RETURN_FALSE;
- }
+ METHOD_NOTSTATIC_NUMPARAMS(0);
+ GET_REFLECTION_OBJECT_PTR(ce);
if (ce->type == ZEND_USER_CLASS) {
RETURN_STRING(ce->filename, 1);
}
RETURN_FALSE;
}
+/* }}} */
+/* {{{ proto int Reflection_Class::getStartLine()
+ Returns the line this class' declaration starts at */
ZEND_FUNCTION(reflection_class_getstartline)
{
- zval *object;
reflection_object *intern;
zend_class_entry *ce;
- if (ZEND_NUM_ARGS() > 0) {
- ZEND_WRONG_PARAM_COUNT();
- }
- object = getThis();
- intern = (reflection_object *) zend_object_store_get_object(object TSRMLS_CC);
- if (intern == NULL || ((ce = intern->ptr) == NULL)) {
- RETURN_FALSE;
- }
+ METHOD_NOTSTATIC_NUMPARAMS(0);
+ GET_REFLECTION_OBJECT_PTR(ce);
if (ce->type == ZEND_USER_FUNCTION) {
RETURN_LONG(ce->line_start);
}
RETURN_FALSE;
}
+/* }}} */
+/* {{{ proto int Reflection_Class::getEndLine()
+ Returns the line this class' declaration ends at */
ZEND_FUNCTION(reflection_class_getendline)
{
- zval *object;
reflection_object *intern;
zend_class_entry *ce;
- if (ZEND_NUM_ARGS() > 0) {
- ZEND_WRONG_PARAM_COUNT();
- }
- object = getThis();
- intern = (reflection_object *) zend_object_store_get_object(object TSRMLS_CC);
- if (intern == NULL || (ce = intern->ptr) == NULL) {
- RETURN_FALSE;
- }
+ METHOD_NOTSTATIC_NUMPARAMS(0);
+ GET_REFLECTION_OBJECT_PTR(ce);
if (ce->type == ZEND_USER_CLASS) {
RETURN_LONG(ce->line_end);
}
RETURN_FALSE;
}
+/* }}} */
+/* {{{ proto string Reflection_Class::getDocComment()
+ Returns the doc comment for this class */
ZEND_FUNCTION(reflection_class_getdoccomment)
{
- zval *object;
reflection_object *intern;
zend_class_entry *ce;
- if (ZEND_NUM_ARGS() > 0) {
- ZEND_WRONG_PARAM_COUNT();
- }
- object = getThis();
- intern = (reflection_object *) zend_object_store_get_object(object TSRMLS_CC);
- if (intern == NULL && (ce = intern->ptr) == NULL) {
- RETURN_FALSE;
- }
+ METHOD_NOTSTATIC_NUMPARAMS(0);
+ GET_REFLECTION_OBJECT_PTR(ce);
if (ce->type == ZEND_USER_CLASS && ce->doc_comment) {
RETURN_STRINGL(ce->doc_comment, ce->doc_comment_len, 1);
}
RETURN_FALSE;
}
+/* }}} */
-ZEND_FUNCTION(reflection_class_getconstants)
+/* {{{ proto PHP_Method Reflection_Class::getConstructor()
+ Returns the class' constructor if there is one, NULL otherwise */
+ZEND_FUNCTION(reflection_class_getconstructor)
{
- zval *object, *tmp_copy;
reflection_object *intern;
zend_class_entry *ce;
-
- if (ZEND_NUM_ARGS() > 0) {
+
+ METHOD_NOTSTATIC_NUMPARAMS(0);
+ GET_REFLECTION_OBJECT_PTR(ce);
+
+ if (ce->constructor) {
+ reflection_method_factory(ce, ce->constructor, return_value TSRMLS_CC);
+ } else {
+ RETURN_NULL();
+ }
+}
+/* }}} */
+
+/* {{{ proto PHP_Method Reflection_Class::getMethod(string name)
+ Returns the class' method specified by it's name */
+ZEND_FUNCTION(reflection_class_getmethod)
+{
+ reflection_object *intern;
+ zend_class_entry *ce;
+ zend_function *mptr;
+ zval **name;
+ int argc = ZEND_NUM_ARGS();
+
+ METHOD_NOTSTATIC;
+ if (zend_get_parameters_ex(argc, &name) == FAILURE) {
ZEND_WRONG_PARAM_COUNT();
}
- object = getThis();
- intern = (reflection_object *) zend_object_store_get_object(object TSRMLS_CC);
- if (intern == NULL || (ce = intern->ptr) == NULL) {
- RETURN_FALSE;
+
+ GET_REFLECTION_OBJECT_PTR(ce);
+
+ if (zend_hash_find(&ce->function_table, Z_STRVAL_PP(name), Z_STRLEN_PP(name) + 1, (void**) &mptr) == SUCCESS) {
+ reflection_method_factory(ce, mptr, return_value TSRMLS_CC);
+ } else {
+ RETURN_NULL();
+ }
+}
+/* }}} */
+
+static int _addmethod(zend_function *mptr, int num_args, va_list args, zend_hash_key *hash_key)
+{
+ zval *method;
+ zend_class_entry *ce = *va_arg(args, zend_class_entry**);
+
+ ALLOC_ZVAL(method);
+ reflection_method_factory(ce, mptr, method TSRMLS_CC);
+ add_next_index_zval(va_arg(args, zval*), method);
+ return 0;
+}
+
+/* {{{ proto PHP_Method[] Reflection_Class::getMethods()
+ Returns an array of this class' methods */
+ZEND_FUNCTION(reflection_class_getmethods)
+{
+ reflection_object *intern;
+ zend_class_entry *ce;
+ zval **name;
+ int argc = ZEND_NUM_ARGS();
+
+ METHOD_NOTSTATIC;
+ if (zend_get_parameters_ex(argc, &name) == FAILURE) {
+ ZEND_WRONG_PARAM_COUNT();
}
+
+ GET_REFLECTION_OBJECT_PTR(ce);
+
+ array_init(return_value);
+ zend_hash_apply_with_arguments(&ce->function_table, (apply_func_args_t) _addmethod, 2, &ce, return_value);
+}
+/* }}} */
+
+/* {{{ proto array Reflection_Class::getConstants()
+ Returns an associative array containing this class' constants and their values */
+ZEND_FUNCTION(reflection_class_getconstants)
+{
+ zval *tmp_copy;
+ reflection_object *intern;
+ zend_class_entry *ce;
+
+ METHOD_NOTSTATIC_NUMPARAMS(0);
+ GET_REFLECTION_OBJECT_PTR(ce);
array_init(return_value);
zend_hash_copy(Z_ARRVAL_P(return_value), &ce->constants_table, (copy_ctor_func_t) zval_add_ref, (void *) &tmp_copy, sizeof(zval *));
}
+/* }}} */
+/* {{{ proto mixed Reflection_Class::getConstant(string name)
+ Returns the class' constant specified by its name */
ZEND_FUNCTION(reflection_class_getconstant)
{
- zval *object;
reflection_object *intern;
zend_class_entry *ce;
zval **value;
zval **name;
int argc = ZEND_NUM_ARGS();
+ METHOD_NOTSTATIC;
if (zend_get_parameters_ex(argc, &name) == FAILURE) {
ZEND_WRONG_PARAM_COUNT();
}
- object = getThis();
- intern = (reflection_object *) zend_object_store_get_object(object TSRMLS_CC);
- if (intern == NULL || (ce = intern->ptr) == NULL) {
- RETURN_FALSE;
- }
+ GET_REFLECTION_OBJECT_PTR(ce);
if (zend_hash_find(&ce->constants_table, Z_STRVAL_PP(name), Z_STRLEN_PP(name) + 1, (void **) &value) == FAILURE) {
RETURN_FALSE;
}
*return_value = **value;
zval_copy_ctor(return_value);
}
+/* }}} */
+
+static void _check_flag(INTERNAL_FUNCTION_PARAMETERS, int mask)
+{
+ reflection_object *intern;
+ zend_class_entry *ce;
+
+ METHOD_NOTSTATIC_NUMPARAMS(0);
+ GET_REFLECTION_OBJECT_PTR(ce);
+ RETURN_BOOL(ce->ce_flags & mask);
+}
+/* {{{ proto bool Reflection_Class::isInterface()
+ Returns whether this is an interface or a class */
ZEND_FUNCTION(reflection_class_isinterface)
{
- zval *object;
+ _check_flag(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_ACC_INTERFACE);
+}
+/* }}} */
+
+/* {{{ proto bool Reflection_Class::isFinal()
+ Returns whether this class is final */
+ZEND_FUNCTION(reflection_class_isfinal)
+{
+ _check_flag(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_ACC_FINAL_CLASS);
+}
+/* }}} */
+
+/* {{{ proto bool Reflection_Class::isAbstract()
+ Returns whether this class is abstract */
+ZEND_FUNCTION(reflection_class_isabstract)
+{
+ _check_flag(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_ACC_ABSTRACT_CLASS);
+}
+/* }}} */
+
+/* {{{ proto bool Reflection_Class::getModifiers()
+ Returns the access modifiers for this class */
+ZEND_FUNCTION(reflection_class_getmodifiers)
+{
reflection_object *intern;
zend_class_entry *ce;
- if (ZEND_NUM_ARGS() > 0) {
- ZEND_WRONG_PARAM_COUNT();
+ METHOD_NOTSTATIC_NUMPARAMS(0);
+ GET_REFLECTION_OBJECT_PTR(ce);
+ RETURN_LONG(ce->ce_flags);
+}
+/* }}} */
+
+/* {{{ proto bool Reflection_Class::isInstance(stdclass object)
+ Returns whether the given object is an instance of this class */
+ZEND_FUNCTION(reflection_class_isinstance)
+{
+ reflection_object *intern;
+ zend_class_entry *ce;
+ zval *object;
+
+ METHOD_NOTSTATIC;
+ if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "o", &object) == FAILURE) {
+ return;
}
- object = getThis();
- intern = (reflection_object *) zend_object_store_get_object(object TSRMLS_CC);
- if (intern == NULL || (ce = intern->ptr) == NULL) {
+ GET_REFLECTION_OBJECT_PTR(ce);
+ RETURN_BOOL(ce == Z_OBJCE_P(object));
+}
+/* }}} */
+
+/* {{{ proto stdclass Reflection_Class::newInstance(mixed* args)
+ Returns an instance of this class */
+ZEND_FUNCTION(reflection_class_newinstance)
+{
+ zval *retval_ptr;
+ reflection_object *intern;
+ zend_class_entry *ce;
+ int argc = ZEND_NUM_ARGS();
+
+ METHOD_NOTSTATIC;
+ GET_REFLECTION_OBJECT_PTR(ce);
+
+ object_init_ex(return_value, ce);
+
+ /* Run the constructor if there is one */
+ if (ce->constructor) {
+ zval ***params;
+
+ params = safe_emalloc(sizeof(zval **), argc, 0);
+ if (zend_get_parameters_array_ex(argc, params) == FAILURE) {
+ efree(params);
RETURN_FALSE;
+ }
+
+ if (fast_call_user_function(EG(function_table), &return_value, NULL,
+ &retval_ptr, argc, params,
+ 1, NULL, &ce->constructor TSRMLS_CC) == FAILURE) {
+ efree(params);
+ zend_error(E_WARNING, "Invokation of %s's constructor failed\n", ce->name);
+ RETURN_NULL();
+ }
+ if (retval_ptr) {
+ zval_ptr_dtor(&retval_ptr);
+ }
+ efree(params);
}
- RETURN_BOOL(ce->type & ZEND_ACC_INTERFACE);
}
+/* }}} */
+/* {{{ proto Reflection_Class[] Reflection_Class::getInterfaces()
+ Returns an array of interfaces this class implements */
ZEND_FUNCTION(reflection_class_getinterfaces)
{
- zval *object;
reflection_object *intern;
zend_class_entry *ce;
- if (ZEND_NUM_ARGS() > 0) {
- ZEND_WRONG_PARAM_COUNT();
- }
- object = getThis();
- intern = (reflection_object *) zend_object_store_get_object(object TSRMLS_CC);
- if (intern == NULL || (ce = intern->ptr) == NULL) {
- RETURN_FALSE;
- }
+ METHOD_NOTSTATIC_NUMPARAMS(0);
+ GET_REFLECTION_OBJECT_PTR(ce);
+
+ /* Return an empty array if this class implements no interfaces */
+ array_init(return_value);
+
if (ce->num_interfaces) {
int i;
- array_init(return_value);
+
for(i=0; i < ce->num_interfaces; i++) {
zval *interface;
ALLOC_ZVAL(interface);
@@ -600,25 +878,25 @@
}
}
}
+/* }}} */
+/* {{{ proto Reflection_Class Reflection_Class::getParentClass()
+ Returns the class' parent class, or, if none exists, FALSE */
ZEND_FUNCTION(reflection_class_getparentclass)
{
- zval *object;
reflection_object *intern;
zend_class_entry *ce;
- if (ZEND_NUM_ARGS() > 0) {
- ZEND_WRONG_PARAM_COUNT();
- }
- object = getThis();
- intern = (reflection_object *) zend_object_store_get_object(object TSRMLS_CC);
- if (intern && (ce = intern->ptr) && ce->parent) {
+ METHOD_NOTSTATIC_NUMPARAMS(0);
+ GET_REFLECTION_OBJECT_PTR(ce);
+
+ if (ce->parent) {
reflection_class_factory(ce->parent, return_value TSRMLS_CC);
- }
- else {
+ } else {
RETURN_FALSE;
}
}
+/* }}} */
static zend_function_entry reflection_function_functions[] = {
ZEND_FE(reflection_function, NULL)
@@ -634,6 +912,20 @@
{NULL, NULL, NULL}
};
+static zend_function_entry reflection_method_functions[] = {
+ ZEND_FE(reflection_method, NULL)
+ ZEND_NAMED_FE(ispublic, ZEND_FN(reflection_method_ispublic), NULL)
+ ZEND_NAMED_FE(isprivate, ZEND_FN(reflection_method_isprivate), NULL)
+ ZEND_NAMED_FE(isprotected, ZEND_FN(reflection_method_isprotected), NULL)
+ ZEND_NAMED_FE(isabstract, ZEND_FN(reflection_method_isabstract), NULL)
+ ZEND_NAMED_FE(isfinal, ZEND_FN(reflection_method_isfinal), NULL)
+ ZEND_NAMED_FE(isstatic, ZEND_FN(reflection_method_isstatic), NULL)
+ ZEND_NAMED_FE(getmodifiers, ZEND_FN(reflection_method_getmodifiers), NULL)
+ ZEND_NAMED_FE(isconstructor, ZEND_FN(reflection_method_isconstructor), NULL)
+ ZEND_NAMED_FE(invoke, ZEND_FN(reflection_method_invoke), NULL)
+ {NULL, NULL, NULL}
+};
+
static zend_function_entry reflection_class_functions[] = {
ZEND_FE(reflection_class, NULL)
ZEND_NAMED_FE(getname, ZEND_FN(reflection_class_getname), NULL)
@@ -643,23 +935,51 @@
ZEND_NAMED_FE(getstartline, ZEND_FN(reflection_class_getstartline), NULL)
ZEND_NAMED_FE(getendline, ZEND_FN(reflection_class_getendline), NULL)
ZEND_NAMED_FE(getdoccomment, ZEND_FN(reflection_class_getdoccomment), NULL)
+ ZEND_NAMED_FE(getconstructor, ZEND_FN(reflection_class_getconstructor), NULL)
+ ZEND_NAMED_FE(getmethod, ZEND_FN(reflection_class_getmethod), NULL)
+ ZEND_NAMED_FE(getmethods, ZEND_FN(reflection_class_getmethods), NULL)
ZEND_NAMED_FE(getconstants, ZEND_FN(reflection_class_getconstants), NULL)
ZEND_NAMED_FE(getconstant, ZEND_FN(reflection_class_getconstant), NULL)
ZEND_NAMED_FE(getinterfaces, ZEND_FN(reflection_class_getinterfaces), NULL)
ZEND_NAMED_FE(isinterface, ZEND_FN(reflection_class_isinterface), NULL)
+ ZEND_NAMED_FE(isabstract, ZEND_FN(reflection_class_isabstract), NULL)
+ ZEND_NAMED_FE(isfinal, ZEND_FN(reflection_class_isfinal), NULL)
+ ZEND_NAMED_FE(getmodifiers, ZEND_FN(reflection_class_getmodifiers), NULL)
+ ZEND_NAMED_FE(isinstance, ZEND_FN(reflection_class_isinstance), NULL)
+ ZEND_NAMED_FE(newinstance, ZEND_FN(reflection_class_newinstance), NULL)
ZEND_NAMED_FE(getparentclass, ZEND_FN(reflection_class_getparentclass), NULL)
{NULL, NULL, NULL}
};
ZEND_API void zend_register_reflection_api(TSRMLS_D) {
zend_class_entry _reflection_entry;
+
memcpy(&reflection_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
+
INIT_CLASS_ENTRY(_reflection_entry, "reflection_function", reflection_function_functions);
_reflection_entry.create_object = reflection_objects_new;
reflection_function_ptr = zend_register_internal_class(&_reflection_entry TSRMLS_CC);
+
+ INIT_CLASS_ENTRY(_reflection_entry, "reflection_method", reflection_method_functions);
+ _reflection_entry.create_object = reflection_objects_new;
+ reflection_method_ptr = zend_register_internal_class_ex(&_reflection_entry, reflection_function_ptr, NULL TSRMLS_CC);
+
INIT_CLASS_ENTRY(_reflection_entry, "reflection_class", reflection_class_functions);
_reflection_entry.create_object = reflection_objects_new;
reflection_class_ptr = zend_register_internal_class(&_reflection_entry TSRMLS_CC);
+
+ REGISTER_MAIN_LONG_CONSTANT("STATIC", sizeof("STATIC"), ZEND_ACC_STATIC);
+ REGISTER_MAIN_LONG_CONSTANT("ABSTRACT", sizeof("ABSTRACT"), ZEND_ACC_ABSTRACT);
+ REGISTER_MAIN_LONG_CONSTANT("FINAL", sizeof("FINAL"), ZEND_ACC_FINAL);
+ REGISTER_MAIN_LONG_CONSTANT("INTERFACE", sizeof("INTERFACE"), ZEND_ACC_INTERFACE);
+ REGISTER_MAIN_LONG_CONSTANT("ABSTRACT_CLASS", sizeof("ABSTRACT_CLASS"), ZEND_ACC_ABSTRACT_CLASS);
+ REGISTER_MAIN_LONG_CONSTANT("FINAL_CLASS", sizeof("FINAL_CLASS"), ZEND_ACC_FINAL_CLASS);
+ REGISTER_MAIN_LONG_CONSTANT("PUBLIC", sizeof("PUBLIC"), ZEND_ACC_PUBLIC);
+ REGISTER_MAIN_LONG_CONSTANT("PROTECTED", sizeof("PROTECTED"), ZEND_ACC_PROTECTED);
+ REGISTER_MAIN_LONG_CONSTANT("PRIVATE", sizeof("PRIVATE"), ZEND_ACC_PRIVATE);
+ REGISTER_MAIN_LONG_CONSTANT("PPP_MASK", sizeof("PPP_MASK"), ZEND_ACC_PPP_MASK);
+ REGISTER_MAIN_LONG_CONSTANT("CHANGED", sizeof("CHANGED"), ZEND_ACC_CHANGED);
+ REGISTER_MAIN_LONG_CONSTANT("IMPLICIT_PUBLIC", sizeof("IMPLICIT_PUBLIC"), ZEND_ACC_IMPLICIT_PUBLIC);
}
/*
reflection.php
Description: application/php
-- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php
