Skip to content

Instantly share code, notes, and snippets.

@hjanuschka
Created May 8, 2015 19:55
Show Gist options
  • Save hjanuschka/3ed54e66f017a379cf25 to your computer and use it in GitHub Desktop.
Save hjanuschka/3ed54e66f017a379cf25 to your computer and use it in GitHub Desktop.
PHP Extension Dynamic Resource
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "php_ini.h"
#include "ext/standard/info.h"
#include "php_hjtest.h"
int resid;
#define MY_RES_NAME "my_resource"
/* If you declare any globals in php_hjtest.h uncomment this:
ZEND_DECLARE_MODULE_GLOBALS(hjtest)
*/
/* True global resources - no need for thread safety here */
static int le_hjtest;
/* {{{ hjtest_functions[]
*
* Every user visible function must have an entry in hjtest_functions[].
*/
const zend_function_entry hjtest_functions[] = {
PHP_FE(confirm_hjtest_compiled, NULL) /* For testing, remove later. */
PHP_FE(test_getMydata, NULL) /* For testing, remove later. */
PHP_FE_END /* Must be the last line in hjtest_functions[] */
};
/* }}} */
/* {{{ hjtest_module_entry
*/
zend_module_entry hjtest_module_entry = {
#if ZEND_MODULE_API_NO >= 20010901
STANDARD_MODULE_HEADER,
#endif
"hjtest",
hjtest_functions,
PHP_MINIT(hjtest),
PHP_MSHUTDOWN(hjtest),
PHP_RINIT(hjtest), /* Replace with NULL if there's nothing to do at request start */
PHP_RSHUTDOWN(hjtest), /* Replace with NULL if there's nothing to do at request end */
PHP_MINFO(hjtest),
#if ZEND_MODULE_API_NO >= 20010901
PHP_HJTEST_VERSION,
#endif
STANDARD_MODULE_PROPERTIES
};
/* }}} */
#ifdef COMPILE_DL_HJTEST
ZEND_GET_MODULE(hjtest)
#endif
/* {{{ PHP_INI
*/
/* Remove comments and fill if you need to have entries in php.ini
PHP_INI_BEGIN()
STD_PHP_INI_ENTRY("hjtest.global_value", "42", PHP_INI_ALL, OnUpdateLong, global_value, zend_hjtest_globals, hjtest_globals)
STD_PHP_INI_ENTRY("hjtest.global_string", "foobar", PHP_INI_ALL, OnUpdateString, global_string, zend_hjtest_globals, hjtest_globals)
PHP_INI_END()
*/
/* }}} */
/* {{{ php_hjtest_init_globals
*/
/* Uncomment this function if you have INI entries
static void php_hjtest_init_globals(zend_hjtest_globals *hjtest_globals)
{
hjtest_globals->global_value = 0;
hjtest_globals->global_string = NULL;
}
*/
/* }}} */
static void resdtor(zend_rsrc_list_entry *rsrc TSRMLS_DC)
{
MYDATA *res = (MYDATA*)rsrc->ptr;
if (res) {
efree(res);
}
}
/* {{{ PHP_MINIT_FUNCTION
*/
PHP_MINIT_FUNCTION(hjtest)
{
/* If you have INI entries, uncomment these lines
REGISTER_INI_ENTRIES();
*/
resid = zend_register_list_destructors_ex(resdtor, NULL, MY_RES_NAME, module_number);
return SUCCESS;
}
/* }}} */
/* {{{ PHP_MSHUTDOWN_FUNCTION
*/
PHP_MSHUTDOWN_FUNCTION(hjtest)
{
/* uncomment this line if you have INI entries
UNREGISTER_INI_ENTRIES();
*/
return SUCCESS;
}
/* }}} */
/* Remove if there's nothing to do at request start */
/* {{{ PHP_RINIT_FUNCTION
*/
PHP_RINIT_FUNCTION(hjtest)
{
return SUCCESS;
}
/* }}} */
/* Remove if there's nothing to do at request end */
/* {{{ PHP_RSHUTDOWN_FUNCTION
*/
PHP_RSHUTDOWN_FUNCTION(hjtest)
{
return SUCCESS;
}
/* }}} */
/* {{{ PHP_MINFO_FUNCTION
*/
PHP_MINFO_FUNCTION(hjtest)
{
php_info_print_table_start();
php_info_print_table_header(2, "hjtest support", "enabled");
php_info_print_table_end();
/* Remove comments if you have entries in php.ini
DISPLAY_INI_ENTRIES();
*/
}
/* }}} */
/* Remove the following function when you have successfully modified config.m4
so that your module can be compiled into PHP, it exists only for testing
purposes. */
/* Every user-visible function in PHP should document itself in the source */
/* {{{ proto string confirm_hjtest_compiled(string arg)
Return a string to confirm that the module is compiled in */
PHP_FUNCTION(confirm_hjtest_compiled)
{
char *arg = NULL;
int arg_len, len;
char *strg;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &arg, &arg_len) == FAILURE) {
return;
}
len = spprintf(&strg, 0, "Congratulations! You have successfully modified ext/%.78s/config.m4. Module %.78s is now compiled into PHP.", "hjtest", arg);
RETURN_STRINGL(strg, len, 0);
}
PHP_FUNCTION(test_getMydata)
{
long int a, b;
long int result;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ll", &a, &b) == FAILURE) {
return;
}
MYDATA * objData=emalloc(sizeof(MYDATA));
objData->m_id = a;
objData->m_age = b;
ZEND_REGISTER_RESOURCE(return_value, objData, resid);
}
/* }}} */
/* The previous line is meant for vim and emacs, so it can correctly fold and
unfold functions in source code. See the corresponding marks just before
function definition, where the functions purpose is also documented. Please
follow this convention for the convenience of others editing your code.
*/
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
:
*/
#ifndef PHP_HJTEST_H
#define PHP_HJTEST_H
typedef struct mydata {
int m_id;
int m_age;
}MYDATA;
extern zend_module_entry hjtest_module_entry;
#define phpext_hjtest_ptr &hjtest_module_entry
#define PHP_HJTEST_VERSION "0.1.0" /* Replace with version number for your extension */
#ifdef PHP_WIN32
# define PHP_HJTEST_API __declspec(dllexport)
#elif defined(__GNUC__) && __GNUC__ >= 4
# define PHP_HJTEST_API __attribute__ ((visibility("default")))
#else
# define PHP_HJTEST_API
#endif
#ifdef ZTS
#include "TSRM.h"
#endif
PHP_MINIT_FUNCTION(hjtest);
PHP_MSHUTDOWN_FUNCTION(hjtest);
PHP_RINIT_FUNCTION(hjtest);
PHP_RSHUTDOWN_FUNCTION(hjtest);
PHP_MINFO_FUNCTION(hjtest);
PHP_FUNCTION(confirm_hjtest_compiled); /* For testing, remove later. */
PHP_FUNCTION(test_getMydata);
/*
Declare any global variables you may need between the BEGIN
and END macros here:
ZEND_BEGIN_MODULE_GLOBALS(hjtest)
long global_value;
char *global_string;
ZEND_END_MODULE_GLOBALS(hjtest)
*/
/* In every utility function you add that needs to use variables
in php_hjtest_globals, call TSRMLS_FETCH(); after declaring other
variables used by that function, or better yet, pass in TSRMLS_CC
after the last function argument and declare your utility function
with TSRMLS_DC after the last declared argument. Always refer to
the globals in your function as HJTEST_G(variable). You are
encouraged to rename these macros something shorter, see
examples in any other php module directory.
*/
#ifdef ZTS
#define HJTEST_G(v) TSRMG(hjtest_globals_id, zend_hjtest_globals *, v)
#else
#define HJTEST_G(v) (hjtest_globals.v)
#endif
#endif /* PHP_HJTEST_H */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment