From 3cb1db985e6a89fa9d5b590d52d4f04f37d61bd4 Mon Sep 17 00:00:00 2001 From: Derick Rethans Date: Sat, 8 Dec 2001 23:44:34 +0000 Subject: [PATCH] - Added var_export, which shows a representation of a variable, much like var_dump, but in such a way you can use it as PHP code. @- Added var_export, which shows a representation of a variable, much like @ var_dump, but in such a way you can use it as PHP code. (Derick) --- ext/standard/basic_functions.c | 1 + ext/standard/php_var.h | 2 + ext/standard/var.c | 95 ++++++++++++++++++++++++++++++++++ 3 files changed, 98 insertions(+) diff --git a/ext/standard/basic_functions.c b/ext/standard/basic_functions.c index ed7dbccc73..2ecf5adf44 100644 --- a/ext/standard/basic_functions.c +++ b/ext/standard/basic_functions.c @@ -533,6 +533,7 @@ function_entry basic_functions[] = { PHP_FE(unserialize, NULL) PHP_FE(var_dump, NULL) + PHP_FE(var_export, NULL) PHP_FE(print_r, NULL) PHP_FE(register_shutdown_function, NULL) diff --git a/ext/standard/php_var.h b/ext/standard/php_var.h index aa5e003955..bcab5cfe73 100644 --- a/ext/standard/php_var.h +++ b/ext/standard/php_var.h @@ -24,10 +24,12 @@ #include "ext/standard/php_smart_str_public.h" PHP_FUNCTION(var_dump); +PHP_FUNCTION(var_export); PHP_FUNCTION(serialize); PHP_FUNCTION(unserialize); void php_var_dump(zval **struc, int level TSRMLS_DC); +void php_var_export(zval **struc, int level TSRMLS_DC); /* typdef HashTable php_serialize_data_t; */ #define php_serialize_data_t HashTable diff --git a/ext/standard/var.c b/ext/standard/var.c index a765abf975..b37f575161 100644 --- a/ext/standard/var.c +++ b/ext/standard/var.c @@ -136,6 +136,101 @@ PHP_FUNCTION(var_dump) /* }}} */ +/* {{{ php_var_export */ + +static int php_array_element_export(zval **zv, int num_args, va_list args, zend_hash_key *hash_key) +{ + int level; + TSRMLS_FETCH(); + + level = va_arg(args, int); + + if (hash_key->nKeyLength==0) { /* numeric key */ + php_printf("%*c%ld => ", level + 1, ' ', hash_key->h); + } else { /* string key */ + php_printf("%*c'%s' => ", level + 1, ' ', hash_key->arKey); + } + php_var_export(zv, level + 2 TSRMLS_CC); + PUTS (",\n"); + return 0; +} + +void php_var_export(zval **struc, int level TSRMLS_DC) +{ + HashTable *myht; +/* + if (level > 1) { + php_printf("%*c", level - 1, ' '); + } +*/ + switch (Z_TYPE_PP(struc)) { + case IS_BOOL: + php_printf("%s", Z_LVAL_PP(struc) ? "true" : "false"); + break; + case IS_NULL: + php_printf("NULL"); + break; + case IS_LONG: + php_printf("%ld", Z_LVAL_PP(struc)); + break; + case IS_DOUBLE: + php_printf("%.*G", (int) EG(precision), Z_DVAL_PP(struc)); + break; + case IS_STRING: + PUTS ("'"); + PHPWRITE(Z_STRVAL_PP(struc), Z_STRLEN_PP(struc)); + PUTS ("'"); + break; + case IS_ARRAY: + myht = Z_ARRVAL_PP(struc); + goto head_done; + case IS_OBJECT: + myht = Z_OBJPROP_PP(struc); +head_done: + if (level > 1) { + php_printf("\n%*c", level - 1, ' '); + } + PUTS ("array (\n"); + zend_hash_apply_with_arguments(myht, (apply_func_args_t) php_array_element_export, 1, level); + if (level > 1) { + php_printf("%*c", level-1, ' '); + } + PUTS(")"); + break; + default: + PUTS ("NULL"); + break; + } +} + +/* }}} */ + + +/* {{{ proto void var_export(mixed var) + Dumps a string representation of variable to output */ +PHP_FUNCTION(var_export) +{ + zval ***args; + int argc; + int i; + + argc = ZEND_NUM_ARGS(); + + args = (zval ***)emalloc(argc * sizeof(zval **)); + if (ZEND_NUM_ARGS() == 0 || zend_get_parameters_array_ex(argc, args) == FAILURE) { + efree(args); + WRONG_PARAM_COUNT; + } + + for (i=0; i