Hello all, With Val Khokhlov's help:
sapi/cli/php -r "php_check_syntax_string('class foo {}'); var_dump(class_exists('foo'));" bool(false) thanks :-) The original purpose is to valid small code like $foo->bar($foo->bar($blah,$foo,44,"foo",$foo[0].bar)) and avoid regexp ( good example in Smarty_Compiler.class.php#144-164 with limitations) fred ----- Original Message ----- From: "Johannes Schlueter" <[EMAIL PROTECTED]> To: <internals@lists.php.net> Sent: Friday, February 18, 2005 1:17 PM Subject: Re: [PHP-DEV] function proposal - php_check_syntax_string | Hi Fred, | | this patch doesn't just check wether the string has the right syntax but | also compiles it and registers it's functions and classes in the relevant | tables. | | $ sapi/cli/php -r "php_check_syntax_string('class foo {}'); | var_dump(class_exists('foo'));" | bool(true) | | johannes |
--- ext/standard/basic_functions.h.old 2004-03-27 01:50:39.000000000 +0100 +++ ext/standard/basic_functions.h 2005-02-18 03:15:00.000000000 +0100 @@ -83,6 +83,7 @@ PHP_FUNCTION(highlight_string); PHP_FUNCTION(php_strip_whitespace); PHP_FUNCTION(php_check_syntax); +PHP_FUNCTION(php_check_syntax_string); ZEND_API void php_get_highlight_struct(zend_syntax_highlighter_ini *syntax_highlighter_ini); PHP_FUNCTION(ini_get);
--- ext/standard/basic_functions.c.old 2004-11-16 00:16:20.000000000 +0100 +++ ext/standard/basic_functions.c 2005-02-19 14:29:44.011407448 +0100 @@ -33,7 +33,7 @@ #include "ext/session/php_session.h" #include "zend_operators.h" #include "ext/standard/dns.h" -#include "ext/standard/php_uuencode.h" +#include "ext/standard/php_uuencode.h" #ifdef PHP_WIN32 #include "win32/php_win32_globals.h" @@ -481,7 +481,8 @@ PHP_FE(highlight_string, NULL) PHP_FE(php_strip_whitespace, NULL) PHP_FE(php_check_syntax, second_arg_force_ref) - + PHP_FE(php_check_syntax_string, second_arg_force_ref) + PHP_FE(ini_get, NULL) PHP_FE(ini_get_all, NULL) PHP_FE(ini_set, NULL) @@ -2366,6 +2367,90 @@ return; } /* }}} */ +/* {{{ proto bool php_check_syntax_string(string string [, &$error_message]) + Check the syntax of the specified string. */ +PHP_FUNCTION(php_check_syntax_string) +{ + zval *zcode, *zerrors = NULL; + zend_op_array *ops; + zend_uchar original_handle_op_arrays; + int n_old_func = zend_hash_num_elements(CG(function_table)); + int n_old_class = zend_hash_num_elements(CG(class_table)); + int old_errors = PG(display_errors); + int log_errors = PG(log_errors); + int i = 0; + zend_function *zf; + zend_class_entry *zc; + HashPosition pos; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|z", &zcode, &zerrors) == FAILURE){ + RETURN_FALSE; + } + + convert_to_string(zcode); + + PG(log_errors) = PG(display_errors) = 0; + + original_handle_op_arrays = CG(handle_op_arrays); + ops = compile_string(zcode, "php_check_syntax_string" TSRMLS_CC); + CG(handle_op_arrays) = original_handle_op_arrays; + + if (ops) { + + #ifdef ZEND_ENGINE_2 + destroy_op_array(ops TSRMLS_CC); + #else + destroy_op_array(ops); + #endif + efree(ops); + + /* clean-up created functions */ + i = 0; + zend_hash_internal_pointer_reset_ex(CG(function_table), &pos); + while (zend_hash_get_current_data_ex(CG(function_table), (void **)&zf, &pos) == SUCCESS) + { + i++; + if (i > n_old_func && zf->type == ZEND_USER_FUNCTION) { + zend_hash_del(CG(function_table), pos->arKey, pos->nKeyLength); + } + zend_hash_move_forward_ex(CG(function_table), &pos); + } + + /* clean-up created classes */ + i = 0; + zend_hash_internal_pointer_reset_ex(CG(class_table), &pos); + while (zend_hash_get_current_data_ex(CG(class_table), (void **)&zc, &pos) == SUCCESS) + { + #ifdef ZEND_ENGINE_2 + zc = *((zend_class_entry**)zc); + #endif + i++; + if (i > n_old_class && zc->type == ZEND_USER_CLASS) { + zend_hash_del(CG(class_table), pos->arKey, pos->nKeyLength); + } + + zend_hash_move_forward_ex(CG(class_table), &pos); + } + + RETVAL_TRUE; + + } else { + if (zerrors) { + char *error_str; + + zval_dtor(zerrors); + spprintf(&error_str, 0, "%s on line %d", PG(last_error_message), PG(last_error_lineno)); + ZVAL_STRING(zerrors, error_str, 0); + } + RETVAL_FALSE; + } + + PG(display_errors) = old_errors; + PG(log_errors) = log_errors; + + return; + } + /* }}} */ /* {{{ proto bool highlight_string(string string [, bool return] ) Syntax highlight a string or optionally return it */
-- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php