Skip to content

Commit f9baa99

Browse files
committed
docs(macro): update documentation with new #[php] attr
Refs: #391
1 parent 4a508a6 commit f9baa99

File tree

9 files changed

+278
-128
lines changed

9 files changed

+278
-128
lines changed

crates/macros/src/class.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ pub struct StructAttributes {
1414
/// The name of the PHP class. Defaults to the same name as the struct.
1515
#[darling(flatten)]
1616
rename: PhpRename,
17+
/// A modifier function which should accept one argument, a `ClassBuilder`,
18+
/// and return the same object. Allows the user to modify the class before
19+
/// it is built.
1720
modifier: Option<syn::Ident>,
1821
/// An expression of `ClassFlags` to be applied to the class.
1922
flags: Option<syn::Expr>,

crates/macros/src/constant.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use quote::{format_ident, quote};
44
use syn::ItemConst;
55

66
use crate::helpers::get_docs;
7-
use crate::parsing::{PhpRename, Visibility};
7+
use crate::parsing::PhpRename;
88
use crate::prelude::*;
99

1010
const INTERNAL_CONST_DOC_PREFIX: &str = "_internal_const_docs_";
@@ -15,7 +15,8 @@ const INTERNAL_CONST_NAME_PREFIX: &str = "_internal_const_name_";
1515
pub(crate) struct PhpConstAttribute {
1616
#[darling(flatten)]
1717
pub(crate) rename: PhpRename,
18-
pub(crate) vis: Option<Visibility>,
18+
// TODO: Implement const Visibility
19+
// pub(crate) vis: Option<Visibility>,
1920
pub(crate) attrs: Vec<syn::Attribute>,
2021
}
2122

crates/macros/src/lib.rs

Lines changed: 143 additions & 116 deletions
Large diffs are not rendered by default.

guide/src/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
- [Constants](./macros/constant.md)
3333
- [PHP Functions](./macros/extern.md)
3434
- [`ZvalConvert`](./macros/zval_convert.md)
35+
- [`Attributes`](./macros/php.md)
3536
- [Exceptions](./exceptions.md)
3637
- [INI Settings](./ini-settings.md)
3738

guide/src/macros/constant.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@ that implements `IntoConst`.
66
The `wrap_constant!()` macro can be used to simplify the registration of constants.
77
It sets the name and doc comments for the constant.
88

9+
You can rename the const with options:
10+
11+
- `name` - Allows you to rename the property, e.g.
12+
`#[php(name = "new_name")]`
13+
- `rename` - Allows you to rename the property using rename rules, e.g.
14+
`#[php(rename = PascalCase)]`
15+
916
## Examples
1017

1118
```rust,no_run
@@ -16,13 +23,18 @@ use ext_php_rs::prelude::*;
1623
#[php_const]
1724
const TEST_CONSTANT: i32 = 100;
1825
26+
#[php_const]
27+
#[php(name = "I_AM_RENAMED")]
28+
const TEST_CONSTANT_THE_SECOND: i32 = 42;
29+
1930
#[php_const]
2031
const ANOTHER_STRING_CONST: &'static str = "Hello world!";
2132
2233
#[php_module]
2334
pub fn get_module(module: ModuleBuilder) -> ModuleBuilder {
2435
module
2536
.constant(wrap_constant!(TEST_CONSTANT))
37+
.constant(wrap_constant!(TEST_CONSTANT_THE_SECOND))
2638
.constant(("MANUAL_CONSTANT", ANOTHER_STRING_CONST, &[]))
2739
}
2840
# fn main() {}
@@ -34,5 +46,6 @@ pub fn get_module(module: ModuleBuilder) -> ModuleBuilder {
3446
<?php
3547

3648
var_dump(TEST_CONSTANT); // int(100)
49+
var_dump(I_AM_RENAMED); // int(42)
3750
var_dump(MANUAL_CONSTANT); // string(12) "Hello world!"
3851
```

guide/src/macros/function.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,16 @@ pub fn get_module(module: ModuleBuilder) -> ModuleBuilder {
3737
```
3838

3939
Default parameter values can also be set for optional parameters. This is done
40-
through the `defaults` attribute option. When an optional parameter has a
40+
through the `#[php(defaults)]` attribute option. When an optional parameter has a
4141
default, it does not need to be a variant of `Option`:
4242

4343
```rust,no_run
4444
# #![cfg_attr(windows, feature(abi_vectorcall))]
4545
# extern crate ext_php_rs;
4646
use ext_php_rs::prelude::*;
4747
48-
#[php_function(defaults(offset = 0))]
48+
#[php_function]
49+
#[php(defaults(offset = 0))]
4950
pub fn rusty_strpos(haystack: &str, needle: &str, offset: i64) -> Option<usize> {
5051
let haystack: String = haystack.chars().skip(offset as usize).collect();
5152
haystack.find(needle)
@@ -98,7 +99,8 @@ use ext_php_rs::prelude::*;
9899
99100
/// `age` will be deemed required and nullable rather than optional,
100101
/// while description will be optional.
101-
#[php_function(optional = "description")]
102+
#[php_function]
103+
#[php(optional = "description")]
102104
pub fn greet(name: String, age: Option<i32>, description: Option<String>) -> String {
103105
let mut greeting = format!("Hello, {}!", name);
104106

guide/src/macros/index.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,13 @@ used from PHP without fiddling around with zvals.
1414
- [`php_const`] - Used to export a Rust constant to PHP as a global constant.
1515
- [`php_extern`] - Attribute used to annotate `extern` blocks which are deemed as
1616
PHP functions.
17+
- [`php`] - Used to modify the default behavior of the above macros. This is a
18+
generic attribute that can be used on most of the above macros.
1719

1820
[`php_module`]: ./module.md
1921
[`php_function`]: ./function.md
2022
[`php_class`]: ./classes.md
2123
[`php_impl`]: ./impl.md
2224
[`php_const`]: ./constant.md
2325
[`php_extern`]: ./extern.md
26+
[`php`]: ./php.md

guide/src/macros/php.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# `#[php]` Attributes
2+
3+
There are a number of attributes that can be used to annotate elements in your
4+
extension.
5+
6+
Multiple `#[php]` attributes will be combined. For example, the following will
7+
be identical:
8+
9+
```rust,no_run
10+
# #![cfg_attr(windows, feature(abi_vectorcall))]
11+
# extern crate ext_php_rs;
12+
# use ext_php_rs::prelude::*;
13+
#[php_function]
14+
#[php(name = "hi_world")]
15+
#[php(defaults(a = 1, b = 2))]
16+
fn hello_world(a: i32, b: i32) -> i32 {
17+
a + b
18+
}
19+
# fn main() {}
20+
```
21+
22+
```rust,no_run
23+
# #![cfg_attr(windows, feature(abi_vectorcall))]
24+
# extern crate ext_php_rs;
25+
# use ext_php_rs::prelude::*;
26+
#[php_function]
27+
#[php(name = "hi_world", defaults(a = 1, b = 2))]
28+
fn hello_world(a: i32, b: i32) -> i32 {
29+
a + b
30+
}
31+
# fn main() {}
32+
```
33+
34+
Which attributes are available depends on the element you are annotating:
35+
36+
| Attribute | `const` | `fn` | `struct` | `struct` Field | `impl` | `impl` `const` | `impl` `fn` |
37+
| ---------------- | ------- | ---- | -------- | -------------- | ------ | -------------- | ----------- |
38+
| name ||||||||
39+
| rename ||||||||
40+
| rename_methods ||||||||
41+
| rename_constants ||||||||
42+
| flags ||||||||
43+
| prop ||||||||
44+
| extends ||||||||
45+
| implements ||||||||
46+
| modifier ||||||||
47+
| defaults ||||||||
48+
| optional ||||||||
49+
| vis ||||||||
50+
| getter ||||||||
51+
| setter ||||||||
52+
| constructor ||||||||
53+
| abstract_method ||||||||

guide/src/migration-guides/v0.14.md

Lines changed: 54 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ register functions, classes, constants and startup function when declaring
1515
the module.
1616

1717
```rs
18-
#[php_module(startup = "startup_function")]
18+
#[php_module]
19+
#[php(startup = "startup_function")]
1920
pub fn get_module(module: ModuleBuilder) -> ModuleBuilder {
2021
module
2122
.class::<TestClass>()
@@ -44,6 +45,13 @@ pub fn get_module(module: ModuleBuilder) -> ModuleBuilder {
4445
}
4546
```
4647

48+
**Supported `#[php]` attributes:**
49+
- `#[php(name = "NEW_NAME")]` - Renames the function
50+
- `#[php(rename = case)]` - Changes the case of the function name
51+
- `#[php(vis = "public")]` - Changes the visibility of the function
52+
- `#[php(defaults(a = 5, test = 100))]` - Sets default values for function arguments
53+
- `#[php(variadic)]` - Marks the function as variadic. The last argument must be a `&[&Zval]`
54+
4755
### Classes
4856

4957
Mostly unchanged in terms of the class and impl definitions, however you now
@@ -55,24 +63,23 @@ use ext_php_rs::prelude::*;
5563
#[php_class]
5664
#[derive(Debug)]
5765
pub struct TestClass {
58-
#[prop]
66+
#[php(prop)]
5967
a: i32,
60-
#[prop]
68+
#[php(prop)]
6169
b: i32,
6270
}
6371

6472
#[php_impl]
6573
impl TestClass {
66-
#[rename("NEW_CONSTANT_NAME")]
74+
#[php(name = "NEW_CONSTANT_NAME")]
6775
pub const SOME_CONSTANT: i32 = 5;
6876
pub const SOME_OTHER_STR: &'static str = "Hello, world!";
6977

7078
pub fn __construct(a: i32, b: i32) -> Self {
7179
Self { a: a + 10, b: b + 10 }
7280
}
7381

74-
#[optional(test)]
75-
#[defaults(a = 5, test = 100)]
82+
#[php(defaults(a = 5, test = 100))]
7683
pub fn test_camel_case(&self, a: i32, test: i32) {
7784
println!("a: {} test: {}", a, test);
7885
}
@@ -95,6 +102,20 @@ pub fn get_module(module: ModuleBuilder) -> ModuleBuilder {
95102
}
96103
```
97104

105+
**Supported `#[php]` attributes (`struct`):**
106+
- `#[php(name = "NEW_NAME")]` - Renames the class
107+
- `#[php(rename = case)]` - Changes the case of the class name
108+
- `#[php(vis = "public")]` - Changes the visibility of the class
109+
- `#[php(extends = "ParentClass")]` - Extends a parent class
110+
- `#[php(implements = "Interface")]` - Implements an interface
111+
- `#[php(prop)]` - Marks a field as a property
112+
113+
**Supported `#[php]` attributes (`impl`):**
114+
- `#[php(rename_consts = case)]` - Changes the case of the constant names. Can be overridden by attributes on the constants.
115+
- `#[php(rename_methods = case)]` - Changes the case of the method names. Can be overridden by attributes on the methods.
116+
117+
For elements in the `#[php_impl]` block see the respective function and constant attributes.
118+
98119
### Constants
99120

100121
Mostly unchanged in terms of constant definition, however you now need to
@@ -106,14 +127,24 @@ use ext_php_rs::prelude::*;
106127
#[php_const]
107128
const SOME_CONSTANT: i32 = 100;
108129

130+
#[php_const]
131+
#[php(name = "HELLO_WORLD")]
132+
const SOME_OTHER_CONSTANT: &'static str = "Hello, world!";
133+
109134
#[php_module]
110135
pub fn get_module(module: ModuleBuilder) -> ModuleBuilder {
111136
module
112137
.constant(wrap_constant!(SOME_CONSTANT)) // SOME_CONSTANT = 100
138+
.constant(wrap_constant!(SOME_OTHER_CONSTANT)) // HELLO_WORLD = "Hello, world!"
113139
.constant(("CONST_NAME", SOME_CONSTANT, &[])) // CONST_NAME = 100
114140
}
115141
```
116142

143+
**Supported `#[php]` attributes:**
144+
- `#[php(name = "NEW_NAME")]` - Renames the constant
145+
- `#[php(rename = case)]` - Changes the case of the constant name
146+
- `#[php(vis = "public")]` - Changes the visibility of the constant
147+
117148
### Extern
118149

119150
No changes.
@@ -144,8 +175,24 @@ fn startup_function(ty: i32, mod_num: i32) -> i32 {
144175
0
145176
}
146177

147-
#[php_module(startup = "startup_function")]
178+
#[php_module]
179+
#[php(startup = "startup_function")]
148180
pub fn get_module(module: ModuleBuilder) -> ModuleBuilder {
149181
module
150182
}
151183
```
184+
185+
### `#[php]` Attributes
186+
187+
Attributes like `#[rename]` or `#[prop]` have been moved to the `#[php]` attribute.
188+
189+
The `#[php]` attribute on an item are combined with each other. This means that
190+
the following variants are equivalent:
191+
```rs
192+
#[php(rename = case)]
193+
#[php(vis = "public")]
194+
```
195+
196+
```rs
197+
#[php(rename = case, vis = "public")]
198+
```

0 commit comments

Comments
 (0)