Skip to content

Commit 6dd741c

Browse files
committed
Add example mini-curl and optional parameters support.
1 parent b82ef7c commit 6dd741c

File tree

16 files changed

+464
-38
lines changed

16 files changed

+464
-38
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ members = [
1010
# internal
1111
"examples/hello",
1212
"examples/hello-class",
13+
"examples/mini-curl",
1314
]

examples/hello-class/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ version = "0.2.0"
44
authors = ["jmjoy <[email protected]>"]
55
edition = "2018"
66
publish = false
7+
license = "Unlicense"
78

89
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
910

examples/hello-class/src/lib.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ static HELLO_CLASS_ENABLE: ModuleGlobals<bool> = ModuleGlobals::new(false);
2323
static HELLO_CLASS_DESCRIPTION: ModuleGlobals<*const c_char> = ModuleGlobals::new(null());
2424

2525
static INI_ENTRIES: IniEntries<2> = IniEntries::new([
26-
HELLO_CLASS_ENABLE.create_ini_entry_def(
26+
HELLO_CLASS_ENABLE.create_ini_entry(
2727
"hello_class.enable",
2828
"1",
2929
Some(OnUpdateBool),
3030
PHP_INI_SYSTEM,
3131
),
32-
HELLO_CLASS_DESCRIPTION.create_ini_entry_def(
32+
HELLO_CLASS_DESCRIPTION.create_ini_entry(
3333
"hello_class.description",
3434
"",
3535
Some(OnUpdateString),
@@ -93,8 +93,11 @@ fn module_info(module: &ModuleEntry) {
9393
}
9494
}
9595

96-
static ARG_INFO_HELLO_CLASS_SAY_HELLO: MultiInternalArgInfo<1> =
97-
MultiInternalArgInfo::new([create_zend_arg_info(c_str_ptr!("prefix"), false)], false);
96+
static ARG_INFO_HELLO_CLASS_SAY_HELLO: MultiInternalArgInfo<1> = MultiInternalArgInfo::new(
97+
1,
98+
false,
99+
[create_zend_arg_info(c_str_ptr!("prefix"), false)],
100+
);
98101

99102
static HELLO_CLASS_METHODS: FunctionEntries<1> = FunctionEntries::new([zend_function_entry {
100103
fname: c_str_ptr!("sayHello"),

examples/hello/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ version = "0.2.0"
44
authors = ["jmjoy <[email protected]>"]
55
edition = "2018"
66
publish = false
7+
license = "Unlicense"
78

89
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
910

examples/hello/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use phper::{
1818

1919
static SIMPLE_ENABLE: ModuleGlobals<bool> = ModuleGlobals::new(false);
2020

21-
static INI_ENTRIES: IniEntries<1> = IniEntries::new([SIMPLE_ENABLE.create_ini_entry_def(
21+
static INI_ENTRIES: IniEntries<1> = IniEntries::new([SIMPLE_ENABLE.create_ini_entry(
2222
"hello.enable",
2323
"1",
2424
Some(OnUpdateBool),
@@ -73,7 +73,7 @@ pub fn say_hello(execute_data: &mut ExecuteData) -> impl SetVal {
7373
}
7474

7575
static ARG_INFO_SAY_HELLO: MultiInternalArgInfo<1> =
76-
MultiInternalArgInfo::new([create_zend_arg_info(c_str_ptr!("name"), false)], false);
76+
MultiInternalArgInfo::new(1, false, [create_zend_arg_info(c_str_ptr!("name"), false)]);
7777

7878
static FUNCTION_ENTRIES: FunctionEntries<1> = FunctionEntries::new([zend_function_entry {
7979
fname: c_str_ptr!("say_hello"),

examples/mini-curl/Cargo.toml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
[package]
2+
name = "mini_curl"
3+
version = "0.2.0"
4+
authors = ["jmjoy <[email protected]>"]
5+
edition = "2018"
6+
publish = false
7+
license = "Unlicense"
8+
9+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
10+
11+
[lib]
12+
crate-type = ["cdylib"]
13+
14+
[dependencies]
15+
curl = "0.4.34"
16+
phper = { version = "0.2", path = "../../phper" }
17+
18+
[dev-dependencies]
19+
phper-test = { version = "0.2", path = "../../phper-test" }
20+
21+
[build-dependencies]
22+
phper-build = { version = "0.2", path = "../../phper-build" }

examples/mini-curl/Makefile.toml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
[env]
2+
PHP_CONFIG = "php-config"
3+
TARGET_DIR = "${CARGO_MAKE_WORKING_DIRECTORY}/../../target"
4+
5+
[env.development]
6+
TARGET_BUILD_DIR = "${TARGET_DIR}/debug"
7+
BUILD_ARGS = "--"
8+
TEST_ARGS = "--"
9+
10+
[env.production]
11+
TARGET_BUILD_DIR = "${TARGET_DIR}/release"
12+
BUILD_ARGS = "--release"
13+
TEST_ARGS = "--release"
14+
15+
[tasks.build]
16+
command = "cargo"
17+
args = ["build", "${BUILD_ARGS}"]
18+
19+
[tasks.test]
20+
command = "cargo"
21+
args = ["test", "${TEST_ARGS}", "--", "--nocapture"]
22+
23+
[tasks.install]
24+
dependencies = ["build"]
25+
script = [
26+
"""
27+
cp ${TARGET_BUILD_DIR}/lib${CARGO_MAKE_CRATE_NAME}.so `${PHP_CONFIG} --extension-dir`/${CARGO_MAKE_CRATE_NAME}.so && \
28+
echo Installing shared extensions: `${PHP_CONFIG} --extension-dir`
29+
"""
30+
]

examples/mini-curl/build.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fn main() {
2+
phper_build::register_configures();
3+
}

examples/mini-curl/src/lib.rs

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
use phper::{
2+
c_str_ptr, php_fn, php_function, php_minfo, php_minfo_function, php_minit, php_minit_function,
3+
php_mshutdown, php_mshutdown_function, php_rinit, php_rinit_function, php_rshutdown,
4+
php_rshutdown_function,
5+
sys::{php_info_print_table_end, php_info_print_table_start, PHP_INI_SYSTEM},
6+
zend::{
7+
api::{FunctionEntries, FunctionEntryBuilder},
8+
compile::{create_zend_arg_info, MultiInternalArgInfo},
9+
ini::{create_ini_entry, IniEntries},
10+
modules::{create_zend_module_entry, ModuleArgs, ModuleEntry},
11+
types::{ClassEntry, ExecuteData, ReturnValue, SetVal},
12+
},
13+
zend_get_module,
14+
};
15+
use std::ptr::null;
16+
17+
static MINI_CURL_CE: ClassEntry = ClassEntry::new();
18+
19+
static INI_ENTRIES: IniEntries<1> =
20+
IniEntries::new([create_ini_entry("curl.cainfo", "", PHP_INI_SYSTEM)]);
21+
22+
#[php_minit_function]
23+
fn module_init(args: ModuleArgs) -> bool {
24+
args.register_ini_entries(&INI_ENTRIES);
25+
MINI_CURL_CE.init("MiniCurl", &MINI_CURL_METHODS);
26+
true
27+
}
28+
29+
#[php_mshutdown_function]
30+
fn module_shutdown(args: ModuleArgs) -> bool {
31+
args.unregister_ini_entries();
32+
true
33+
}
34+
35+
#[php_rinit_function]
36+
fn request_init(_args: ModuleArgs) -> bool {
37+
true
38+
}
39+
40+
#[php_rshutdown_function]
41+
fn request_shutdown(_args: ModuleArgs) -> bool {
42+
true
43+
}
44+
45+
#[php_minfo_function]
46+
fn module_info(__module: &ModuleEntry) {
47+
unsafe {
48+
php_info_print_table_start();
49+
php_info_print_table_end();
50+
}
51+
}
52+
53+
static ARG_INFO_VOID: MultiInternalArgInfo<0> = MultiInternalArgInfo::new(0, false, []);
54+
55+
static ARG_INFO_MINI_CURL_CONSTRUCT: MultiInternalArgInfo<1> =
56+
MultiInternalArgInfo::new(0, false, [create_zend_arg_info(c_str_ptr!("url"), false)]);
57+
58+
static MINI_CURL_METHODS: FunctionEntries<2> = FunctionEntries::new([
59+
FunctionEntryBuilder::new(
60+
c_str_ptr!("__construct"),
61+
Some(php_fn!(mini_curl_construct)),
62+
)
63+
.arg_info(&ARG_INFO_MINI_CURL_CONSTRUCT)
64+
.build(),
65+
FunctionEntryBuilder::new(c_str_ptr!("__destruct"), Some(php_fn!(mini_curl_destruct)))
66+
.arg_info(&ARG_INFO_VOID)
67+
.build(),
68+
]);
69+
70+
#[php_function]
71+
pub fn mini_curl_construct(execute_data: &mut ExecuteData) -> impl SetVal {
72+
match execute_data
73+
.parse_parameters_optional("")
74+
.map(|_url: &str| {})
75+
{
76+
Some(_) => ReturnValue::Bool(true),
77+
None => ReturnValue::Bool(false),
78+
}
79+
}
80+
81+
#[php_function]
82+
pub fn mini_curl_destruct(execute_data: &mut ExecuteData) -> impl SetVal {
83+
if execute_data.parse_parameters::<()>().is_none() {
84+
return ReturnValue::Bool(false);
85+
}
86+
87+
ReturnValue::Null
88+
}
89+
90+
static MODULE_ENTRY: ModuleEntry = ModuleEntry::new(create_zend_module_entry(
91+
c_str_ptr!(env!("CARGO_PKG_NAME")),
92+
c_str_ptr!(env!("CARGO_PKG_VERSION")),
93+
null(),
94+
Some(php_minit!(module_init)),
95+
Some(php_mshutdown!(module_shutdown)),
96+
Some(php_rinit!(request_init)),
97+
Some(php_rshutdown!(request_shutdown)),
98+
Some(php_minfo!(module_info)),
99+
None,
100+
None,
101+
));
102+
103+
#[zend_get_module]
104+
pub fn get_module() -> &'static ModuleEntry {
105+
&MODULE_ENTRY
106+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
use phper_test::test_php_scripts;
2+
use std::{env, path::Path};
3+
4+
#[test]
5+
fn test_php() {
6+
test_php_scripts(
7+
Path::new(env!("CARGO_MANIFEST_DIR"))
8+
.join("..")
9+
.join("..")
10+
.join("target"),
11+
env!("CARGO_PKG_NAME"),
12+
&[Path::new(env!("CARGO_MANIFEST_DIR"))
13+
.join("tests")
14+
.join("php")
15+
.join("test.php")],
16+
);
17+
}

0 commit comments

Comments
 (0)