|
| 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