]> granicus.if.org Git - php/commitdiff
- Added the ability for the intl exception to throw exceptions when a global error...
authorGustavo André dos Santos Lopes <cataphract@php.net>
Sun, 8 Jan 2012 18:41:53 +0000 (18:41 +0000)
committerGustavo André dos Santos Lopes <cataphract@php.net>
Sun, 8 Jan 2012 18:41:53 +0000 (18:41 +0000)
ext/intl/intl_error.c
ext/intl/intl_error.h
ext/intl/php_intl.c
ext/intl/php_intl.h
ext/intl/tests/ini_use_exceptions_basic.phpt [new file with mode: 0644]

index 9c2e13dfd5c47fef161b5184449911d4efa60ac3..2c7066b0817a1c8bb493a5169641967be1f7579e 100755 (executable)
 #endif
 
 #include <php.h>
+#include <zend_exceptions.h>
 
 #include "php_intl.h"
 #include "intl_error.h"
 
 ZEND_EXTERN_MODULE_GLOBALS( intl )
 
+static zend_class_entry *IntlException_ce_ptr;
+
 /* {{{ intl_error* intl_g_error_get()
  * Return global error structure.
  */
@@ -102,8 +105,11 @@ void intl_error_set_custom_msg( intl_error* err, char* msg, int copyMsg TSRMLS_D
        if( !msg )
                return;
 
-       if(!err && INTL_G(error_level)) {
-               php_error_docref(NULL TSRMLS_CC, INTL_G(error_level), "%s", msg);               
+       if( !err ) {
+               if( INTL_G( error_level ) )
+                       php_error_docref( NULL TSRMLS_CC, INTL_G( error_level ), "%s", msg );
+               if( INTL_G( use_exceptions ) )
+                       zend_throw_exception_ex( IntlException_ce_ptr, 0 TSRMLS_CC, "%s", msg );
        }
        if( !err && !( err = intl_g_error_get( TSRMLS_C ) ) )
                return;
@@ -223,6 +229,21 @@ void intl_errors_set_code( intl_error* err, UErrorCode err_code TSRMLS_DC )
 }
 /* }}} */
 
+void intl_register_IntlException_class( TSRMLS_D )
+{
+       zend_class_entry ce,
+                                        *default_exception_ce;
+
+       default_exception_ce = zend_exception_get_default( TSRMLS_C );
+
+       /* Create and register 'IntlException' class. */
+       INIT_CLASS_ENTRY_EX( ce, "IntlException", sizeof( "IntlException" ) - 1, NULL );
+       IntlException_ce_ptr = zend_register_internal_class_ex( &ce,
+               default_exception_ce, NULL TSRMLS_CC );
+       IntlException_ce_ptr->create_object = default_exception_ce->create_object;
+}
+/* }}} */
+
 /*
  * Local variables:
  * tab-width: 4
index 3adae8547419955ddd1c74bb4b643e997e73d2b8..b5000a15de66735b8d92a21d3e33175648fb2195 100755 (executable)
@@ -44,4 +44,7 @@ void        intl_errors_set_custom_msg( intl_error* err, char* msg, int copyMsg
 void        intl_errors_set_code( intl_error* err, UErrorCode err_code TSRMLS_DC );
 void        intl_errors_set( intl_error* err, UErrorCode code, char* msg, int copyMsg TSRMLS_DC );
 
+// exported to be called on extension MINIT
+void           intl_register_IntlException_class( TSRMLS_D );
+
 #endif // INTL_ERROR_H
index efe0ddd242319a0803daf045e2f839ce6fdf780a..514750f733af1e58529f2a2dc73de51c0624231a 100755 (executable)
@@ -545,7 +545,7 @@ zend_function_entry intl_functions[] = {
 PHP_INI_BEGIN()
     STD_PHP_INI_ENTRY(LOCALE_INI_NAME, NULL, PHP_INI_ALL, OnUpdateStringUnempty, default_locale, zend_intl_globals, intl_globals)
     STD_PHP_INI_ENTRY("intl.error_level", "0", PHP_INI_ALL, OnUpdateLong, error_level, zend_intl_globals, intl_globals)
-
+       STD_PHP_INI_ENTRY("intl.use_exceptions", "0", PHP_INI_ALL, OnUpdateBool, use_exceptions, zend_intl_globals, intl_globals)
 PHP_INI_END()
 /* }}} */
 
@@ -653,6 +653,10 @@ PHP_MINIT_FUNCTION( intl )
        /* Expose Spoofchecker constants to PHP scripts */
        spoofchecker_register_constants( INIT_FUNC_ARGS_PASSTHRU );
 #endif
+
+       /* Register 'IntlException' PHP class */
+       intl_register_IntlException_class( TSRMLS_C );
+
        /* Global error handling. */
        intl_error_init( NULL TSRMLS_CC );
 
index 4ede069e2a04118f85fd17e23227cb27dce4ca88..38f61ad8ac4d0f4237872f3d14623cc8c375e114 100755 (executable)
@@ -46,6 +46,7 @@ ZEND_BEGIN_MODULE_GLOBALS(intl)
        UBreakIterator* grapheme_iterator;
        intl_error g_error;
        long error_level;
+       zend_bool use_exceptions;
 ZEND_END_MODULE_GLOBALS(intl)
 
 /* Macro to access request-wide global variables. */
diff --git a/ext/intl/tests/ini_use_exceptions_basic.phpt b/ext/intl/tests/ini_use_exceptions_basic.phpt
new file mode 100644 (file)
index 0000000..d1fb89f
--- /dev/null
@@ -0,0 +1,21 @@
+--TEST--\r
+intl.use_exceptions INI setting\r
+--SKIPIF--\r
+<?php if( !extension_loaded( 'intl' ) ) print 'skip'; ?>\r
+--FILE--\r
+<?php\r
+ini_set("intl.use_exceptions", true);\r
+$t = transliterator_create('any-hex');\r
+try {\r
+       var_dump($t->transliterate('a', 3));\r
+} catch (IntlException $intlE) {\r
+       var_dump($intlE->getMessage());\r
+}\r
+ini_set("intl.use_exceptions", false);\r
+ini_set("intl.error_level", E_NOTICE);\r
+var_dump($t->transliterate('a', 3));\r
+--EXPECTF--\r
+string(130) "transliterator_transliterate: Neither "start" nor the "end" arguments can exceed the number of UTF-16 code units (in this case, 1)"\r
+\r
+Notice: Transliterator::transliterate(): transliterator_transliterate: Neither "start" nor the "end" arguments can exceed the number of UTF-16 code units (in this case, 1) in %s on line %d\r
+bool(false)
\ No newline at end of file