Skip to content

Commit 437fc12

Browse files
author
sasou2008
committed
优化异常处理;
1 parent 90d2f86 commit 437fc12

File tree

4 files changed

+33
-21
lines changed

4 files changed

+33
-21
lines changed

demo/public/cli.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,14 @@
88
exit('This script is run as CLI with no path?');
99
}
1010

11+
function doException($e) {
12+
echo $e->getMessage();
13+
}
14+
1115
$app = \Gene\Application::getInstance();
1216
$app
1317
->autoload(APP_ROOT)
1418
->load("router.ini.php")
1519
->load("config.ini.php")
16-
->setMode(1, 1)
20+
->setMode(1, 1, "doException")
1721
->run('get', $path);

src/app/application.c

+6-5
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,7 @@ PHP_METHOD(gene_application, autoload) {
458458
PHP_METHOD(gene_application, error) {
459459
zval *callback = NULL, *error_type = NULL, *self = getThis();
460460
long type = 0;
461-
if (zend_parse_parameters(ZEND_NUM_ARGS()TSRMLS_CC, "lzz", &type, &callback, &error_type) == FAILURE) {
461+
if (zend_parse_parameters(ZEND_NUM_ARGS()TSRMLS_CC, "l|zz", &type, &callback, &error_type) == FAILURE) {
462462
return;
463463
}
464464
if (type > 0) {
@@ -473,7 +473,7 @@ PHP_METHOD(gene_application, error) {
473473
PHP_METHOD(gene_application, exception) {
474474
zval *callback = NULL, *error_type = NULL, *self = getThis();
475475
long type = 0;
476-
if (zend_parse_parameters(ZEND_NUM_ARGS()TSRMLS_CC, "lz", &type, &callback) == FAILURE) {
476+
if (zend_parse_parameters(ZEND_NUM_ARGS()TSRMLS_CC, "l|z", &type, &callback) == FAILURE) {
477477
return;
478478
}
479479
if (type > 0) {
@@ -489,14 +489,15 @@ PHP_METHOD(gene_application, exception) {
489489
PHP_METHOD(gene_application, setMode) {
490490
zval *self = getThis();
491491
long error_type = 0, exception_type = 0;
492-
if (zend_parse_parameters(ZEND_NUM_ARGS()TSRMLS_CC, "|ll", &error_type, &exception_type) == FAILURE) {
492+
zval *error_callback = NULL, *ex_callback = NULL;
493+
if (zend_parse_parameters(ZEND_NUM_ARGS()TSRMLS_CC, "|llzz", &error_type, &exception_type, &ex_callback, &error_callback) == FAILURE) {
493494
return;
494495
}
495496
if (error_type > 0) {
496-
gene_exception_error_register(NULL, NULL TSRMLS_CC);
497+
gene_exception_error_register(error_callback, NULL TSRMLS_CC);
497498
}
498499
if (exception_type > 0) {
499-
gene_exception_register(NULL TSRMLS_CC);
500+
gene_exception_register(ex_callback TSRMLS_CC);
500501
}
501502
RETURN_ZVAL(self, 1, 0);
502503
}

src/cache/memcached.c

+9-9
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ PHP_METHOD(gene_memcached, set)
378378
if (object) {
379379
if (Z_TYPE_P(value) == IS_ARRAY || Z_TYPE_P(value) == IS_OBJECT) {
380380
serializer_handler = zend_read_property(gene_memcached_ce, self, ZEND_STRL(GENE_MEM_SERIALIZE), 1, NULL);
381-
if(serializer_handler) {
381+
if(serializer_handler && Z_LVAL_P(serializer_handler) > 0) {
382382
zval ret_string;
383383
if (serialize(value, &ret_string, serializer_handler)) {
384384
#ifdef PHP_WIN32
@@ -387,17 +387,17 @@ PHP_METHOD(gene_memcached, set)
387387
gene_memcached_set (object, key, &ret_string, ttl, return_value);
388388
#endif
389389
zval_ptr_dtor(&ret_string);
390+
return;
390391
}
391-
return;
392392
}
393-
} else {
394-
#ifdef PHP_WIN32
395-
gene_memcache_set (object, key, value, ttl, flag, return_value);
396-
#else
397-
gene_memcached_set (object, key, value, ttl, return_value);
398-
#endif
399-
return;
400393
}
394+
395+
#ifdef PHP_WIN32
396+
gene_memcache_set (object, key, value, ttl, flag, return_value);
397+
#else
398+
gene_memcached_set (object, key, value, ttl, return_value);
399+
#endif
400+
return;
401401
}
402402
RETURN_NULL();
403403
}

src/cache/redis.c

+13-6
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ PHP_METHOD(gene_redis, set) {
297297
if (object) {
298298
if (Z_TYPE_P(value) == IS_ARRAY || Z_TYPE_P(value) == IS_OBJECT) {
299299
serializer_handler = zend_read_property(gene_redis_ce, self, ZEND_STRL(GENE_REDIS_SERIALIZE), 1, NULL);
300-
if (serializer_handler) {
300+
if (serializer_handler && Z_LVAL_P(serializer_handler) > 0) {
301301
zval ret_string;
302302
if (serialize(value, &ret_string, serializer_handler) > 0) {
303303
redis_set(object, key, ttl, &ret_string, return_value);
@@ -309,13 +309,20 @@ PHP_METHOD(gene_redis, set) {
309309
}
310310
}
311311
zval_ptr_dtor(&ret_string);
312+
return;
312313
}
313-
return;
314314
}
315-
} else {
316-
redis_set(object, key, ttl, value, return_value);
317-
return;
318315
}
316+
317+
redis_set(object, key, ttl, value, return_value);
318+
if (EG(exception)) {
319+
if (checkError(EG(exception))) {
320+
EG(exception) = NULL;
321+
initRObj (self, NULL);
322+
redis_set(object, key, ttl, value, return_value);
323+
}
324+
}
325+
return;
319326
}
320327
RETURN_NULL();
321328
}
@@ -369,7 +376,7 @@ GENE_MINIT_FUNCTION(redis)
369376

370377
zend_declare_property_null(gene_redis_ce, ZEND_STRL(GENE_REDIS_CONFIG), ZEND_ACC_PUBLIC TSRMLS_CC);
371378
zend_declare_property_null(gene_redis_ce, ZEND_STRL(GENE_REDIS_OBJ), ZEND_ACC_PUBLIC TSRMLS_CC);
372-
zend_declare_property_long(gene_redis_ce, ZEND_STRL(GENE_REDIS_SERIALIZE), 0, ZEND_ACC_PUBLIC TSRMLS_CC);
379+
zend_declare_property_long(gene_redis_ce, ZEND_STRL(GENE_REDIS_SERIALIZE), 1, ZEND_ACC_PUBLIC TSRMLS_CC);
373380
//
374381
return SUCCESS; // @suppress("Symbol is not resolved")
375382
}

0 commit comments

Comments
 (0)