Skip to content

Commit b82ef7c

Browse files
committed
Separate into two examples.
1 parent f6a3674 commit b82ef7c

File tree

21 files changed

+420
-179
lines changed

21 files changed

+420
-179
lines changed

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@ members = [
88
"phper-test",
99

1010
# internal
11-
"examples/simple",
11+
"examples/hello",
12+
"examples/hello-class",
1213
]

examples/hello-class/Cargo.toml

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

examples/simple/Makefile.toml renamed to examples/hello-class/Makefile.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ args = ["build", "${BUILD_ARGS}"]
1919
[tasks.test]
2020
command = "cargo"
2121
args = ["test", "${TEST_ARGS}", "--", "--nocapture"]
22-
env = { "LIB_SO" = "${TARGET_BUILD_DIR}/lib${CARGO_MAKE_CRATE_NAME}.so" }
2322

2423
[tasks.install]
2524
dependencies = ["build"]
File renamed without changes.

examples/hello-class/src/lib.rs

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
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::{
6+
php_info_print_table_end, php_info_print_table_row, php_info_print_table_start,
7+
zend_function_entry, OnUpdateBool, OnUpdateString, PHP_INI_SYSTEM,
8+
},
9+
zend::{
10+
api::{FunctionEntries, ModuleGlobals},
11+
compile::{create_zend_arg_info, MultiInternalArgInfo, Visibility},
12+
ini::IniEntries,
13+
modules::{create_zend_module_entry, ModuleArgs, ModuleEntry},
14+
types::{ClassEntry, ExecuteData, SetVal, Value},
15+
},
16+
zend_get_module,
17+
};
18+
use std::{os::raw::c_char, ptr::null};
19+
20+
static HELLO_CLASS_CE: ClassEntry = ClassEntry::new();
21+
22+
static HELLO_CLASS_ENABLE: ModuleGlobals<bool> = ModuleGlobals::new(false);
23+
static HELLO_CLASS_DESCRIPTION: ModuleGlobals<*const c_char> = ModuleGlobals::new(null());
24+
25+
static INI_ENTRIES: IniEntries<2> = IniEntries::new([
26+
HELLO_CLASS_ENABLE.create_ini_entry_def(
27+
"hello_class.enable",
28+
"1",
29+
Some(OnUpdateBool),
30+
PHP_INI_SYSTEM,
31+
),
32+
HELLO_CLASS_DESCRIPTION.create_ini_entry_def(
33+
"hello_class.description",
34+
"",
35+
Some(OnUpdateString),
36+
PHP_INI_SYSTEM,
37+
),
38+
]);
39+
40+
#[php_minit_function]
41+
fn module_init(args: ModuleArgs) -> bool {
42+
args.register_ini_entries(&INI_ENTRIES);
43+
HELLO_CLASS_CE.init("HelloClass", &HELLO_CLASS_METHODS);
44+
HELLO_CLASS_CE.declare_property("name", "world", Visibility::Public);
45+
true
46+
}
47+
48+
#[php_mshutdown_function]
49+
fn module_shutdown(args: ModuleArgs) -> bool {
50+
args.unregister_ini_entries();
51+
true
52+
}
53+
54+
#[php_rinit_function]
55+
fn request_init(_args: ModuleArgs) -> bool {
56+
true
57+
}
58+
59+
#[php_rshutdown_function]
60+
fn request_shutdown(_args: ModuleArgs) -> bool {
61+
true
62+
}
63+
64+
#[php_minfo_function]
65+
fn module_info(module: &ModuleEntry) {
66+
unsafe {
67+
php_info_print_table_start();
68+
php_info_print_table_row(
69+
2,
70+
c_str_ptr!("hello_class.version"),
71+
(*module.as_ptr()).version,
72+
);
73+
php_info_print_table_row(
74+
2,
75+
c_str_ptr!("hello_class.build_id"),
76+
(*module.as_ptr()).build_id,
77+
);
78+
php_info_print_table_row(
79+
2,
80+
c_str_ptr!("hello_class.enable"),
81+
if HELLO_CLASS_ENABLE.get() {
82+
c_str_ptr!("1")
83+
} else {
84+
c_str_ptr!("0")
85+
},
86+
);
87+
php_info_print_table_row(
88+
2,
89+
c_str_ptr!("hello_class.description"),
90+
HELLO_CLASS_DESCRIPTION.get(),
91+
);
92+
php_info_print_table_end();
93+
}
94+
}
95+
96+
static ARG_INFO_HELLO_CLASS_SAY_HELLO: MultiInternalArgInfo<1> =
97+
MultiInternalArgInfo::new([create_zend_arg_info(c_str_ptr!("prefix"), false)], false);
98+
99+
static HELLO_CLASS_METHODS: FunctionEntries<1> = FunctionEntries::new([zend_function_entry {
100+
fname: c_str_ptr!("sayHello"),
101+
handler: Some(php_fn!(hello_class_get_hello)),
102+
arg_info: ARG_INFO_HELLO_CLASS_SAY_HELLO.as_ptr(),
103+
num_args: 1,
104+
flags: 0,
105+
}]);
106+
107+
#[php_function]
108+
pub fn hello_class_get_hello(execute_data: &mut ExecuteData) -> impl SetVal {
109+
execute_data.parse_parameters::<()>().map(|_| {
110+
let this = execute_data.get_this();
111+
let val = HELLO_CLASS_CE.read_property(this, "name");
112+
let value = val.try_into_value().unwrap();
113+
114+
if let Value::CStr(value) = value {
115+
Some(format!("Hello, {}!", value.to_str().unwrap()))
116+
} else {
117+
None
118+
}
119+
})
120+
}
121+
122+
static FUNCTION_ENTRIES: FunctionEntries<1> = FunctionEntries::new([zend_function_entry {
123+
fname: c_str_ptr!("hello_class_get_hello"),
124+
handler: Some(php_fn!(hello_class_get_hello)),
125+
arg_info: null(),
126+
num_args: 0,
127+
flags: 0,
128+
}]);
129+
130+
static MODULE_ENTRY: ModuleEntry = ModuleEntry::new(create_zend_module_entry(
131+
c_str_ptr!(env!("CARGO_PKG_NAME")),
132+
c_str_ptr!(env!("CARGO_PKG_VERSION")),
133+
FUNCTION_ENTRIES.as_ptr(),
134+
Some(php_minit!(module_init)),
135+
Some(php_mshutdown!(module_shutdown)),
136+
Some(php_rinit!(request_init)),
137+
Some(php_rshutdown!(request_shutdown)),
138+
Some(php_minfo!(module_info)),
139+
None,
140+
None,
141+
));
142+
143+
#[zend_get_module]
144+
pub fn get_module() -> &'static ModuleEntry {
145+
&MODULE_ENTRY
146+
}

examples/simple/tests/php/test.php renamed to examples/hello-class/tests/php/test.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44
ini_set("display_startup_errors", "On");
55
error_reporting(E_ALL);
66

7-
assert_eq(get_extension_funcs('simple'), ["test_simple"]);
8-
assert_eq(test_simple("aaa", "bbb"), "a = aaa, a_len = 3, b = bbb, b_len = 3");
9-
assert_eq((new MyClass())->hello("hello, "), "hello, world");
7+
assert_eq((new HelloClass())->sayHello(), "Hello, world!");
108

119
function assert_eq($left, $right) {
1210
if ($left !== $right) {

examples/simple/Cargo.toml renamed to examples/hello/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[package]
2-
name = "simple"
2+
name = "hello"
33
version = "0.2.0"
44
authors = ["jmjoy <[email protected]>"]
55
edition = "2018"

examples/hello/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/hello/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+
}

0 commit comments

Comments
 (0)