Skip to content

Commit 30f22fe

Browse files
committed
namespace registration
1 parent c6eef8d commit 30f22fe

File tree

4 files changed

+41
-13
lines changed

4 files changed

+41
-13
lines changed

bencode.cc

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -547,11 +547,23 @@ static zend_function_entry bint_methods[] = {
547547
};
548548

549549
/**** PHP ****/
550+
PHP_INI_BEGIN()
551+
PHP_INI_ENTRY("bencode.namespace", "0", PHP_INI_SYSTEM, NULL)
552+
PHP_INI_END()
553+
550554
PHP_MINIT_FUNCTION(bencode)
551555
{
556+
REGISTER_INI_ENTRIES();
557+
558+
char *ini_ns_key = zend_container::get_cstring("bencode.namespace");
559+
zend_bool ini_ns = zend_ini_long(ini_ns_key, strlen(ini_ns_key), 0);
560+
efree(ini_ns_key);
552561
do {
553562
zend_class_entry ce;
554-
INIT_CLASS_ENTRY(ce, "bitem", bitem_methods);
563+
if (ini_ns)
564+
INIT_CLASS_ENTRY(ce, "bencode\\bitem", bitem_methods)
565+
else
566+
INIT_CLASS_ENTRY(ce, "bitem", bitem_methods);
555567
ce.ce_flags |= ZEND_ACC_EXPLICIT_ABSTRACT_CLASS;
556568
ce.ce_flags |= ZEND_ACC_IMPLICIT_ABSTRACT_CLASS;
557569
zend_container::bitem_ce = zend_register_internal_class(&ce TSRMLS_CC);
@@ -566,6 +578,12 @@ PHP_MINIT_FUNCTION(bencode)
566578
return SUCCESS;
567579
}
568580

581+
PHP_MSHUTDOWN_FUNCTION(bencode)
582+
{
583+
UNREGISTER_INI_ENTRIES();
584+
return SUCCESS;
585+
}
586+
569587
static zend_function_entry bencode_functions[] = {
570588
PHP_FE(bencode_hello, NULL)
571589
{NULL, NULL, NULL}
@@ -578,7 +596,7 @@ zend_module_entry bencode_module_entry = {
578596
PHP_BENCODE_EXTNAME,
579597
bencode_functions, //FUNCTIONS
580598
PHP_MINIT(bencode), //PHP_MINIT
581-
NULL, //PHP_MSHUTDOWN(bencode),
599+
PHP_MSHUTDOWN(bencode), //PHP_MSHUTDOWN(bencode),
582600
NULL, //PHP_RINIT(bencode),
583601
NULL, //PHP_RSHUTDOWN
584602
NULL, //PHP_MINFO
@@ -596,5 +614,6 @@ ZEND_GET_MODULE(bencode)
596614

597615
PHP_FUNCTION(bencode_hello)
598616
{
599-
php_printf("Hello Bencode!\n");
617+
php_printf("hello bencode!\n");
618+
RETURN_TRUE;
600619
}

binit.h

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,17 @@
3636
return &new_object->std; \
3737
}
3838

39-
#define BI_MINIT(bclass) do { \
40-
zend_class_entry ce; \
41-
INIT_CLASS_ENTRY(ce, #bclass, bclass##_methods); \
42-
ce.parent = zend_container::bitem_ce; \
43-
zend_container::bclass##_ce = zend_register_internal_class(&ce TSRMLS_CC); \
44-
zend_container::bclass##_ce->create_object = zend_container::bclass##_object_new; \
45-
memcpy(&zend_container::bclass##_object_handlers, \
46-
zend_get_std_object_handlers(), sizeof(zend_object_handlers)); \
47-
zend_container::bclass##_object_handlers.offset = XtOffsetOf(bclass##_object, std); \
48-
zend_container::bclass##_object_handlers.clone_obj = zend_container::bclass##_object_clone; \
39+
#define BI_MINIT(bclass) do { \
40+
zend_class_entry ce; \
41+
if (ini_ns) INIT_CLASS_ENTRY(ce, "bencode\\"#bclass, bclass##_methods) \
42+
else INIT_CLASS_ENTRY(ce, #bclass, bclass##_methods); \
43+
ce.parent = zend_container::bitem_ce; \
44+
zend_container::bclass##_ce = zend_register_internal_class(&ce TSRMLS_CC); \
45+
zend_container::bclass##_ce->create_object = zend_container::bclass##_object_new; \
46+
memcpy(&zend_container::bclass##_object_handlers, \
47+
zend_get_std_object_handlers(), sizeof(zend_object_handlers)); \
48+
zend_container::bclass##_object_handlers.offset = XtOffsetOf(bclass##_object, std); \
49+
zend_container::bclass##_object_handlers.clone_obj = zend_container::bclass##_object_clone; \
4950
zend_container::bclass##_object_handlers.free_obj = zend_container::bclass##_free_storage; } while (0);
5051

5152
#define Z_BITEM_OBJ_P(zv) zend_container::bitem_fetch_object(Z_OBJ_P(zv))

php_bencode.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,14 @@
66

77
extern "C" {
88
#include "php.h"
9+
#include "php_ini.h"
910
#ifdef ZTS
1011
#include "TSRM.h"
1112
#endif
1213
}
1314

15+
PHP_MINIT_FUNCTION(bencode);
16+
PHP_MSHUTDOWN_FUNCTION(bencode);
1417
PHP_FUNCTION(bencode_hello);
1518

1619
extern zend_module_entry bencode_module_entry;

zend_container.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ class zend_container {
5151
ZVAL_OBJ(new_object, Z_OBJ_P(object)->handlers->clone_obj(object));
5252
return Z_OBJ_P(new_object);
5353
}
54+
static char * get_cstring(std::string cppstring) {
55+
char *result = (char *)emalloc(cppstring.length());
56+
strcpy(result, cppstring.c_str());
57+
return result;
58+
}
5459
};
5560

5661
#endif

0 commit comments

Comments
 (0)