1
+ use once_cell:: sync:: Lazy ;
1
2
use phper:: {
2
3
c_str_ptr, php_function, php_get_module, php_minfo_function, php_minit_function,
3
4
php_mshutdown_function, php_rinit_function, php_rshutdown_function,
@@ -7,93 +8,173 @@ use phper::{
7
8
} ,
8
9
zend:: {
9
10
api:: { FunctionEntries , ModuleGlobals } ,
11
+ classes:: { Class , MethodEntity , StdClass , This } ,
10
12
compile:: { create_zend_arg_info, MultiInternalArgInfo } ,
11
- ini:: IniEntries ,
12
- modules:: { ModuleArgs , ModuleEntry , ModuleEntryBuilder } ,
13
- types:: { ExecuteData , SetVal } ,
13
+ ini:: { IniEntries , Policy } ,
14
+ modules:: {
15
+ read_global_module, write_global_module, Module , ModuleArgs , ModuleEntry ,
16
+ ModuleEntryBuilder ,
17
+ } ,
18
+ types:: { ExecuteData , SetVal , Val } ,
14
19
} ,
15
20
} ;
21
+ use std:: { fs:: OpenOptions , io:: Write } ;
16
22
17
- static HELLO_ENABLE : ModuleGlobals < bool > = ModuleGlobals :: new ( false ) ;
23
+ // static HELLO_ENABLE: ModuleGlobals<bool> = ModuleGlobals::new(false);
24
+ //
25
+ // static INI_ENTRIES: IniEntries<1> = IniEntries::new([HELLO_ENABLE.create_ini_entry(
26
+ // "hello.enable",
27
+ // "1",
28
+ // Some(OnUpdateBool),
29
+ // PHP_INI_SYSTEM,
30
+ // )]);
31
+ //
32
+ // #[php_minit_function]
33
+ // fn module_init(args: ModuleArgs) -> bool {
34
+ // args.register_ini_entries(&INI_ENTRIES);
35
+ // true
36
+ // }
37
+ //
38
+ // #[php_mshutdown_function]
39
+ // fn module_shutdown(args: ModuleArgs) -> bool {
40
+ // args.unregister_ini_entries();
41
+ // true
42
+ // }
43
+ //
44
+ // #[php_rinit_function]
45
+ // fn request_init(_args: ModuleArgs) -> bool {
46
+ // true
47
+ // }
48
+ //
49
+ // #[php_rshutdown_function]
50
+ // fn request_shutdown(_args: ModuleArgs) -> bool {
51
+ // true
52
+ // }
53
+ //
54
+ // #[php_minfo_function]
55
+ // fn module_info(module: &ModuleEntry) {
56
+ // unsafe {
57
+ // php_info_print_table_start();
58
+ // php_info_print_table_row(2, c_str_ptr!("hello.version"), (*module.as_ptr()).version);
59
+ // php_info_print_table_row(
60
+ // 2,
61
+ // c_str_ptr!("hello.enable"),
62
+ // if HELLO_ENABLE.get() {
63
+ // c_str_ptr!("1")
64
+ // } else {
65
+ // c_str_ptr!("0")
66
+ // },
67
+ // );
68
+ // php_info_print_table_end();
69
+ // }
70
+ // }
71
+ //
72
+ // #[php_function]
73
+ // pub fn say_hello(execute_data: &mut ExecuteData) -> impl SetVal {
74
+ // execute_data
75
+ // .parse_parameters::<&str>()
76
+ // .map(|name| format!("Hello, {}!", name))
77
+ // }
78
+ //
79
+ // static ARG_INFO_SAY_HELLO: MultiInternalArgInfo<1> =
80
+ // MultiInternalArgInfo::new(1, false, [create_zend_arg_info(c_str_ptr!("n_ame"), false)]);
81
+ //
82
+ // static FUNCTION_ENTRIES: FunctionEntries<1> = FunctionEntries::new([zend_function_entry {
83
+ // fname: c_str_ptr!("say_hello"),
84
+ // handler: Some(say_hello),
85
+ // arg_info: ARG_INFO_SAY_HELLO.as_ptr(),
86
+ // num_args: 2,
87
+ // flags: 0,
88
+ // }]);
89
+ //
90
+ // static MODULE_ENTRY: ModuleEntry = ModuleEntryBuilder::new(
91
+ // c_str_ptr!(env!("CARGO_PKG_NAME")),
92
+ // c_str_ptr!(env!("CARGO_PKG_VERSION")),
93
+ // )
94
+ // .functions(FUNCTION_ENTRIES.as_ptr())
95
+ // .module_startup_func(module_init)
96
+ // .module_shutdown_func(module_shutdown)
97
+ // .request_startup_func(request_init)
98
+ // .request_shutdown_func(request_shutdown)
99
+ // .info_func(module_info)
100
+ // .build();
18
101
19
- static INI_ENTRIES : IniEntries < 1 > = IniEntries :: new ( [ HELLO_ENABLE . create_ini_entry (
20
- "hello.enable" ,
21
- "1" ,
22
- Some ( OnUpdateBool ) ,
23
- PHP_INI_SYSTEM ,
24
- ) ] ) ;
102
+ // pub fn get_module() -> Module {
103
+ // let mut module = Module::new(env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION"));
104
+ // module.on_module_init(Box::new(|_| {
105
+ // println!("Are you ok?");
106
+ // true
107
+ // }));
108
+ // module
109
+ // }
25
110
26
- #[ php_minit_function]
27
111
fn module_init ( args : ModuleArgs ) -> bool {
28
- args . register_ini_entries ( & INI_ENTRIES ) ;
112
+ // append_file("module_init" );
29
113
true
30
114
}
31
115
32
- #[ php_mshutdown_function]
33
116
fn module_shutdown ( args : ModuleArgs ) -> bool {
34
- args . unregister_ini_entries ( ) ;
117
+ // append_file("module_shutdown" );
35
118
true
36
119
}
37
120
38
- # [ php_rinit_function ]
39
- fn request_init ( _args : ModuleArgs ) -> bool {
121
+ fn request_init ( args : ModuleArgs ) -> bool {
122
+ // append_file("request_init");
40
123
true
41
124
}
42
125
43
- # [ php_rshutdown_function ]
44
- fn request_shutdown ( _args : ModuleArgs ) -> bool {
126
+ fn request_shutdown ( args : ModuleArgs ) -> bool {
127
+ // append_file("request_shutdown");
45
128
true
46
129
}
47
130
48
- #[ php_minfo_function]
49
- fn module_info ( module : & ModuleEntry ) {
50
- unsafe {
51
- php_info_print_table_start ( ) ;
52
- php_info_print_table_row ( 2 , c_str_ptr ! ( "hello.version" ) , ( * module. as_ptr ( ) ) . version ) ;
53
- php_info_print_table_row (
54
- 2 ,
55
- c_str_ptr ! ( "hello.enable" ) ,
56
- if HELLO_ENABLE . get ( ) {
57
- c_str_ptr ! ( "1" )
58
- } else {
59
- c_str_ptr ! ( "0" )
60
- } ,
61
- ) ;
62
- php_info_print_table_end ( ) ;
63
- }
64
- }
131
+ fn append_file ( s : & str ) {
132
+ let mut file = OpenOptions :: new ( )
133
+ . write ( true )
134
+ . append ( true )
135
+ . open ( "/tmp/hello" )
136
+ . unwrap ( ) ;
65
137
66
- #[ php_function]
67
- pub fn say_hello ( execute_data : & mut ExecuteData ) -> impl SetVal {
68
- execute_data
69
- . parse_parameters :: < & str > ( )
70
- . map ( |name| format ! ( "Hello, {}!" , name) )
138
+ writeln ! ( file, "{}" , s) . unwrap ( ) ;
71
139
}
72
140
73
- static ARG_INFO_SAY_HELLO : MultiInternalArgInfo < 1 > =
74
- MultiInternalArgInfo :: new ( 1 , false , [ create_zend_arg_info ( c_str_ptr ! ( "n_ame" ) , false ) ] ) ;
75
-
76
- static FUNCTION_ENTRIES : FunctionEntries < 1 > = FunctionEntries :: new ( [ zend_function_entry {
77
- fname : c_str_ptr ! ( "say_hello" ) ,
78
- handler : Some ( say_hello) ,
79
- arg_info : ARG_INFO_SAY_HELLO . as_ptr ( ) ,
80
- num_args : 2 ,
81
- flags : 0 ,
82
- } ] ) ;
83
-
84
- static MODULE_ENTRY : ModuleEntry = ModuleEntryBuilder :: new (
85
- c_str_ptr ! ( env!( "CARGO_PKG_NAME" ) ) ,
86
- c_str_ptr ! ( env!( "CARGO_PKG_VERSION" ) ) ,
87
- )
88
- . functions ( FUNCTION_ENTRIES . as_ptr ( ) )
89
- . module_startup_func ( module_init)
90
- . module_shutdown_func ( module_shutdown)
91
- . request_startup_func ( request_init)
92
- . request_shutdown_func ( request_shutdown)
93
- . info_func ( module_info)
94
- . build ( ) ;
95
-
96
- #[ php_get_module]
97
- pub fn get_module ( ) -> & ' static ModuleEntry {
98
- & MODULE_ENTRY
141
+ fn test_func ( ) { }
142
+
143
+ #[ no_mangle]
144
+ pub extern "C" fn get_module ( ) -> * const :: phper:: sys:: zend_module_entry {
145
+ // static module: Lazy<Module> = Lazy::new(|| {
146
+ // let mut module = Module::new(env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION"));
147
+ // module
148
+ // });
149
+ // unsafe {
150
+ // module.create_zend_module_entry()
151
+ // }
152
+
153
+ let f = |module : & mut Module | {
154
+ module. set_name ( env ! ( "CARGO_PKG_NAME" ) ) ;
155
+ module. set_version ( env ! ( "CARGO_PKG_VERSION" ) ) ;
156
+
157
+ module. add_ini ( "hello.enable" , "on" , Policy :: All ) ;
158
+
159
+ module. on_module_init ( module_init) ;
160
+ module. on_module_shutdown ( module_shutdown) ;
161
+ module. on_request_init ( request_init) ;
162
+ module. on_request_shutdown ( request_shutdown) ;
163
+
164
+ module. add_function ( "hello_fuck" , || {
165
+ println ! ( "Fuck you!" ) ;
166
+ } ) ;
167
+ module. add_function ( "test_func" , test_func) ;
168
+
169
+ let mut std_class = StdClass :: new ( ) ;
170
+ std_class. add_property ( "foo" , 100 ) ;
171
+ std_class. add_method ( "test1" , |_: & mut This | {
172
+ println ! ( "hello test1" ) ;
173
+ } ) ;
174
+ module. add_class ( "Test1" , std_class) ;
175
+ } ;
176
+
177
+ f ( & mut * write_global_module ( ) ) ;
178
+
179
+ unsafe { read_global_module ( ) . module_entry ( ) }
99
180
}
0 commit comments