Skip to content

Commit 5f33598

Browse files
add before flag to #[php_startup] (#170)
* add `before` flag to `#[php_startup]` this calls the user-provided startup function _before_ the classes and constants registered by the macro system are registered with PHP. by default the behaviour is to run this function after, which means you cannot define an interface and use it on a struct. * cargo fmt
1 parent 997fded commit 5f33598

File tree

4 files changed

+32
-8
lines changed

4 files changed

+32
-8
lines changed

crates/macros/src/lib.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,11 @@ pub fn php_module(_: TokenStream, input: TokenStream) -> TokenStream {
8787
}
8888

8989
#[proc_macro_attribute]
90-
pub fn php_startup(_: TokenStream, input: TokenStream) -> TokenStream {
90+
pub fn php_startup(args: TokenStream, input: TokenStream) -> TokenStream {
91+
let args = parse_macro_input!(args as AttributeArgs);
9192
let input = parse_macro_input!(input as ItemFn);
9293

93-
match startup_function::parser(input) {
94+
match startup_function::parser(Some(args), input) {
9495
Ok(parsed) => parsed,
9596
Err(e) => syn::Error::new(Span::call_site(), e).to_compile_error(),
9697
}

crates/macros/src/module.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ pub fn parser(input: ItemFn) -> Result<TokenStream> {
3434
fn php_module_startup() {}
3535
})
3636
.map_err(|_| anyhow!("Unable to generate PHP module startup function."))?;
37-
let startup = startup_function::parser(parsed)?;
37+
let startup = startup_function::parser(None, parsed)?;
3838

3939
state = STATE.lock();
4040
Some(startup)

crates/macros/src/startup_function.rs

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,27 @@
11
use std::collections::HashMap;
22

33
use anyhow::{anyhow, Result};
4+
use darling::FromMeta;
45
use proc_macro2::{Ident, Span, TokenStream};
56
use quote::quote;
6-
use syn::{Expr, ItemFn, Signature};
7+
use syn::{AttributeArgs, Expr, ItemFn, Signature};
78

89
use crate::{class::Class, constant::Constant, STATE};
910

10-
pub fn parser(input: ItemFn) -> Result<TokenStream> {
11+
#[derive(Default, Debug, FromMeta)]
12+
#[darling(default)]
13+
struct StartupArgs {
14+
before: bool,
15+
}
16+
17+
pub fn parser(args: Option<AttributeArgs>, input: ItemFn) -> Result<TokenStream> {
18+
let args = if let Some(args) = args {
19+
StartupArgs::from_list(&args)
20+
.map_err(|e| anyhow!("Unable to parse attribute arguments: {:?}", e))?
21+
} else {
22+
StartupArgs::default()
23+
};
24+
1125
let ItemFn { sig, block, .. } = input;
1226
let Signature { ident, .. } = sig;
1327
let stmts = &block.stmts;
@@ -17,6 +31,11 @@ pub fn parser(input: ItemFn) -> Result<TokenStream> {
1731

1832
let classes = build_classes(&state.classes)?;
1933
let constants = build_constants(&state.constants);
34+
let (before, after) = if args.before {
35+
(Some(quote! { internal(); }), None)
36+
} else {
37+
(None, Some(quote! { internal(); }))
38+
};
2039

2140
let func = quote! {
2241
#[doc(hidden)]
@@ -30,11 +49,10 @@ pub fn parser(input: ItemFn) -> Result<TokenStream> {
3049

3150
::ext_php_rs::internal::ext_php_rs_startup();
3251

52+
#before
3353
#(#classes)*
3454
#(#constants)*
35-
36-
// TODO return result?
37-
internal();
55+
#after
3856

3957
0
4058
}

src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,11 @@ pub use ext_php_rs_derive::php_class;
518518
/// this macro if you have registered any classes or constants when using the
519519
/// [`macro@php_module`] macro.
520520
///
521+
/// The attribute accepts one optional flag -- `#[php_startup(before)]` --
522+
/// which forces the annotated function to be called _before_ the other classes
523+
/// and constants are registered. By default the annotated function is called
524+
/// after these classes and constants are registered.
525+
///
521526
/// # Example
522527
///
523528
/// ```

0 commit comments

Comments
 (0)