Skip to content

Commit

Permalink
refactor yaf_registry
Browse files Browse the repository at this point in the history
  • Loading branch information
laruence committed Apr 5, 2020
1 parent 286efda commit 16583e3
Show file tree
Hide file tree
Showing 10 changed files with 119 additions and 104 deletions.
5 changes: 3 additions & 2 deletions php_yaf.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,10 @@ extern zend_module_entry yaf_module_entry;
#define yaf_exception_t zval

ZEND_BEGIN_MODULE_GLOBALS(yaf)
/* for conveniently retrieving */
yaf_loader_t loader;
/* for instances stash */
yaf_application_t app;
yaf_loader_t loader;
yaf_registry_t registry;

/* ini configurations */
char *global_library;
Expand Down
2 changes: 1 addition & 1 deletion responses/yaf_response_http.c
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ int yaf_response_http_send(yaf_response_object *response) /* {{{ */ {
} ZEND_HASH_FOREACH_END();
efree(ctr.line);

ZEND_HASH_FOREACH_VAL(&response->bodys, entry) {
ZEND_HASH_FOREACH_VAL(&response->body, entry) {
zend_string *str = zval_get_string(entry);
php_write(ZSTR_VAL(str), ZSTR_LEN(str));
zend_string_release(str);
Expand Down
4 changes: 2 additions & 2 deletions tests/026.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ echo $response;
Yaf_Response_Http Object
(
[response_code] => 0
[headers:protected] => Array
[header:protected] => Array
(
)

[bodys:protected] => Array
[body:protected] => Array
(
[content] => Hello
[footer] => World
Expand Down
3 changes: 3 additions & 0 deletions yaf.c
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,10 @@ PHP_RINIT_FUNCTION(yaf)
{
YAF_G(throw_exception) = 1;
YAF_G(catch_exception) = 0;
ZVAL_UNDEF(&YAF_G(registry));
ZVAL_UNDEF(&YAF_G(loader));
ZVAL_UNDEF(&YAF_G(app));

return SUCCESS;
}
/* }}} */
Expand All @@ -196,6 +198,7 @@ PHP_RINIT_FUNCTION(yaf)
*/
PHP_RSHUTDOWN_FUNCTION(yaf)
{
zval_ptr_dtor(&YAF_G(registry));
zval_ptr_dtor(&YAF_G(loader));
zval_ptr_dtor(&YAF_G(app));

Expand Down
9 changes: 0 additions & 9 deletions yaf_application.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,6 @@
#ifndef PHP_YAF_APPLICATION_H
#define PHP_YAF_APPLICATION_H

#define YAF_APPLICATION_PROPERTY_NAME_CONFIG "config"
#define YAF_APPLICATION_PROPERTY_NAME_DISPATCHER "dispatcher"
#define YAF_APPLICATION_PROPERTY_NAME_RUN "_running"
#define YAF_APPLICATION_PROPERTY_NAME_APP "_app"
#define YAF_APPLICATION_PROPERTY_NAME_ENV "_environ"
#define YAF_APPLICATION_PROPERTY_NAME_MODULES "_modules"
#define YAF_APPLICATION_PROPERTY_NAME_ERRNO "_err_no"
#define YAF_APPLICATION_PROPERTY_NAME_ERRMSG "_err_msg"

typedef struct {
zend_object std;
zend_array modules;
Expand Down
13 changes: 0 additions & 13 deletions yaf_dispatcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,6 @@
#ifndef PHP_YAF_DISPATCHER_H
#define PHP_YAF_DISPATCHER_H

#define YAF_DISPATCHER_PROPERTY_NAME_REQUEST "_request"
#define YAF_DISPATCHER_PROPERTY_NAME_VIEW "_view"
#define YAF_DISPATCHER_PROPERTY_NAME_ROUTER "_router"
#define YAF_DISPATCHER_PROPERTY_NAME_INSTANCE "_instance"
#define YAF_DISPATCHER_PROPERTY_NAME_RENDER "_auto_render"
#define YAF_DISPATCHER_PROPERTY_NAME_RETURN "_return_response"
#define YAF_DISPATCHER_PROPERTY_NAME_FLUSH "_instantly_flush"
#define YAF_DISPATCHER_PROPERTY_NAME_ARGS "_invoke_args"

#define YAF_DISPATCHER_PROPERTY_NAME_MODULE "_default_module"
#define YAF_DISPATCHER_PROPERTY_NAME_CONTROLLER "_default_controller"
#define YAF_DISPATCHER_PROPERTY_NAME_ACTION "_default_action"

#define YAF_ERROR_CONTROLLER "Error"
#define YAF_ERROR_ACTION "error"

Expand Down
144 changes: 86 additions & 58 deletions yaf_registry.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@
#include "php.h"

#include "php_yaf.h"
#include "Zend/zend_interfaces.h" /* for zend_class_serialize_deny */

#include "yaf_namespace.h"
#include "yaf_registry.h"

zend_class_entry *yaf_registry_ce;
zend_class_entry *yaf_registry_ce;
zend_object_handlers yaf_registry_obj_handlers;

/* {{{ ARG_INFO
*/
Expand All @@ -46,37 +49,71 @@ ZEND_BEGIN_ARG_INFO_EX(yaf_registry_set_arginfo, 0, 0, 2)
ZEND_END_ARG_INFO()
/* }}} */

yaf_registry_t *yaf_registry_instance(yaf_registry_t *this_ptr) /* {{{ */ {
yaf_registry_t *instance = zend_read_static_property(
yaf_registry_ce, ZEND_STRL(YAF_REGISTRY_PROPERTY_NAME_INSTANCE), 1);
static HashTable *yaf_registry_get_debug_info(zval *object, int *is_temp) /* {{{ */ {
zval rv;
HashTable *ht;
yaf_registry_object *registry = Z_YAFREGISTRYOBJ_P(object);

*is_temp = 1;

ALLOC_HASHTABLE(ht);
zend_hash_init(ht, 8, NULL, ZVAL_PTR_DTOR, 0);

ZVAL_ARR(&rv, zend_array_dup(&registry->entries));
zend_hash_str_add(ht, "entries:protected", sizeof("entries:protected") - 1, &rv);

return ht;
}
/* }}} */

static void yaf_registry_object_free(zend_object *object) /* {{{ */ {
yaf_registry_object *registry = (yaf_registry_object*)object;

ZEND_ASSERT(Z_OBJ(YAF_G(registry)) == object);

zend_hash_destroy(&registry->entries);

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

yaf_registry_object *yaf_registry_instance() /* {{{ */ {
yaf_registry_object *registry;

if (UNEXPECTED(Z_TYPE(YAF_G(registry)) != IS_OBJECT)) {

if (UNEXPECTED(Z_TYPE_P(instance) != IS_OBJECT ||
!instanceof_function(Z_OBJCE_P(instance), yaf_registry_ce))) {
zval regs;
registry = emalloc(sizeof(yaf_registry_object));

object_init_ex(this_ptr, yaf_registry_ce);
zend_object_std_init(&registry->std, yaf_registry_ce);
registry->std.handlers = &yaf_registry_obj_handlers;

array_init(&regs);
zend_update_property(yaf_registry_ce, this_ptr, ZEND_STRL(YAF_REGISTRY_PROPERTY_NAME_ENTRYS), &regs);
zend_update_static_property(yaf_registry_ce, ZEND_STRL(YAF_REGISTRY_PROPERTY_NAME_INSTANCE), this_ptr);
zval_ptr_dtor(&regs);
zval_ptr_dtor(this_ptr);
zend_hash_init(&registry->entries, 8, NULL, ZVAL_PTR_DTOR, 0);

instance = this_ptr;
ZVAL_OBJ(&YAF_G(registry), &registry->std);
}

return instance;
return Z_YAFREGISTRYOBJ(YAF_G(registry));
}
/* }}} */

static zval *yaf_registry_find(yaf_registry_object *registry, zend_string *name) /* {{{ */ {
return zend_hash_find(&registry->entries, name);
}
/* }}} */

int yaf_registry_is_set(zend_string *name) /* {{{ */ {
yaf_registry_t *registry, rv;
zval *entrys;
static int yaf_registry_has(yaf_registry_object *registry, zend_string *name) /* {{{ */ {
return zend_hash_exists(&registry->entries, name);
}
/* }}} */

registry = yaf_registry_instance(&rv);
entrys = zend_read_property(yaf_registry_ce, registry, ZEND_STRL(YAF_REGISTRY_PROPERTY_NAME_ENTRYS), 1, NULL);
static zval *yaf_registry_update(yaf_registry_object *registry, zend_string *name, zval *value) /* {{{ */ {
Z_TRY_ADDREF_P(value);
return zend_hash_update(&registry->entries, name, value);
}
/* }}} */

return zend_hash_exists(Z_ARRVAL_P(entrys), name);
static void yaf_registry_del(yaf_registry_object *registry, zend_string *name) /* {{{ */ {
zend_hash_del(&registry->entries, name);
}
/* }}} */

Expand All @@ -95,23 +132,18 @@ PHP_METHOD(yaf_registry, __clone) {
/** {{{ proto public static Yaf_Registry::get($name)
*/
PHP_METHOD(yaf_registry, get) {
zval *value;
zend_string *name;
yaf_registry_object *registry = yaf_registry_instance();

if (zend_parse_parameters(ZEND_NUM_ARGS(), "S", &name) == FAILURE) {
return;
} else {
zval *pzval, *entrys;
yaf_registry_t *registry, rv;

registry = yaf_registry_instance(&rv);
entrys = zend_read_property(yaf_registry_ce, registry, ZEND_STRL(YAF_REGISTRY_PROPERTY_NAME_ENTRYS), 1, NULL);

if (EXPECTED(entrys && Z_TYPE_P(entrys) == IS_ARRAY)) {
if ((pzval = zend_hash_find(Z_ARRVAL_P(entrys), name)) != NULL) {
RETURN_ZVAL(pzval, 1, 0);
}
}
}

if ((value = yaf_registry_find(registry, name))) {
RETURN_ZVAL(value, 1, 0);
}

RETURN_NULL();
}
/* }}} */
Expand All @@ -121,20 +153,14 @@ PHP_METHOD(yaf_registry, get) {
PHP_METHOD(yaf_registry, set) {
zval *value;
zend_string *name;
yaf_registry_object *registry = yaf_registry_instance();

if (zend_parse_parameters(ZEND_NUM_ARGS(), "Sz", &name, &value) == FAILURE) {
return;
} else {
yaf_registry_t *registry, rv;
zval *entrys;

registry = yaf_registry_instance(&rv);
entrys = zend_read_property(yaf_registry_ce, registry, ZEND_STRL(YAF_REGISTRY_PROPERTY_NAME_ENTRYS), 1, NULL);
}

if (zend_hash_update(Z_ARRVAL_P(entrys), name, value) != NULL) {
Z_TRY_ADDREF_P(value);
RETURN_TRUE;
}
if (yaf_registry_update(registry, name, value)) {
RETURN_TRUE;
}

RETURN_FALSE;
Expand All @@ -145,18 +171,14 @@ PHP_METHOD(yaf_registry, set) {
*/
PHP_METHOD(yaf_registry, del) {
zend_string *name;
yaf_registry_object *registry = yaf_registry_instance();

if (zend_parse_parameters(ZEND_NUM_ARGS(), "S", &name) == FAILURE) {
return;
} else {
yaf_registry_t *registry, rv;
zval *entrys;

registry = yaf_registry_instance(&rv);
entrys = zend_read_property(yaf_registry_ce, registry, ZEND_STRL(YAF_REGISTRY_PROPERTY_NAME_ENTRYS), 1, NULL);

zend_hash_del(Z_ARRVAL_P(entrys), name);
}

yaf_registry_del(registry, name);

RETURN_TRUE;
}
/* }}} */
Expand All @@ -165,20 +187,22 @@ PHP_METHOD(yaf_registry, del) {
*/
PHP_METHOD(yaf_registry, has) {
zend_string *name;
yaf_registry_object *registry = yaf_registry_instance();

if (zend_parse_parameters(ZEND_NUM_ARGS(), "S", &name) == FAILURE) {
return;
} else {
RETURN_BOOL(yaf_registry_is_set(name));
}

RETURN_BOOL(yaf_registry_has(registry, name));
}
/* }}} */

/** {{{ proto public Yaf_Registry::getInstance(void)
*/
PHP_METHOD(yaf_registry, getInstance) {
yaf_registry_t *registry, rv = {{0}};
registry = yaf_registry_instance(&rv);
RETURN_ZVAL(registry, 1, 0);
ZVAL_OBJ(return_value, &(yaf_registry_instance())->std);
Z_ADDREF_P(return_value);
return;
}
/* }}} */

Expand All @@ -204,9 +228,13 @@ YAF_STARTUP_FUNCTION(registry) {

yaf_registry_ce = zend_register_internal_class_ex(&ce, NULL);
yaf_registry_ce->ce_flags |= ZEND_ACC_FINAL;
yaf_registry_ce->serialize = zend_class_serialize_deny;
yaf_registry_ce->unserialize = zend_class_unserialize_deny;

zend_declare_property_null(yaf_registry_ce, ZEND_STRL(YAF_REGISTRY_PROPERTY_NAME_INSTANCE), ZEND_ACC_PROTECTED|ZEND_ACC_STATIC);
zend_declare_property_null(yaf_registry_ce, ZEND_STRL(YAF_REGISTRY_PROPERTY_NAME_ENTRYS), ZEND_ACC_PROTECTED);
memcpy(&yaf_registry_obj_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
yaf_registry_obj_handlers.clone_obj = NULL;
yaf_registry_obj_handlers.free_obj = yaf_registry_object_free;
yaf_registry_obj_handlers.get_debug_info = yaf_registry_get_debug_info;

return SUCCESS;
}
Expand Down
9 changes: 7 additions & 2 deletions yaf_registry.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,13 @@
#ifndef YAF_REGISTRY_H
#define YAF_REGISTRY_H

#define YAF_REGISTRY_PROPERTY_NAME_ENTRYS "_entries"
#define YAF_REGISTRY_PROPERTY_NAME_INSTANCE "_instance"
typedef struct {
zend_object std;
zend_array entries;
} yaf_registry_object;

#define Z_YAFREGISTRYOBJ(zv) ((yaf_registry_object*)(Z_OBJ(zv)))
#define Z_YAFREGISTRYOBJ_P(zv) Z_YAFREGISTRYOBJ(*zv)

extern zend_class_entry *yaf_registry_ce;

Expand Down
Loading

0 comments on commit 16583e3

Please sign in to comment.