Skip to content

Commit 1ea7015

Browse files
committed
Finish refactoring the base dynamic framework, prepare to clean code.
1 parent 5b88923 commit 1ea7015

File tree

15 files changed

+772
-131
lines changed

15 files changed

+772
-131
lines changed

examples/hello/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ license = "Unlicense"
1212
crate-type = ["cdylib"]
1313

1414
[dependencies]
15+
once_cell = "1.7.2"
1516
phper = { version = "0.2", path = "../../phper" }
1617

1718
[dev-dependencies]

examples/hello/src/lib.rs

Lines changed: 147 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use once_cell::sync::Lazy;
12
use phper::{
23
c_str_ptr, php_function, php_get_module, php_minfo_function, php_minit_function,
34
php_mshutdown_function, php_rinit_function, php_rshutdown_function,
@@ -7,93 +8,173 @@ use phper::{
78
},
89
zend::{
910
api::{FunctionEntries, ModuleGlobals},
11+
classes::{Class, MethodEntity, StdClass, This},
1012
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},
1419
},
1520
};
21+
use std::{fs::OpenOptions, io::Write};
1622

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();
18101

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+
// }
25110

26-
#[php_minit_function]
27111
fn module_init(args: ModuleArgs) -> bool {
28-
args.register_ini_entries(&INI_ENTRIES);
112+
// append_file("module_init");
29113
true
30114
}
31115

32-
#[php_mshutdown_function]
33116
fn module_shutdown(args: ModuleArgs) -> bool {
34-
args.unregister_ini_entries();
117+
// append_file("module_shutdown");
35118
true
36119
}
37120

38-
#[php_rinit_function]
39-
fn request_init(_args: ModuleArgs) -> bool {
121+
fn request_init(args: ModuleArgs) -> bool {
122+
// append_file("request_init");
40123
true
41124
}
42125

43-
#[php_rshutdown_function]
44-
fn request_shutdown(_args: ModuleArgs) -> bool {
126+
fn request_shutdown(args: ModuleArgs) -> bool {
127+
// append_file("request_shutdown");
45128
true
46129
}
47130

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();
65137

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();
71139
}
72140

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() }
99180
}

examples/mini-curl/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
2-
name = "mini_curl"
3-
version = "0.2.0"
2+
name = "mini-curl"
3+
version = "0.0.0"
44
authors = ["jmjoy <[email protected]>"]
55
edition = "2018"
66
publish = false

examples/mini-curl/src/lib.rs

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,3 @@
1-
use curl::easy::Easy;
2-
use phper::{
3-
c_str_ptr,
4-
main::php::error_doc_ref,
5-
php_fn, php_function, php_minfo, php_minfo_function, php_minit, php_minit_function,
6-
php_mshutdown, php_mshutdown_function, php_rinit, php_rinit_function, php_rshutdown,
7-
php_rshutdown_function,
8-
sys::{php_info_print_table_end, php_info_print_table_start, PHP_INI_SYSTEM},
9-
zend::{
10-
api::{FunctionEntries, FunctionEntryBuilder},
11-
compile::{create_zend_arg_info, MultiInternalArgInfo, Visibility},
12-
errors::Level,
13-
ini::{create_ini_entry, IniEntries},
14-
modules::{ModuleArgs, ModuleEntry, ModuleEntryBuilder},
15-
types::{ClassEntry, ExecuteData, ReturnValue, SetVal, Value},
16-
},
17-
php_get_module,
18-
};
19-
20-
static MINI_CURL_CE: ClassEntry = ClassEntry::new();
21-
22-
static INI_ENTRIES: IniEntries<1> =
23-
IniEntries::new([create_ini_entry("curl.cainfo", "", PHP_INI_SYSTEM)]);
24-
251
#[php_minit_function]
262
fn module_init(args: ModuleArgs) -> bool {
273
args.register_ini_entries(&INI_ENTRIES);
@@ -155,7 +131,32 @@ static MODULE_ENTRY: ModuleEntry = ModuleEntryBuilder::new(
155131
.info_func(php_minfo!(module_info))
156132
.build();
157133

158-
#[php_get_module]
159-
pub fn get_module() -> &'static ModuleEntry {
160-
&MODULE_ENTRY
134+
135+
fn curl_init() {
136+
137+
}
138+
139+
140+
#[get_module]
141+
pub fn get_module() -> Module {
142+
let module = Module::new(env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION"));
143+
144+
module.on_module_startup(|module_args: ModuleArgs| -> bool {
145+
true
146+
});
147+
module.on_module_shutdown(|module_args: ModuleArgs| -> bool {
148+
true
149+
});
150+
module.on_request_startup(|module_args: ModuleArgs| -> bool {
151+
true
152+
});
153+
module.on_request_shutdown(|module_args: ModuleArgs| -> bool {
154+
true
155+
});
156+
157+
module.add_ini("curl.cainfo", "???", INI::System);
158+
159+
module.add_function("curl_init", curl_init);
160+
161+
module
161162
}

phper-macros/src/inner.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ pub(crate) fn php_get_module(_attr: TokenStream, input: TokenStream) -> TokenStr
110110
fn internal(#inputs) #ret {
111111
#body
112112
}
113-
let internal: fn() -> &'static ::phper::zend::modules::ModuleEntry = internal;
113+
let internal: fn() -> &::phper::zend::modules::ModuleEntry = internal;
114114
internal().as_ptr()
115115
}
116116
};

phper-sys/php_wrapper.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,4 @@ char *phper_z_strval_p(const zval *v) {
3232

3333
zval *phper_get_this(zend_execute_data *execute_data) {
3434
return getThis();
35-
}
35+
}

phper/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ keywords = ["php", "binding", "extension"]
1212
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
1313

1414
[dependencies]
15+
once_cell = "1.7.2"
1516
phper-alloc = { version = "0.2", path = "../phper-alloc" }
1617
phper-macros = { version = "0.2", path = "../phper-macros" }
1718
phper-sys = { version = "0.2", path = "../phper-sys" }

0 commit comments

Comments
 (0)