This source file includes following definitions.
- ResourceBundle_object_destroy
- ResourceBundle_object_create
- resourcebundle_ctor
- ZEND_BEGIN_ARG_INFO_EX
- PHP_FUNCTION
- resourcebundle_array_fetch
- resourcebundle_array_get
- ZEND_BEGIN_ARG_INFO_EX
- resourcebundle_array_count
- ZEND_BEGIN_ARG_INFO_EX
- ZEND_BEGIN_ARG_INFO_EX
- ZEND_BEGIN_ARG_INFO_EX
- ZEND_BEGIN_ARG_INFO_EX
- resourcebundle_register_class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 #include <stdlib.h>
18 #include <unicode/ures.h>
19 #include <unicode/uenum.h>
20
21 #include <zend.h>
22 #include <Zend/zend_exceptions.h>
23 #include <Zend/zend_interfaces.h>
24 #include <php.h>
25
26 #include "php_intl.h"
27 #include "intl_data.h"
28 #include "intl_common.h"
29
30 #include "resourcebundle/resourcebundle.h"
31 #include "resourcebundle/resourcebundle_iterator.h"
32 #include "resourcebundle/resourcebundle_class.h"
33
34 zend_class_entry *ResourceBundle_ce_ptr = NULL;
35
36 static zend_object_handlers ResourceBundle_object_handlers;
37
38
39 static void ResourceBundle_object_destroy( zend_object *object )
40 {
41 ResourceBundle_object *rb = php_intl_resourcebundle_fetch_object(object);
42
43
44 intl_error_reset( INTL_DATA_ERROR_P(rb) );
45
46 if (rb->me) {
47 ures_close( rb->me );
48 }
49 if (rb->child) {
50 ures_close( rb->child );
51 }
52 }
53
54
55
56 static zend_object *ResourceBundle_object_create( zend_class_entry *ce )
57 {
58 ResourceBundle_object *rb;
59
60 rb = ecalloc( 1, sizeof(ResourceBundle_object) + zend_object_properties_size(ce));
61
62 zend_object_std_init( &rb->zend, ce );
63 object_properties_init( &rb->zend, ce);
64
65 intl_error_init( INTL_DATA_ERROR_P(rb) );
66 rb->me = NULL;
67 rb->child = NULL;
68
69 rb->zend.handlers = &ResourceBundle_object_handlers;
70
71 return &rb->zend;
72 }
73
74
75
76 static int resourcebundle_ctor(INTERNAL_FUNCTION_PARAMETERS, zend_bool is_constructor)
77 {
78 const char *bundlename;
79 size_t bundlename_len = 0;
80 const char *locale;
81 size_t locale_len = 0;
82 zend_bool fallback = 1;
83 int zpp_flags = is_constructor ? ZEND_PARSE_PARAMS_THROW : 0;
84
85 zval *object = return_value;
86 ResourceBundle_object *rb = Z_INTL_RESOURCEBUNDLE_P( object );
87
88 intl_error_reset( NULL );
89
90 if( zend_parse_parameters_ex( zpp_flags, ZEND_NUM_ARGS(), "s!s!|b",
91 &locale, &locale_len, &bundlename, &bundlename_len, &fallback ) == FAILURE )
92 {
93 intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
94 "resourcebundle_ctor: unable to parse input parameters", 0 );
95 return FAILURE;
96 }
97
98 INTL_CHECK_LOCALE_LEN_OR_FAILURE(locale_len);
99
100 if (locale == NULL) {
101 locale = intl_locale_get_default();
102 }
103
104 if (fallback) {
105 rb->me = ures_open(bundlename, locale, &INTL_DATA_ERROR_CODE(rb));
106 } else {
107 rb->me = ures_openDirect(bundlename, locale, &INTL_DATA_ERROR_CODE(rb));
108 }
109
110 INTL_CTOR_CHECK_STATUS(rb, "resourcebundle_ctor: Cannot load libICU resource bundle");
111
112 if (!fallback && (INTL_DATA_ERROR_CODE(rb) == U_USING_FALLBACK_WARNING ||
113 INTL_DATA_ERROR_CODE(rb) == U_USING_DEFAULT_WARNING)) {
114 char *pbuf;
115 intl_errors_set_code(NULL, INTL_DATA_ERROR_CODE(rb));
116 spprintf(&pbuf, 0, "resourcebundle_ctor: Cannot load libICU resource "
117 "'%s' without fallback from %s to %s",
118 bundlename ? bundlename : "(default data)", locale,
119 ures_getLocaleByType(
120 rb->me, ULOC_ACTUAL_LOCALE, &INTL_DATA_ERROR_CODE(rb)));
121 intl_errors_set_custom_msg(INTL_DATA_ERROR_P(rb), pbuf, 1);
122 efree(pbuf);
123 return FAILURE;
124 }
125
126 return SUCCESS;
127 }
128
129
130
131 ZEND_BEGIN_ARG_INFO_EX( arginfo_resourcebundle___construct, 0, 0, 2 )
132 ZEND_ARG_INFO( 0, locale )
133 ZEND_ARG_INFO( 0, bundlename )
134 ZEND_ARG_INFO( 0, fallback )
135 ZEND_END_ARG_INFO()
136
137
138
139
140
141 PHP_METHOD( ResourceBundle, __construct )
142 {
143 zend_error_handling error_handling;
144
145 zend_replace_error_handling(EH_THROW, IntlException_ce_ptr, &error_handling);
146 return_value = getThis();
147 if (resourcebundle_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1) == FAILURE) {
148 if (!EG(exception)) {
149 zend_throw_exception(IntlException_ce_ptr, "Constructor failed", 0);
150 }
151 }
152 zend_restore_error_handling(&error_handling);
153 }
154
155
156
157
158
159 PHP_FUNCTION( resourcebundle_create )
160 {
161 object_init_ex( return_value, ResourceBundle_ce_ptr );
162 if (resourcebundle_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0) == FAILURE) {
163 zval_ptr_dtor(return_value);
164 RETURN_NULL();
165 }
166 }
167
168
169
170 static void resourcebundle_array_fetch(zval *object, zval *offset, zval *return_value, int fallback)
171 {
172 int32_t meindex = 0;
173 char * mekey = NULL;
174 zend_bool is_numeric = 0;
175 char *pbuf;
176 ResourceBundle_object *rb;
177
178 intl_error_reset( NULL );
179 RESOURCEBUNDLE_METHOD_FETCH_OBJECT;
180
181 if(Z_TYPE_P(offset) == IS_LONG) {
182 is_numeric = 1;
183 meindex = (int32_t)Z_LVAL_P(offset);
184 rb->child = ures_getByIndex( rb->me, meindex, rb->child, &INTL_DATA_ERROR_CODE(rb) );
185 } else if(Z_TYPE_P(offset) == IS_STRING) {
186 mekey = Z_STRVAL_P(offset);
187 rb->child = ures_getByKey(rb->me, mekey, rb->child, &INTL_DATA_ERROR_CODE(rb) );
188 } else {
189 intl_errors_set(INTL_DATA_ERROR_P(rb), U_ILLEGAL_ARGUMENT_ERROR,
190 "resourcebundle_get: index should be integer or string", 0);
191 RETURN_NULL();
192 }
193
194 intl_error_set_code( NULL, INTL_DATA_ERROR_CODE(rb) );
195 if (U_FAILURE(INTL_DATA_ERROR_CODE(rb))) {
196 if (is_numeric) {
197 spprintf( &pbuf, 0, "Cannot load resource element %d", meindex );
198 } else {
199 spprintf( &pbuf, 0, "Cannot load resource element '%s'", mekey );
200 }
201 intl_errors_set_custom_msg( INTL_DATA_ERROR_P(rb), pbuf, 1 );
202 efree(pbuf);
203 RETURN_NULL();
204 }
205
206 if (!fallback && (INTL_DATA_ERROR_CODE(rb) == U_USING_FALLBACK_WARNING || INTL_DATA_ERROR_CODE(rb) == U_USING_DEFAULT_WARNING)) {
207 UErrorCode icuerror;
208 const char * locale = ures_getLocaleByType( rb->me, ULOC_ACTUAL_LOCALE, &icuerror );
209 if (is_numeric) {
210 spprintf( &pbuf, 0, "Cannot load element %d without fallback from to %s", meindex, locale );
211 } else {
212 spprintf( &pbuf, 0, "Cannot load element '%s' without fallback from to %s", mekey, locale );
213 }
214 intl_errors_set_custom_msg( INTL_DATA_ERROR_P(rb), pbuf, 1 );
215 efree(pbuf);
216 RETURN_NULL();
217 }
218
219 resourcebundle_extract_value( return_value, rb );
220 }
221
222
223
224 zval *resourcebundle_array_get(zval *object, zval *offset, int type, zval *rv)
225 {
226 if(offset == NULL) {
227 php_error( E_ERROR, "Cannot apply [] to ResourceBundle object" );
228 }
229 ZVAL_NULL(rv);
230 resourcebundle_array_fetch(object, offset, rv, 1);
231 return rv;
232 }
233
234
235
236 ZEND_BEGIN_ARG_INFO_EX( arginfo_resourcebundle_get, 0, 0, 1 )
237 ZEND_ARG_INFO( 0, index )
238 ZEND_ARG_INFO( 0, fallback )
239 ZEND_END_ARG_INFO()
240
241
242
243
244
245
246 PHP_FUNCTION( resourcebundle_get )
247 {
248 zend_bool fallback = 1;
249 zval * offset;
250 zval * object;
251
252 if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Oz|b", &object, ResourceBundle_ce_ptr, &offset, &fallback ) == FAILURE) {
253 intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
254 "resourcebundle_get: unable to parse input params", 0);
255 RETURN_FALSE;
256 }
257
258 resourcebundle_array_fetch(object, offset, return_value, fallback);
259 }
260
261
262
263 int resourcebundle_array_count(zval *object, zend_long *count)
264 {
265 ResourceBundle_object *rb;
266 RESOURCEBUNDLE_METHOD_FETCH_OBJECT_NO_CHECK;
267
268 if (rb->me == NULL) {
269 intl_errors_set(&rb->error, U_ILLEGAL_ARGUMENT_ERROR,
270 "Found unconstructed ResourceBundle", 0);
271 return 0;
272 }
273
274 *count = ures_getSize( rb->me );
275
276 return SUCCESS;
277 }
278
279
280
281 ZEND_BEGIN_ARG_INFO_EX( arginfo_resourcebundle_count, 0, 0, 0 )
282 ZEND_END_ARG_INFO()
283
284
285
286
287
288
289 PHP_FUNCTION( resourcebundle_count )
290 {
291 int32_t len;
292 RESOURCEBUNDLE_METHOD_INIT_VARS;
293
294 if( zend_parse_method_parameters( ZEND_NUM_ARGS(), getThis(), "O", &object, ResourceBundle_ce_ptr ) == FAILURE ) {
295 intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
296 "resourcebundle_count: unable to parse input params", 0);
297 RETURN_FALSE;
298 }
299
300 RESOURCEBUNDLE_METHOD_FETCH_OBJECT;
301
302 len = ures_getSize( rb->me );
303 RETURN_LONG( len );
304 }
305
306
307 ZEND_BEGIN_ARG_INFO_EX( arginfo_resourcebundle_getlocales, 0, 0, 1 )
308 ZEND_ARG_INFO( 0, bundlename )
309 ZEND_END_ARG_INFO()
310
311
312
313
314
315
316 PHP_FUNCTION( resourcebundle_locales )
317 {
318 char * bundlename;
319 size_t bundlename_len = 0;
320 const char * entry;
321 int entry_len;
322 UEnumeration *icuenum;
323 UErrorCode icuerror = U_ZERO_ERROR;
324
325 intl_errors_reset( NULL );
326
327 if( zend_parse_parameters(ZEND_NUM_ARGS(), "s", &bundlename, &bundlename_len ) == FAILURE )
328 {
329 intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
330 "resourcebundle_locales: unable to parse input params", 0);
331 RETURN_FALSE;
332 }
333
334 if(bundlename_len == 0) {
335
336 bundlename = NULL;
337 }
338
339 icuenum = ures_openAvailableLocales( bundlename, &icuerror );
340 INTL_CHECK_STATUS(icuerror, "Cannot fetch locales list");
341
342 uenum_reset( icuenum, &icuerror );
343 INTL_CHECK_STATUS(icuerror, "Cannot iterate locales list");
344
345 array_init( return_value );
346 while ((entry = uenum_next( icuenum, &entry_len, &icuerror ))) {
347 add_next_index_stringl( return_value, (char *) entry, entry_len);
348 }
349 uenum_close( icuenum );
350 }
351
352
353
354 ZEND_BEGIN_ARG_INFO_EX( arginfo_resourcebundle_get_error_code, 0, 0, 0 )
355 ZEND_END_ARG_INFO()
356
357
358
359
360
361
362 PHP_FUNCTION( resourcebundle_get_error_code )
363 {
364 RESOURCEBUNDLE_METHOD_INIT_VARS;
365
366 if( zend_parse_method_parameters( ZEND_NUM_ARGS(), getThis(), "O",
367 &object, ResourceBundle_ce_ptr ) == FAILURE )
368 {
369 intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
370 "resourcebundle_get_error_code: unable to parse input params", 0 );
371 RETURN_FALSE;
372 }
373
374 rb = Z_INTL_RESOURCEBUNDLE_P( object );
375
376 RETURN_LONG(INTL_DATA_ERROR_CODE(rb));
377 }
378
379
380
381 ZEND_BEGIN_ARG_INFO_EX( arginfo_resourcebundle_get_error_message, 0, 0, 0 )
382 ZEND_END_ARG_INFO()
383
384
385
386
387
388
389 PHP_FUNCTION( resourcebundle_get_error_message )
390 {
391 zend_string* message = NULL;
392 RESOURCEBUNDLE_METHOD_INIT_VARS;
393
394 if( zend_parse_method_parameters( ZEND_NUM_ARGS(), getThis(), "O",
395 &object, ResourceBundle_ce_ptr ) == FAILURE )
396 {
397 intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
398 "resourcebundle_get_error_message: unable to parse input params", 0 );
399 RETURN_FALSE;
400 }
401
402 rb = Z_INTL_RESOURCEBUNDLE_P( object );
403 message = intl_error_get_message(INTL_DATA_ERROR_P(rb));
404 RETURN_STR(message);
405 }
406
407
408
409
410
411 static zend_function_entry ResourceBundle_class_functions[] = {
412 PHP_ME( ResourceBundle, __construct, arginfo_resourcebundle___construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR )
413 ZEND_NAMED_ME( create, ZEND_FN( resourcebundle_create ), arginfo_resourcebundle___construct, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC )
414 ZEND_NAMED_ME( get, ZEND_FN(resourcebundle_get), arginfo_resourcebundle_get, ZEND_ACC_PUBLIC )
415 ZEND_NAMED_ME( count, ZEND_FN(resourcebundle_count), arginfo_resourcebundle_count, ZEND_ACC_PUBLIC )
416 ZEND_NAMED_ME( getLocales, ZEND_FN(resourcebundle_locales), arginfo_resourcebundle_getlocales, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC )
417 ZEND_NAMED_ME( getErrorCode, ZEND_FN(resourcebundle_get_error_code), arginfo_resourcebundle_get_error_code, ZEND_ACC_PUBLIC )
418 ZEND_NAMED_ME( getErrorMessage, ZEND_FN(resourcebundle_get_error_message), arginfo_resourcebundle_get_error_message, ZEND_ACC_PUBLIC )
419 PHP_FE_END
420 };
421
422
423
424
425
426 void resourcebundle_register_class( void )
427 {
428 zend_class_entry ce;
429
430 INIT_CLASS_ENTRY( ce, "ResourceBundle", ResourceBundle_class_functions );
431
432 ce.create_object = ResourceBundle_object_create;
433 ce.get_iterator = resourcebundle_get_iterator;
434
435 ResourceBundle_ce_ptr = zend_register_internal_class( &ce );
436
437 if( !ResourceBundle_ce_ptr )
438 {
439 zend_error(E_ERROR, "Failed to register ResourceBundle class");
440 return;
441 }
442
443 ResourceBundle_object_handlers = std_object_handlers;
444 ResourceBundle_object_handlers.offset = XtOffsetOf(ResourceBundle_object, zend);
445 ResourceBundle_object_handlers.clone_obj = NULL;
446 ResourceBundle_object_handlers.dtor_obj = ResourceBundle_object_destroy;
447 ResourceBundle_object_handlers.read_dimension = resourcebundle_array_get;
448 ResourceBundle_object_handlers.count_elements = resourcebundle_array_count;
449
450 zend_class_implements(ResourceBundle_ce_ptr, 1, zend_ce_traversable);
451 }
452
453
454
455
456
457
458
459
460
461