|
1 | 1 | # INI参数设置 |
2 | 2 |
|
3 | | -1,创建一个新的扩展 |
| 3 | +## 创建一个新的扩展 |
4 | 4 |
|
5 | | - ./ext_skel --extname=inis |
| 5 | ++ 生成扩展 |
6 | 6 |
|
7 | | -2,宏 |
| 7 | +``` |
| 8 | +./ext_skel --extname=inis |
| 9 | +``` |
8 | 10 |
|
9 | | - #define ZEND_DECLARE_MODULE_GLOBALS(module_name) ts_rsrc_id module_name##_globals_id; |
10 | | - |
11 | | - #define STD_PHP_INI_ENTRY STD_ZEND_INI_ENTRY |
12 | | - #define STD_ZEND_INI_ENTRY(name, default_value, modifiable, on_modify, property_name, struct_type, struct_ptr) \ |
13 | | - ZEND_INI_ENTRY2(name, default_value, modifiable, on_modify, (void *) XtOffsetOf(struct_type, property_name), (void *) &struct_ptr##_id) |
14 | | - #define ZEND_INI_ENTRY3_EX(name, default_value, modifiable, on_modify, arg1, arg2, arg3, displayer) \ |
| 11 | ++ config.m4 |
| 12 | + |
| 13 | +``` |
| 14 | +PHP_ARG_ENABLE(inis, whether to enable inis support, |
| 15 | +[ --enable-inis Enable inis support]) |
| 16 | +
|
| 17 | +if test "$PHP_INIS" != "no"; then |
| 18 | + PHP_NEW_EXTENSION(inis, inis.c, $ext_shared) |
| 19 | +fi |
| 20 | +``` |
| 21 | + |
| 22 | ++ php_inis.h |
| 23 | + |
| 24 | +``` |
| 25 | +ZEND_BEGIN_MODULE_GLOBALS(inis) |
| 26 | + char *name; |
| 27 | + long level; |
| 28 | +ZEND_END_MODULE_GLOBALS(inis) |
| 29 | +
|
| 30 | +#ifdef ZTS |
| 31 | +#define INIS_G(v) TSRMG(inis_globals_id, zend_inis_globals *, v) |
| 32 | +#else |
| 33 | +#define INIS_G(v) (inis_globals.v) |
| 34 | +#endif |
| 35 | +``` |
| 36 | + |
| 37 | ++ inis.c |
| 38 | + |
| 39 | +定义全局变量 |
| 40 | + |
| 41 | +```c |
| 42 | +ZEND_DECLARE_MODULE_GLOBALS(inis) |
| 43 | +``` |
| 44 | +
|
| 45 | +设置INI参数 |
| 46 | +
|
| 47 | +```c |
| 48 | +PHP_INI_BEGIN() |
| 49 | + STD_PHP_INI_ENTRY("inis.name","sunzy",PHP_INI_ALL,OnUpdateString,name,zend_inis_globals,inis_globals) |
| 50 | + STD_PHP_INI_ENTRY("inis.level","1",PHP_INI_ALL,OnUpdateLong,level,zend_inis_globals,inis_globals) |
| 51 | +PHP_INI_END() |
| 52 | +``` |
| 53 | + |
| 54 | +初始化函数 |
| 55 | + |
| 56 | +```c |
| 57 | +static void php_inis_init_globals(zend_inis_globals *inis_globals) |
| 58 | +{ |
| 59 | + inis_globals->name="sunzy.org"; |
| 60 | + inis_globals->level=10; |
| 61 | +} |
| 62 | +``` |
| 63 | +
|
| 64 | +注册&清除INI变量 |
| 65 | +
|
| 66 | +```c |
| 67 | +PHP_MINIT_FUNCTION(inis) |
| 68 | +{ |
| 69 | + REGISTER_INI_ENTRIES(); |
| 70 | + return SUCCESS; |
| 71 | +} |
| 72 | +
|
| 73 | +PHP_MSHUTDOWN_FUNCTION(inis) |
| 74 | +{ |
| 75 | + UNREGISTER_INI_ENTRIES(); |
| 76 | + return SUCCESS; |
| 77 | +} |
| 78 | +``` |
| 79 | + |
| 80 | +自定义函数 |
| 81 | + |
| 82 | +```c |
| 83 | +PHP_FUNCTION(inis_report) |
| 84 | +{ |
| 85 | + php_printf("Name:%s\n",INIS_G(name)); |
| 86 | + php_printf("Level:%d\n",INIS_G(level)); |
| 87 | +} |
| 88 | +``` |
| 89 | +
|
| 90 | +注册自定义函数 |
| 91 | +
|
| 92 | +```c |
| 93 | +const zend_function_entry inis_functions[] = { |
| 94 | + PHP_FE(inis_report,NULL) |
| 95 | + PHP_FE_END /* Must be the last line in inis_functions[] */ |
| 96 | +}; |
| 97 | +``` |
| 98 | + |
| 99 | ++ 测试 |
| 100 | + |
| 101 | +测试脚本 |
| 102 | + |
| 103 | +```php |
| 104 | +$function = "inis_report"; |
| 105 | +call_user_func($function); |
| 106 | +``` |
| 107 | + |
| 108 | +测试 |
| 109 | + |
| 110 | +```shell |
| 111 | +$ /usr/local/php5.6.9/bin/php -q inis.php |
| 112 | +Functions available in the test extension: |
| 113 | +inis_report |
| 114 | +Name:sunzy |
| 115 | +Level:1 |
| 116 | +``` |
| 117 | + |
| 118 | +修改php.ini |
| 119 | + |
| 120 | +``` |
| 121 | +inis.name=test |
| 122 | +inis.level=2 |
| 123 | +``` |
| 124 | + |
| 125 | +```shell |
| 126 | +$ /usr/local/php5.6.9/bin/php -q inis.php |
| 127 | +Functions available in the test extension: |
| 128 | +inis_report |
| 129 | +Name:test |
| 130 | +Level:2 |
| 131 | +``` |
| 132 | + |
| 133 | + |
| 134 | +## 相关的宏 |
| 135 | + |
| 136 | ++ ZEND_DECLARE_MODULE_GLOBALS ->Zend/zend_API.h |
| 137 | + |
| 138 | + |
| 139 | +```c |
| 140 | +#ifdef ZTS |
| 141 | +#define ZEND_DECLARE_MODULE_GLOBALS(module_name) \ |
| 142 | + ts_rsrc_id module_name##_globals_id; |
| 143 | +#else |
| 144 | +#define ZEND_DECLARE_MODULE_GLOBALS(module_name) \ |
| 145 | + zend_##module_name##_globals module_name##_globals; |
| 146 | +#endif |
| 147 | +``` |
| 148 | + |
| 149 | +```ts_rsrc_id```是[线程安全](http://walu.cc/phpbook/1.4.md)里的内容 |
| 150 | + |
| 151 | ++ PHP_INI_BEGIN、PHP_INI_END、STD_PHP_INI_ENTRY ->main/php_ini.h |
| 152 | + |
| 153 | +```c |
| 154 | +#define PHP_INI_BEGIN ZEND_INI_BEGIN |
| 155 | +#define PHP_INI_END ZEND_INI_END |
| 156 | +#define STD_PHP_INI_ENTRY STD_ZEND_INI_ENTRY |
| 157 | +``` |
| 158 | + |
| 159 | ++ ZEND_INI_BEGIN、ZEND_INI_END、STD_ZEND_INI_ENTRY -> Zend/zend_ini.h |
| 160 | + |
| 161 | +```c |
| 162 | +#define ZEND_INI_BEGIN() static const zend_ini_entry ini_entries[] = { |
| 163 | +#define ZEND_INI_END() { 0, 0, NULL, 0, NULL, NULL, NULL, NULL, NULL, 0, NULL, 0, 0, 0, NULL } }; |
| 164 | + |
| 165 | +#define ZEND_INI_ENTRY3_EX(name, default_value, modifiable, on_modify, arg1, arg2, arg3, displayer) \ |
15 | 166 | { 0, modifiable, name, sizeof(name), on_modify, arg1, arg2, arg3, default_value, sizeof(default_value)-1, NULL, 0, 0, 0, displayer }, |
16 | | - |
17 | | - #define ZEND_INI_BEGIN() static const zend_ini_entry ini_entries[] = { |
18 | | - #define ZEND_INI_END() { 0, 0, NULL, 0, NULL, NULL, NULL, NULL, NULL, 0, NULL, 0, 0, 0, NULL } }; |
19 | | - |
20 | | - //注册ini变量,在minit中调用 |
21 | | - #define REGISTER_INI_ENTRIES() zend_register_ini_entries(ini_entries, module_number TSRMLS_CC) |
22 | | - |
23 | | - //清除ini变量,在mshutdown中调用 |
24 | | - #define UNREGISTER_INI_ENTRIES() zend_unregister_ini_entries(module_number TSRMLS_CC) |
25 | | - |
26 | | - //在phpinfo中显示变量值,在minfo中调用 |
27 | | - #define DISPLAY_INI_ENTRIES() display_ini_entries(zend_module) |
28 | | - |
29 | | - // |
30 | | - |
31 | | -3,定义ini变量 |
32 | | - |
33 | | - struct _zend_ini_entry { |
34 | | - int module_number; |
35 | | - int modifiable; |
36 | | - char *name; |
37 | | - uint name_length; |
38 | | - ZEND_INI_MH((*on_modify)); |
39 | | - void *mh_arg1; |
40 | | - void *mh_arg2; |
41 | | - void *mh_arg3; |
42 | | - |
43 | | - char *value; |
44 | | - uint value_length; |
45 | | - |
46 | | - char *orig_value; |
47 | | - uint orig_value_length; |
48 | | - int orig_modifiable; |
49 | | - int modified; |
50 | | - |
51 | | - void (*displayer)(zend_ini_entry *ini_entry, int type); |
52 | | - }; |
53 | | - |
54 | | - //第二个参数即为ini变量的初始值 |
55 | | - PHP_INI_BEGIN() |
56 | | - STD_PHP_INI_ENTRY("inis.enable", "1", PHP_INI_ALL, OnUpdateBool, enable, zend_inis_globals, inis_globals) |
57 | | - STD_PHP_INI_ENTRY("inis.name", "", PHP_INI_ALL, OnUpdateString, name, zend_inis_globals, inis_globals) |
58 | | - STD_PHP_INI_ENTRY("inis.level", "0", PHP_INI_ALL, OnUpdateLong, level, zend_inis_globals, inis_globals) |
59 | | - PHP_INI_END() |
60 | | - |
61 | | - #展开后 |
62 | | - |
63 | | - static const zend_ini_entry ini_entries[] = {//BEGIN |
64 | | - {0, PHP_INI_ALL, "inis.enable", sizeof("inis.enable"), OnUpdateBool, enable, zend_inis_globals, inis_globals... }//inis.enable定义 |
65 | | - ... |
66 | | - { 0, 0, NULL, 0, NULL, NULL, NULL, NULL, NULL, 0, NULL, 0, 0, 0, NULL } //END |
67 | | - }; |
68 | | - |
69 | | - /*初始化ini值的宏*/ |
70 | | - ZEND_INIT_MODULE_GLOBALS(module_name, globals_ctor, globals_dtor) |
71 | | - |
72 | | - #module_name 与传递给ZEND_BEGIN_MODULE_GLOBALS()宏相同的扩展名称。 |
73 | | - #globals_ctor 构造函数指针。在myfile扩展里,函数原形与void php_myfile_init_globals(zend_myfile_globals *myfile_globals)类似 |
74 | | - #globals_dtor 析构函数指针。例如,php_myfile_init_globals(zend_myfile_globals *myfile_globals) |
75 | | - |
76 | | -4,注册/清除/显示 ini变量 |
77 | 167 |
|
78 | | - PHP_MINIT_FUNCTION(inis) |
79 | | - { |
80 | | - REGISTER_INI_ENTRIES(); |
81 | | - return SUCCESS; |
82 | | - } |
83 | | - |
84 | | - PHP_MSHUTDOWN_FUNCTION(inis) |
85 | | - { |
86 | | - UNREGISTER_INI_ENTRIES(); |
87 | | - return SUCCESS; |
88 | | - } |
89 | | - |
90 | | - PHP_MINFO_FUNCTION(inis) |
91 | | - { |
92 | | - php_info_print_table_start(); |
93 | | - php_info_print_table_header(2, "inis support", "enabled"); |
94 | | - php_info_print_table_end(); |
95 | | - |
96 | | - DISPLAY_INI_ENTRIES(); |
97 | | - } |
98 | | - |
99 | | -5,获取ini变量的值 |
| 168 | +#define ZEND_INI_ENTRY3(name, default_value, modifiable, on_modify, arg1, arg2, arg3) \ |
| 169 | + ZEND_INI_ENTRY3_EX(name, default_value, modifiable, on_modify, arg1, arg2, arg3, NULL) |
100 | 170 |
|
101 | | - //INIS_G宏即是用于获取ini变量的值的方法 |
102 | | - PHP_FUNCTION(inis_report) { |
103 | | - php_printf("Name:%s\n", INIS_G(name)); |
104 | | - php_printf("Level:%d\n", INIS_G(level)); |
105 | | - } |
106 | | - |
107 | | - |
108 | | -6,修改ini变量 |
109 | | - |
110 | | - #编辑php.ini文件 |
111 | | - vi /etc/php/php.ini |
112 | | - |
113 | | - #添加动态库,并修改其默认值 |
114 | | - extension=inis.so |
115 | | - inis.name = leon123 |
116 | | - inis.level = 200 |
| 171 | +#define ZEND_INI_ENTRY2_EX(name, default_value, modifiable, on_modify, arg1, arg2, displayer) \ |
| 172 | + ZEND_INI_ENTRY3_EX(name, default_value, modifiable, on_modify, arg1, arg2, NULL, displayer) |
| 173 | + |
| 174 | +#define ZEND_INI_ENTRY2(name, default_value, modifiable, on_modify, arg1, arg2) \ |
| 175 | + ZEND_INI_ENTRY2_EX(name, default_value, modifiable, on_modify, arg1, arg2, NULL) |
117 | 176 |
|
118 | | - #运行,ini变量的值变成为上面修改过的值 |
119 | | - kentchentekiiMac-23868:inis kentchen$ php -q inis.php |
120 | | - Functions available in the test extension: |
121 | | - inis_report |
122 | | - Name:leon123 |
123 | | - Level:200 |
| 177 | +#ifdef ZTS |
| 178 | +#define STD_ZEND_INI_ENTRY(name, default_value, modifiable, on_modify, property_name, struct_type, struct_ptr) \ |
| 179 | + ZEND_INI_ENTRY2(name, default_value, modifiable, on_modify, (void *) XtOffsetOf(struct_type, property_name), (void *) &struct_ptr##_id) |
| 180 | +#else |
| 181 | +#define STD_ZEND_INI_ENTRY(name, default_value, modifiable, on_modify, property_name, struct_type, struct_ptr) \ |
| 182 | + ZEND_INI_ENTRY2(name, default_value, modifiable, on_modify, (void *) XtOffsetOf(struct_type, property_name), (void *) &struct_ptr) |
| 183 | +#endif |
| 184 | + |
| 185 | +``` |
| 186 | + |
| 187 | +前面扩展中对ini变量的定义展开后 |
| 188 | + |
| 189 | +```c |
| 190 | +static const zend_ini_entry ini_entries[] = {//BEGIN |
| 191 | + {0, PHP_INI_ALL, "inis.enable", sizeof("inis.enable"), OnUpdateBool, enable, zend_inis_globals, inis_globals... }//inis.enable定义 |
| 192 | + ... |
| 193 | + { 0, 0, NULL, 0, NULL, NULL, NULL, NULL, NULL, 0, NULL, 0, 0, 0, NULL } //END |
| 194 | +}; |
| 195 | +``` |
| 196 | + |
| 197 | ++ REGISTER_INI_ENTRIES、UNREGISTER_INI_ENTRIES ->Zend/zend_ini.h |
| 198 | + |
| 199 | +```c |
| 200 | +#define REGISTER_INI_ENTRIES() zend_register_ini_entries(ini_entries, module_number TSRMLS_CC) //注册ini变量 |
| 201 | +#define UNREGISTER_INI_ENTRIES() zend_unregister_ini_entries(module_number TSRMLS_CC) //清除ini变量 |
| 202 | +#define DISPLAY_INI_ENTRIES() display_ini_entries(zend_module) //在phpinfo中显示变量值 |
| 203 | +``` |
| 204 | +
|
| 205 | +
|
| 206 | ++ ZEND_INIT_MODULE_GLOBALS ->Zend/zend_ini.h |
| 207 | +
|
| 208 | +```c |
| 209 | +#ifdef ZTS |
| 210 | +#define ZEND_INIT_MODULE_GLOBALS(module_name, globals_ctor, globals_dtor) \ |
| 211 | + ts_allocate_id(&module_name##_globals_id, sizeof(zend_##module_name##_globals), (ts_allocate_ctor) globals_ctor, (ts_allocate_dtor) globals_dtor); |
| 212 | +#else |
| 213 | +#define ZEND_INIT_MODULE_GLOBALS(module_name, globals_ctor, globals_dtor) \ |
| 214 | + globals_ctor(&module_name##_globals); |
| 215 | +#endif |
| 216 | +``` |
| 217 | +初始化ini值的宏 |
| 218 | + |
| 219 | +> module_name 与传递给ZEND_BEGIN_MODULE_GLOBALS()宏相同的扩展名称。 |
| 220 | + |
| 221 | +> globals_ctor 构造函数指针。在myfile扩展里,函数原形与void php_myfile_init_globals(zend_myfile_globals *myfile_globals)类似 |
| 222 | + |
| 223 | +> globals_dtor 析构函数指针。例如,php_myfile_init_globals(zend_myfile_globals *myfile_globals) |
124 | 224 | |
125 | | - #注释php.ini里设置值的语句,ini变量值变回了默认值 |
126 | | - kentchentekiiMac-23868:inis kentchen$ php -q inis.php |
127 | | - Functions available in the test extension: |
128 | | - inis_report |
129 | | - Name:leon peng |
130 | | - Level:100 |
131 | 225 |
|
0 commit comments