Skip to content

Commit

Permalink
Added Yaf_Dispatcher's get_properties, and get_gc for fix cycle refer…
Browse files Browse the repository at this point in the history
…ence
  • Loading branch information
laruence committed Apr 11, 2020
1 parent cb957d8 commit 548e82b
Show file tree
Hide file tree
Showing 9 changed files with 321 additions and 121 deletions.
2 changes: 2 additions & 0 deletions package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
- Added Yaf_Request::cleanParams()
- Added Yaf_Controller::getName(), Yaf_Action::getControllerName()
- Added Yaf_Dispatcher::getDefaultModule(), Yaf_Dispatcher::getDefaultController() and Yaf_Dispatcher::getDefaultAction()
- Added Yaf_Application::getInstance(), which is alias of Yaf_Application:app()
- Rmoved all lead underline for fake protected property name(examing by var_dump)
- Fixed bug that protected method of Bootstrap get executed
- Yaf_View_Simple is final class now, custom view engin should implements Yaf_View_Interface
Expand Down Expand Up @@ -214,6 +215,7 @@
<file name="098.phpt" role="test" />
<file name="099.phpt" role="test" />
<file name="100.phpt" role="test" />
<file name="101.phpt" role="test" />
<file name="build.inc" role="test" />
<file name="bug61493.phpt" role="test" />
<file name="bug63381.phpt" role="test" />
Expand Down
39 changes: 39 additions & 0 deletions tests/014.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,45 @@ Yaf_Application Object

[dispatcher:protected] => Yaf_Dispatcher Object
(
[auto_render:protected] => 1
[instant_flush:protected] =>
[return_response:protected] =>
[request:protected] => Yaf_Request_Http Object
(
[method] => CLI
[module] =>
[controller] =>
[action] =>
[uri:protected] =>
[base_uri:protected] =>
[dispatched:protected] =>
[routed:protected] =>
[language:protected] =>
[params:protected] => Array
(
)

)

[response:protected] =>
[router:protected] => Yaf_Router Object
(
[routes:protected] => Array
(
[_default] => Yaf_Route_Static Object
(
)

)

[current:protected] =>
)

[view:protected] =>
[plugins:protected] => Array
(
)

)

[modules:protected] => Array
Expand Down
1 change: 1 addition & 0 deletions tests/095.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ object(Yaf_Application)#1 (13) {
}
["dispatcher:protected"]=>
object(Yaf_Dispatcher)#%d (%d) {
%A
}
["modules:protected"]=>
array(1) {
Expand Down
29 changes: 29 additions & 0 deletions tests/101.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
--TEST--
Check for various cycle references
--SKIPIF--
<?php if (!extension_loaded("yaf")) print "skip"; ?>
--INI--
yaf.use_spl_autoload=0
yaf.lowcase_path=0
yaf.use_namespace=0
--FILE--
<?php
$foo = new Stdclass();
$foo->request = new Yaf_Request_Simple();
$foo->request->setParam("foo", $foo);
unset($foo);

$foo = new Stdclass();
$foo->config = new Yaf_Config_Simple(array(), 0);
$foo->config->foo = $foo;
unset($foo);

$foo = new Stdclass();
$foo->request = new Yaf_Request_Simple();
$app = new Yaf_Application(["yaf" => ["directory" => __DIR__]]);
$app->getDispatcher()->setRequest($foo->request);
unset($foo);
?>
okey
--EXPECT--
okey
141 changes: 76 additions & 65 deletions yaf_application.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,80 @@ ZEND_BEGIN_ARG_INFO_EX(yaf_application_setappdir_arginfo, 0, 0, 1)
ZEND_END_ARG_INFO()
/* }}} */

static zend_object* yaf_application_new(zend_class_entry *ce) /* {{{ */ {
yaf_application_object *app = emalloc(sizeof(yaf_application_object) + zend_object_properties_size(ce));

memset(app, 0, XtOffsetOf(yaf_application_object, std));
app->std.handlers = &yaf_application_obj_handlers;
zend_object_std_init(&app->std, ce);

return &app->std;
}
/* }}} */

static void yaf_application_free(zend_object *object) /* {{{ */ {
yaf_application_object *app = yaf_application_instance();

if ((app != php_yaf_application_fetch_object(object)) || !app->env) {
zend_object_std_dtor(object);
return;
}

zend_string_release(app->env);

OBJ_RELEASE(Z_OBJ(app->config));
OBJ_RELEASE(Z_OBJ(app->dispatcher));

zend_string_release(app->default_module);
zend_string_release(app->default_controller);
zend_string_release(app->default_action);
if (app->library) {
zend_string_release(app->library);
}
zend_string_release(app->directory);

if (app->ext) {
zend_string_release(app->ext);
}
if (app->bootstrap) {
zend_string_release(app->bootstrap);
}
if (app->view_ext) {
zend_string_release(app->view_ext);
}
if (app->base_uri) {
zend_string_release(app->base_uri);
}
if (app->err_msg) {
zend_string_release(app->err_msg);
}
if (app->modules) {
if (GC_DELREF(app->modules) == 0) {
GC_REMOVE_FROM_BUFFER(app->modules);
zend_array_destroy(app->modules);
}
}
if (app->properties) {
if (GC_DELREF(app->properties) == 0) {
GC_REMOVE_FROM_BUFFER(app->properties);
zend_array_destroy(app->properties);
}
}

zend_object_std_dtor(object);
}
/* }}} */

static HashTable *yaf_application_get_gc(zval *object, zval **table, int *n) /* {{{ */ {
yaf_application_object *app = Z_YAFAPPOBJ_P(object);

*table = &app->dispatcher;
*n = 2;

return NULL;
}
/* }}} */

static HashTable *yaf_application_get_properties(zval *object) /* {{{ */ {
zval rv;
HashTable *ht;
Expand Down Expand Up @@ -599,70 +673,6 @@ static int yaf_application_parse_option(yaf_application_object *app) /* {{{ */ {
}
/* }}} */

static zend_object* yaf_application_new(zend_class_entry *ce) /* {{{ */ {
yaf_application_object *app = emalloc(sizeof(yaf_application_object) + zend_object_properties_size(ce));

memset(app, 0, XtOffsetOf(yaf_application_object, std));
app->std.handlers = &yaf_application_obj_handlers;
zend_object_std_init(&app->std, ce);

return &app->std;
}
/* }}} */

static void yaf_application_free(zend_object *object) /* {{{ */ {
yaf_application_object *app = yaf_application_instance();

if ((app != php_yaf_application_fetch_object(object)) || !app->env) {
zend_object_std_dtor(object);
return;
}

zend_string_release(app->env);

OBJ_RELEASE(Z_OBJ(app->config));
OBJ_RELEASE(Z_OBJ(app->dispatcher));

zend_string_release(app->default_module);
zend_string_release(app->default_controller);
zend_string_release(app->default_action);
if (app->library) {
zend_string_release(app->library);
}
zend_string_release(app->directory);

if (app->ext) {
zend_string_release(app->ext);
}
if (app->bootstrap) {
zend_string_release(app->bootstrap);
}
if (app->view_ext) {
zend_string_release(app->view_ext);
}
if (app->base_uri) {
zend_string_release(app->base_uri);
}
if (app->err_msg) {
zend_string_release(app->err_msg);
}
if (app->modules) {
if (GC_DELREF(app->modules) == 0) {
GC_REMOVE_FROM_BUFFER(app->modules);
zend_array_destroy(app->modules);
}
}
if (app->properties) {
if (GC_DELREF(app->properties) == 0) {
GC_REMOVE_FROM_BUFFER(app->properties);
zend_array_destroy(app->properties);
}
}

zend_object_std_dtor(object);
}
/* }}} */

/** {{{ proto Yaf_Application::__construct(mixed $config, string $environ = YAF_G(environ_name))
*/
PHP_METHOD(yaf_application, __construct) {
Expand Down Expand Up @@ -990,6 +1000,7 @@ zend_function_entry yaf_application_methods[] = {
PHP_ME(yaf_application, getLastErrorNo, yaf_application_void_arginfo, ZEND_ACC_PUBLIC)
PHP_ME(yaf_application, getLastErrorMsg, yaf_application_void_arginfo, ZEND_ACC_PUBLIC)
PHP_ME(yaf_application, clearLastError, yaf_application_void_arginfo, ZEND_ACC_PUBLIC)
PHP_MALIAS(yaf_application, getInstance, app, yaf_application_app_arginfo, ZEND_ACC_PUBLIC)
{NULL, NULL, NULL}
};
/* }}} */
Expand All @@ -1009,7 +1020,7 @@ YAF_STARTUP_FUNCTION(application) {
memcpy(&yaf_application_obj_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
yaf_application_obj_handlers.offset = XtOffsetOf(yaf_application_object, std);
yaf_application_obj_handlers.clone_obj = NULL;
yaf_application_obj_handlers.get_gc = NULL;
yaf_application_obj_handlers.get_gc = yaf_application_get_gc;
yaf_application_obj_handlers.free_obj = yaf_application_free;
yaf_application_obj_handlers.get_properties = yaf_application_get_properties;
yaf_application_obj_handlers.read_property = yaf_application_read_property;
Expand Down
12 changes: 11 additions & 1 deletion yaf_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,16 @@ ZEND_BEGIN_ARG_INFO_EX(yaf_config_isset_arginfo, 0, 0, 1)
ZEND_END_ARG_INFO()
/* }}} */

static HashTable *yaf_config_get_gc(zval *object, zval **table, int *n) /* {{{ */ {
yaf_config_object *conf = Z_YAFCONFIGOBJ_P(object);;

*table = NULL;
*n = 0;

return conf->config;
}
/* }}} */

static HashTable *yaf_config_get_properties(zval *object) /* {{{ */ {
zval rv;
HashTable *ht;
Expand Down Expand Up @@ -412,7 +422,7 @@ YAF_STARTUP_FUNCTION(config) {
yaf_config_obj_handlers.offset = XtOffsetOf(yaf_config_object, std);
yaf_config_obj_handlers.free_obj = yaf_config_object_free;
yaf_config_obj_handlers.clone_obj = NULL;
yaf_config_obj_handlers.get_gc = NULL;
yaf_config_obj_handlers.get_gc = yaf_config_get_gc;
yaf_config_obj_handlers.get_properties = yaf_config_get_properties;

#if defined(HAVE_SPL) && PHP_VERSION_ID < 70200
Expand Down
Loading

0 comments on commit 548e82b

Please sign in to comment.