Skip to content

Commit af6d07a

Browse files
authored
Add Module::add_info method, supplement module document. (#98)
1 parent 219e9a8 commit af6d07a

File tree

11 files changed

+454
-10
lines changed

11 files changed

+454
-10
lines changed

examples/complex/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,5 +94,8 @@ pub fn get_module() -> Module {
9494
.argument(Argument::by_val("foo"));
9595
module.add_class(foo_class);
9696

97+
// register extra info
98+
module.add_info("extra info key", "extra info value");
99+
97100
module
98101
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Hooks
2+
3+
> PHP is a complex piece of machinery whose lifecycle really should be understood
4+
> by anyone who wants to understand how PHP operates.
5+
>
6+
> Refer: <https://www.phpinternalsbook.com/php7/extensions_design/php_lifecycle.html>
7+
8+
PHP provides many hooks in lifecycle for extension to override.
9+
10+
There are `MINIT`, `MSHUTDOWN`, `RINIT`, `RSHUTDOWN`, `GINIT`, `RSHUTDOWN`.
11+
12+
Correspondingly, `PHPER` sets these hooks to complete some internal operations,
13+
such as registering extension information, registering functions, classes,
14+
constants, etc., but also exposes these hooks to users to overwrite.
15+
16+
The following is the corresponding relationship between PHP hooks and `Module`
17+
methods:
18+
19+
| PHP hooks | `Module` method |
20+
| --------- | -------------------------------------------------------- |
21+
| MINIT | [on_module_init](phper::modules::Module::on_module_init) |
22+
| MSHUTDOWN | [on_module_shutdown](phper::modules::Module::on_module_shutdown) |
23+
| RINIT | [on_request_init](phper::modules::Module::on_request_init) |
24+
| RSHUTDOWN | [on_request_shutdown](phper::modules::Module::on_request_shutdown) |
25+
26+
27+
```rust,no_run
28+
use phper::{modules::Module, php_get_module};
29+
30+
#[php_get_module]
31+
pub fn get_module() -> Module {
32+
let mut module = Module::new(
33+
env!("CARGO_CRATE_NAME"),
34+
env!("CARGO_PKG_VERSION"),
35+
env!("CARGO_PKG_AUTHORS"),
36+
);
37+
38+
module.on_module_init(|_| {
39+
// Do somethings in `MINIT` stage.
40+
true
41+
});
42+
43+
module
44+
}
45+
```
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# Register functions
2+
3+
In `PHPER`, you can use [`add_function`](phper::modules::Module::add_function) to
4+
register functions.
5+
6+
```rust,no_run
7+
use phper::{modules::Module, php_get_module, functions::Argument, echo};
8+
9+
#[php_get_module]
10+
pub fn get_module() -> Module {
11+
let mut module = Module::new(
12+
env!("CARGO_CRATE_NAME"),
13+
env!("CARGO_PKG_VERSION"),
14+
env!("CARGO_PKG_AUTHORS"),
15+
);
16+
17+
module.add_function("say_hello", |arguments| -> phper::Result<()> {
18+
let name = arguments[0].expect_z_str()?.to_str()?;
19+
echo!("Hello, {}!\n", name);
20+
Ok(())
21+
}).argument(Argument::by_val("name"));
22+
23+
module
24+
}
25+
```
26+
27+
This example registers a function called `say_hello` and accepts a parameter
28+
`name` passed by value, just like these codes in PHP:
29+
30+
```php
31+
<?php
32+
33+
function say_hello($name) {
34+
echo "Hello, {$name}\n";
35+
}
36+
```
37+
38+
You can get the function info by `php --re <EXTENSION_NAME>`:
39+
40+
```txt
41+
Extension [ <persistent> extension #56 hello version <no_version> ] {
42+
43+
- Functions {
44+
Function [ <internal:hello> function say_hello ] {
45+
46+
- Parameters [1] {
47+
Parameter #0 [ <required> $name ]
48+
}
49+
}
50+
}
51+
}
52+
```
53+
54+
It is useful to register the parameters of the function, which can limit the
55+
number of parameters of the function.
56+
57+
Especially, when the parameters need to be passed by reference.
58+
59+
```rust,no_run
60+
use phper::{modules::Module, php_get_module, functions::Argument};
61+
62+
#[php_get_module]
63+
pub fn get_module() -> Module {
64+
let mut module = Module::new(
65+
env!("CARGO_CRATE_NAME"),
66+
env!("CARGO_PKG_VERSION"),
67+
env!("CARGO_PKG_AUTHORS"),
68+
);
69+
70+
module.add_function("add_count", |arguments| -> phper::Result<()> {
71+
let count = arguments[0].expect_mut_z_ref()?;
72+
*count.val_mut().expect_mut_long()? += 100;
73+
Ok(())
74+
}).argument(Argument::by_ref("count"));
75+
76+
module
77+
}
78+
```
79+
80+
Here, the argument is registered as
81+
[`Argument::by_ref`](phper::functions::Argument::by_ref). Therefore, the type of
82+
the `count` parameter is no longer long, but a reference.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Register constants
2+
3+
In `PHPER`, you can use [`add_constant`](phper::modules::Module::add_constant) to
4+
register constants.
5+
6+
```rust,no_run
7+
use phper::{modules::Module, php_get_module};
8+
9+
#[php_get_module]
10+
pub fn get_module() -> Module {
11+
let mut module = Module::new(
12+
env!("CARGO_CRATE_NAME"),
13+
env!("CARGO_PKG_VERSION"),
14+
env!("CARGO_PKG_AUTHORS"),
15+
);
16+
17+
module.add_constant("FOO", 100i64);
18+
19+
module
20+
}
21+
```
22+
23+
Because in PHP, you can also use copyable values as constants, such as long,
24+
double and string, so the value have to implement [`Scalar`](phper::types::Scalar).
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Register ini settings
2+
3+
In `PHPER`, you can use [`add_ini`](phper::modules::Module::add_ini) to
4+
register ini settings.
5+
6+
```rust,no_run
7+
use phper::{modules::Module, php_get_module, ini::Policy};
8+
9+
#[php_get_module]
10+
pub fn get_module() -> Module {
11+
let mut module = Module::new(
12+
env!("CARGO_CRATE_NAME"),
13+
env!("CARGO_PKG_VERSION"),
14+
env!("CARGO_PKG_AUTHORS"),
15+
);
16+
17+
module.add_ini("demo.enable", false, Policy::All);
18+
module.add_ini("demo.foo", 100, Policy::All);
19+
20+
module
21+
}
22+
```
23+
24+
About the policy of setting, you can refer to
25+
<https://www.php.net/manual/en/configuration.changes.modes.php>.
26+
27+
## Configure ini settings
28+
29+
The user can configure the ini settings in the `php.ini`. if not configure, the
30+
configuration item will use the default value.
31+
32+
```ini
33+
demo.enable = On
34+
```
35+
36+
You can show the ini settings by `php --ri <EXTENSION_NAME>`.
37+
38+
```txt
39+
demo
40+
41+
version => 0.0.0
42+
authors => PHPER Framework Team:jmjoy <[email protected]>
43+
44+
Directive => Local Value => Master Value
45+
demo.enable => 1 => 1
46+
demo.num => 100 => 100
47+
```
48+
49+
## Get ini settings
50+
51+
After the ini settings registered, you can get it by
52+
[`ini_get`](phper::ini::ini_get).
53+
54+
```rust,no_run
55+
use phper::ini::ini_get;
56+
57+
let _foo = ini_get::<bool>("demo.enable");
58+
let _bar = ini_get::<i64>("demo.foo");
59+
```
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Extension information
2+
3+
By default, `PHPER` will auto register the `MINFO` handle, show the info about
4+
extension name, version, authors, and display configuration items.
5+
6+
As you execute the command `php --ri <EXTENSION_NAME>`:
7+
8+
```txt
9+
demo
10+
11+
version => 0.0.0
12+
authors => PHPER Framework Team:jmjoy <[email protected]>
13+
14+
Directive => Local Value => Master Value
15+
complex.enable => 0 => 0
16+
complex.foo => 100 => 100
17+
```
18+
19+
If you want to add extra info items, you can use
20+
[`Module::add_info`](phper::modules::Module::add_info) method.
21+
22+
```rust,no_run
23+
use phper::{modules::Module, php_get_module};
24+
25+
#[php_get_module]
26+
pub fn get_module() -> Module {
27+
let mut module = Module::new(
28+
env!("CARGO_CRATE_NAME"),
29+
env!("CARGO_PKG_VERSION"),
30+
env!("CARGO_PKG_AUTHORS"),
31+
);
32+
33+
module.add_info("extra info key", "extra info value");
34+
35+
module
36+
}
37+
```
38+
39+
Then build the extension and call `php --ri <EXTENSION_NAME>`:
40+
41+
```txt
42+
demo
43+
44+
version => 0.0.0
45+
authors => PHPER Framework Team:jmjoy <[email protected]>
46+
extra info key => extra info value
47+
48+
Directive => Local Value => Master Value
49+
complex.enable => 0 => 0
50+
complex.foo => 100 => 100
51+
```
52+
53+
The `extra info key` item is appeared.

0 commit comments

Comments
 (0)