Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add before flag to #[php_startup] #170

Merged
merged 2 commits into from
Nov 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions crates/macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,11 @@ pub fn php_module(_: TokenStream, input: TokenStream) -> TokenStream {
}

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

match startup_function::parser(input) {
match startup_function::parser(Some(args), input) {
Ok(parsed) => parsed,
Err(e) => syn::Error::new(Span::call_site(), e).to_compile_error(),
}
Expand Down
2 changes: 1 addition & 1 deletion crates/macros/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub fn parser(input: ItemFn) -> Result<TokenStream> {
fn php_module_startup() {}
})
.map_err(|_| anyhow!("Unable to generate PHP module startup function."))?;
let startup = startup_function::parser(parsed)?;
let startup = startup_function::parser(None, parsed)?;

state = STATE.lock();
Some(startup)
Expand Down
28 changes: 23 additions & 5 deletions crates/macros/src/startup_function.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,27 @@
use std::collections::HashMap;

use anyhow::{anyhow, Result};
use darling::FromMeta;
use proc_macro2::{Ident, Span, TokenStream};
use quote::quote;
use syn::{Expr, ItemFn, Signature};
use syn::{AttributeArgs, Expr, ItemFn, Signature};

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

pub fn parser(input: ItemFn) -> Result<TokenStream> {
#[derive(Default, Debug, FromMeta)]
#[darling(default)]
struct StartupArgs {
before: bool,
}

pub fn parser(args: Option<AttributeArgs>, input: ItemFn) -> Result<TokenStream> {
let args = if let Some(args) = args {
StartupArgs::from_list(&args)
.map_err(|e| anyhow!("Unable to parse attribute arguments: {:?}", e))?
} else {
StartupArgs::default()
};

let ItemFn { sig, block, .. } = input;
let Signature { ident, .. } = sig;
let stmts = &block.stmts;
Expand All @@ -17,6 +31,11 @@ pub fn parser(input: ItemFn) -> Result<TokenStream> {

let classes = build_classes(&state.classes)?;
let constants = build_constants(&state.constants);
let (before, after) = if args.before {
(Some(quote! { internal(); }), None)
} else {
(None, Some(quote! { internal(); }))
};

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

::ext_php_rs::internal::ext_php_rs_startup();

#before
#(#classes)*
#(#constants)*

// TODO return result?
internal();
#after

0
}
Expand Down
5 changes: 5 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,11 @@ pub use ext_php_rs_derive::php_class;
/// this macro if you have registered any classes or constants when using the
/// [`macro@php_module`] macro.
///
/// The attribute accepts one optional flag -- `#[php_startup(before)]` --
/// which forces the annotated function to be called _before_ the other classes
/// and constants are registered. By default the annotated function is called
/// after these classes and constants are registered.
///
/// # Example
///
/// ```
Expand Down