Skip to content

Commit 8f58b49

Browse files
authored
Add simple example hello. (#76)
1 parent fe59a32 commit 8f58b49

File tree

5 files changed

+108
-0
lines changed

5 files changed

+108
-0
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ members = [
1919

2020
# internal
2121
"examples/complex",
22+
"examples/hello",
2223
"examples/http-client",
2324
"examples/http-server",
2425
"examples/logging",

examples/hello/Cargo.toml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Copyright (c) 2022 PHPER Framework Team
2+
# PHPER is licensed under Mulan PSL v2.
3+
# You can use this software according to the terms and conditions of the Mulan
4+
# PSL v2. You may obtain a copy of Mulan PSL v2 at:
5+
# http://license.coscl.org.cn/MulanPSL2
6+
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY
7+
# KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
8+
# NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
9+
# See the Mulan PSL v2 for more details.
10+
11+
[package]
12+
name = "hello"
13+
version = "0.0.0"
14+
publish = false
15+
authors = { workspace = true }
16+
edition = { workspace = true }
17+
rust-version = { workspace = true }
18+
license = { workspace = true }
19+
20+
[lib]
21+
crate-type = ["lib", "cdylib"]
22+
23+
[dependencies]
24+
phper = { version = "0.6.0", path = "../../phper" }

examples/hello/README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# hello
2+
3+
Hello world example.
4+
5+
## Environment
6+
7+
```bash
8+
# Optional, specify if php isn't installed globally.
9+
export PHP_CONFIG=<Your path of php-config>
10+
```
11+
12+
## Build
13+
14+
```bash
15+
cargo build --release
16+
```
17+
18+
## Run
19+
20+
```bash
21+
php -d "extension=target/release/libhello.so" -r "say_hello('Bob');"
22+
```
23+
24+
## License
25+
26+
[MulanPSL-2.0](https://github.com/phper-framework/phper/blob/master/LICENSE).

examples/hello/build.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright (c) 2022 PHPER Framework Team
2+
// PHPER is licensed under Mulan PSL v2.
3+
// You can use this software according to the terms and conditions of the Mulan
4+
// PSL v2. You may obtain a copy of Mulan PSL v2 at:
5+
// http://license.coscl.org.cn/MulanPSL2
6+
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY
7+
// KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
8+
// NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
9+
// See the Mulan PSL v2 for more details.
10+
11+
fn main() {
12+
#[cfg(target_os = "macos")]
13+
{
14+
println!("cargo:rustc-link-arg=-undefined");
15+
println!("cargo:rustc-link-arg=dynamic_lookup");
16+
}
17+
}

examples/hello/src/lib.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright (c) 2022 PHPER Framework Team
2+
// PHPER is licensed under Mulan PSL v2.
3+
// You can use this software according to the terms and conditions of the Mulan
4+
// PSL v2. You may obtain a copy of Mulan PSL v2 at:
5+
// http://license.coscl.org.cn/MulanPSL2
6+
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY
7+
// KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
8+
// NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
9+
// See the Mulan PSL v2 for more details.
10+
11+
use phper::{echo, functions::Argument, modules::Module, php_get_module, values::ZVal};
12+
13+
/// The php function, receive arguments with type `ZVal`.
14+
fn say_hello(arguments: &mut [ZVal]) -> phper::Result<()> {
15+
// Get the first argument, expect the type `ZStr`, and convert to Rust utf-8
16+
// str.
17+
let name = arguments[0].expect_z_str()?.to_str()?;
18+
19+
// Macro which do php internal `echo`.
20+
echo!("Hello, {}!\n", name);
21+
22+
Ok(())
23+
}
24+
25+
/// This is the entry of php extension, the attribute macro `php_get_module`
26+
/// will generate the `extern "C" fn`.
27+
#[php_get_module]
28+
pub fn get_module() -> Module {
29+
// New `Module` with extension info.
30+
let mut module = Module::new(
31+
env!("CARGO_CRATE_NAME"),
32+
env!("CARGO_PKG_VERSION"),
33+
env!("CARGO_PKG_AUTHORS"),
34+
);
35+
36+
// Register function `say_hello`, with one argument `name`.
37+
module.add_function("say_hello", say_hello, vec![Argument::by_val("name")]);
38+
39+
module
40+
}

0 commit comments

Comments
 (0)