Skip to content

Commit

Permalink
Refactor module paths (#101)
Browse files Browse the repository at this point in the history
* Refactor module layout

* Fixed documentation tests

* Removed skel, moved macro crate

* Ignore folders for crate publish

* Fix builder for zts

* Add `rustfmt.toml`, wrap all comments #96

* Fixed up documentation links, tidied up

* Add `Zend` prefix to callable and hashtable

* Updated guide types

* Updated changelog
  • Loading branch information
davidcole1340 authored Oct 10, 2021
1 parent f8b5727 commit 466c165
Show file tree
Hide file tree
Showing 93 changed files with 4,253 additions and 3,514 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# Changelog

## Version 0.6.0

- Reorganized project. [#101]
- Changed (almost all) module paths. Too many changes to list them all, check
out the docs.
- Removed `skel` project.

[#101]: https://github.com/davidcole1340/ext-php-rs/pull/101

## Version 0.5.3

- Fixed docs.rs PHP bindings file.
Expand Down
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ version = "0.5.3"
authors = ["David Cole <[email protected]>"]
edition = "2018"
categories = ["api-bindings"]
exclude = ["/.github", "/.crates", "/guide"]

[dependencies]
bitflags = "1.2.1"
parking_lot = "0.11.2"
ext-php-rs-derive = { version = "=0.5.2", path = "./ext-php-rs-derive" }
ext-php-rs-derive = { version = "=0.5.2", path = "./crates/macros" }

[build-dependencies]
bindgen = { version = "0.59" }
Expand All @@ -25,8 +26,7 @@ closure = []

[workspace]
members = [
"ext-php-rs-derive",
"example/skel"
"crates/macros",
]

[package.metadata.docs.rs]
Expand Down
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,6 @@ easily), structs have to be hard coded in.

Check out one of the example projects:

- [ext-skel](example/skel) - Testbed for testing the library. Check out previous
commits as well to see what else is possible.
- [anonaddy-sequoia](https://gitlab.com/willbrowning/anonaddy-sequoia) - Sequoia
encryption PHP extension.
- [opus-php](https://github.com/davidcole1340/opus-php/tree/rewrite_rs) -
Expand Down
17 changes: 9 additions & 8 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ const MAX_PHP_API_VER: u32 = 20200930;

fn main() {
// rerun if wrapper header is changed
println!("cargo:rerun-if-changed=src/wrapper/wrapper.h");
println!("cargo:rerun-if-changed=src/wrapper/wrapper.c");
println!("cargo:rerun-if-changed=src/wrapper.h");
println!("cargo:rerun-if-changed=src/wrapper.c");

let out_path = PathBuf::from(env::var("OUT_DIR").unwrap()).join("bindings.rs");

Expand All @@ -40,8 +40,8 @@ fn main() {
}

// Ensure the PHP API version is supported.
// We could easily use grep and sed here but eventually we want to support Windows,
// so it's easier to just use regex.
// We could easily use grep and sed here but eventually we want to support
// Windows, so it's easier to just use regex.
let php_i_cmd = Command::new("php")
.arg("-i")
.output()
Expand Down Expand Up @@ -71,7 +71,7 @@ fn main() {

// Build `wrapper.c` and link to Rust.
cc::Build::new()
.file("src/wrapper/wrapper.c")
.file("src/wrapper.c")
.includes(
str::replace(includes.as_ref(), "-I", "")
.split(' ')
Expand All @@ -80,7 +80,7 @@ fn main() {
.compile("wrapper");

let mut bindgen = bindgen::Builder::default()
.header("src/wrapper/wrapper.h")
.header("src/wrapper.h")
.clang_args(includes.split(' '))
.parse_callbacks(Box::new(bindgen::CargoCallbacks))
.rustfmt_bindings(true)
Expand Down Expand Up @@ -143,8 +143,9 @@ impl Configure {
}
}

/// Array of functions/types used in `ext-php-rs` - used to allowlist when generating
/// bindings, as we don't want to generate bindings for everything (i.e. stdlib headers).
/// Array of functions/types used in `ext-php-rs` - used to allowlist when
/// generating bindings, as we don't want to generate bindings for everything
/// (i.e. stdlib headers).
const ALLOWED_BINDINGS: &[&str] = &[
"HashTable",
"_Bucket",
Expand Down
1 change: 1 addition & 0 deletions crates/macros/.gitignore
File renamed without changes.
1 change: 1 addition & 0 deletions crates/macros/LICENSE_APACHE
1 change: 1 addition & 0 deletions crates/macros/LICENSE_MIT
1 change: 1 addition & 0 deletions crates/macros/README.md
4 changes: 2 additions & 2 deletions ext-php-rs-derive/src/class.rs → crates/macros/src/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ impl Property {
PropertyType::Field { field_name } => {
let field_name = Ident::new(field_name, Span::call_site());
quote! {
(#name, ::ext_php_rs::php::types::props::Property::field(|obj: &mut Self| &mut obj.#field_name)),
(#name, ::ext_php_rs::props::Property::field(|obj: &mut Self| &mut obj.#field_name)),
}
}
PropertyType::Method { getter, setter } => {
Expand All @@ -216,7 +216,7 @@ impl Property {
quote! { None }
};
quote! {
(#name, ::ext_php_rs::php::types::props::Property::method(#getter, #setter)),
(#name, ::ext_php_rs::props::Property::method(#getter, #setter)),
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,6 @@ impl Constant {
// Visibility::Private => quote! { Private },
// };

// quote! { ::ext_php_rs::php::flags::ConstantFlags}
// quote! { ::ext_php_rs::flags::ConstantFlags}
// }
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ fn parse_function(mut func: ForeignItemFn) -> Result<TokenStream> {
#(#attrs)* #vis #sig {
use ::std::convert::TryInto;

let callable = ::ext_php_rs::php::types::callable::Callable::try_from_name(
let callable = ::ext_php_rs::types::ZendCallable::try_from_name(
#name
).expect(concat!("Unable to find callable function `", #name, "`."));

Expand Down
24 changes: 13 additions & 11 deletions ext-php-rs-derive/src/function.rs → crates/macros/src/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,16 @@ pub fn parser(args: AttributeArgs, input: ItemFn) -> Result<(TokenStream, Functi
#input

#[doc(hidden)]
pub extern "C" fn #internal_ident(ex: &mut ::ext_php_rs::php::execution_data::ExecutionData, retval: &mut ::ext_php_rs::php::types::zval::Zval) {
use ::ext_php_rs::php::types::zval::IntoZval;
pub extern "C" fn #internal_ident(ex: &mut ::ext_php_rs::zend::ExecuteData, retval: &mut ::ext_php_rs::types::Zval) {
use ::ext_php_rs::convert::IntoZval;

#(#arg_definitions)*
#arg_parser

let result = #ident(#(#arg_accessors, )*);

if let Err(e) = result.set_zval(retval, false) {
let e: ::ext_php_rs::php::exceptions::PhpException = e.into();
let e: ::ext_php_rs::exception::PhpException = e.into();
e.throw().expect("Failed to throw exception");
}
}
Expand Down Expand Up @@ -206,7 +206,7 @@ pub fn build_arg_parser<'a>(
let this = match this {
Some(this) => this,
None => {
::ext_php_rs::php::exceptions::PhpException::default("Failed to retrieve reference to `$this`".into())
::ext_php_rs::exception::PhpException::default("Failed to retrieve reference to `$this`".into())
.throw()
.unwrap();
return;
Expand Down Expand Up @@ -309,7 +309,7 @@ impl Arg {
pub fn get_type_ident(&self) -> TokenStream {
let ty: Type = syn::parse_str(&self.ty).unwrap();
quote! {
<#ty as ::ext_php_rs::php::types::zval::FromZval>::TYPE
<#ty as ::ext_php_rs::convert::FromZval>::TYPE
}
}

Expand All @@ -318,7 +318,8 @@ impl Arg {
Ident::new(&self.name, Span::call_site())
}

/// Returns a [`TokenStream`] containing the line required to retrieve the value from the argument.
/// Returns a [`TokenStream`] containing the line required to retrieve the
/// value from the argument.
pub fn get_accessor(&self, ret: &TokenStream) -> TokenStream {
let name = &self.name;
let name_ident = self.get_name_ident();
Expand All @@ -338,7 +339,7 @@ impl Arg {
match #name_ident.val() {
Some(val) => val,
None => {
::ext_php_rs::php::exceptions::PhpException::default(
::ext_php_rs::exception::PhpException::default(
concat!("Invalid value given for argument `", #name, "`.").into()
)
.throw()
Expand All @@ -350,7 +351,8 @@ impl Arg {
}
}

/// Returns a [`TokenStream`] containing the line required to instantiate the argument.
/// Returns a [`TokenStream`] containing the line required to instantiate
/// the argument.
pub fn get_arg_definition(&self) -> TokenStream {
let name = &self.name;
let ty = self.get_type_ident();
Expand All @@ -363,7 +365,7 @@ impl Arg {
});

quote! {
::ext_php_rs::php::args::Arg::new(#name, #ty) #null #default
::ext_php_rs::args::Arg::new(#name, #ty) #null #default
}
}
}
Expand Down Expand Up @@ -397,12 +399,12 @@ impl Function {

// TODO allow reference returns?
quote! {
.returns(<#ty as ::ext_php_rs::php::types::zval::IntoZval>::TYPE, false, #nullable)
.returns(<#ty as ::ext_php_rs::convert::IntoZval>::TYPE, false, #nullable)
}
});

quote! {
::ext_php_rs::php::function::FunctionBuilder::new(#name, #name_ident)
::ext_php_rs::builders::FunctionBuilder::new(#name, #name_ident)
#(#args)*
#output
.build()
Expand Down
File renamed without changes.
File renamed without changes.
22 changes: 11 additions & 11 deletions ext-php-rs-derive/src/method.rs → crates/macros/src/method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,10 @@ pub fn parser(input: &mut ImplItemMethod, rename_rule: RenameRule) -> Result<Par

#[doc(hidden)]
pub fn #internal_ident(
ex: &mut ::ext_php_rs::php::execution_data::ExecutionData
) -> ::ext_php_rs::php::types::object::ConstructorResult<Self> {
use ::ext_php_rs::php::types::zval::IntoZval;
use ::ext_php_rs::php::types::object::ConstructorResult;
ex: &mut ::ext_php_rs::zend::ExecuteData
) -> ::ext_php_rs::class::ConstructorResult<Self> {
use ::ext_php_rs::convert::IntoZval;
use ::ext_php_rs::class::ConstructorResult;

#(#arg_definitions)*
#arg_parser
Expand All @@ -171,18 +171,18 @@ pub fn parser(input: &mut ImplItemMethod, rename_rule: RenameRule) -> Result<Par

#[doc(hidden)]
pub extern "C" fn #internal_ident(
ex: &mut ::ext_php_rs::php::execution_data::ExecutionData,
retval: &mut ::ext_php_rs::php::types::zval::Zval
ex: &mut ::ext_php_rs::zend::ExecuteData,
retval: &mut ::ext_php_rs::types::Zval
) {
use ::ext_php_rs::php::types::zval::IntoZval;
use ::ext_php_rs::convert::IntoZval;

#(#arg_definitions)*
#arg_parser

let result = #this #ident(#(#arg_accessors, )*);

if let Err(e) = result.set_zval(retval, false) {
let e: ::ext_php_rs::php::exceptions::PhpException = e.into();
let e: ::ext_php_rs::exception::PhpException = e.into();
e.throw().expect("Failed to throw exception");
}
}
Expand Down Expand Up @@ -313,12 +313,12 @@ impl Method {

// TODO allow reference returns?
quote! {
.returns(<#ty as ::ext_php_rs::php::types::zval::IntoZval>::TYPE, false, #nullable)
.returns(<#ty as ::ext_php_rs::convert::IntoZval>::TYPE, false, #nullable)
}
});

quote! {
::ext_php_rs::php::function::FunctionBuilder::new(#name, #class_path :: #name_ident)
::ext_php_rs::builders::FunctionBuilder::new(#name, #class_path :: #name_ident)
#(#args)*
#output
.build()
Expand All @@ -338,7 +338,7 @@ impl Method {

flags
.iter()
.map(|flag| quote! { ::ext_php_rs::php::flags::MethodFlags::#flag })
.map(|flag| quote! { ::ext_php_rs::flags::MethodFlags::#flag })
.collect::<Punctuated<TokenStream, Token![|]>>()
.to_token_stream()
}
Expand Down
18 changes: 9 additions & 9 deletions ext-php-rs-derive/src/module.rs → crates/macros/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ pub fn parser(input: ItemFn) -> Result<TokenStream> {

#[doc(hidden)]
#[no_mangle]
pub extern "C" fn get_module() -> *mut ::ext_php_rs::php::module::ModuleEntry {
pub extern "C" fn get_module() -> *mut ::ext_php_rs::zend::ModuleEntry {
fn internal(#inputs) #output {
#(#stmts)*
}

let mut builder = ::ext_php_rs::php::module::ModuleBuilder::new(
let mut builder = ::ext_php_rs::builders::ModuleBuilder::new(
env!("CARGO_PKG_NAME"),
env!("CARGO_PKG_VERSION")
)
Expand Down Expand Up @@ -98,10 +98,10 @@ pub fn generate_registered_class_impl(class: &Class) -> Result<TokenStream> {
let func = Ident::new(&constructor.ident, Span::call_site());
let args = constructor.get_arg_definitions();
quote! {
Some(::ext_php_rs::php::types::object::ConstructorMeta {
Some(::ext_php_rs::class::ConstructorMeta {
constructor: Self::#func,
build_fn: {
use ext_php_rs::php::function::FunctionBuilder;
use ::ext_php_rs::builders::FunctionBuilder;
fn build_fn(func: FunctionBuilder) -> FunctionBuilder {
func
#(#args)*
Expand All @@ -115,19 +115,19 @@ pub fn generate_registered_class_impl(class: &Class) -> Result<TokenStream> {
};

Ok(quote! {
static #meta: ::ext_php_rs::php::types::object::ClassMetadata<#self_ty> = ::ext_php_rs::php::types::object::ClassMetadata::new();
static #meta: ::ext_php_rs::class::ClassMetadata<#self_ty> = ::ext_php_rs::class::ClassMetadata::new();

impl ::ext_php_rs::php::types::object::RegisteredClass for #self_ty {
impl ::ext_php_rs::class::RegisteredClass for #self_ty {
const CLASS_NAME: &'static str = #class_name;
const CONSTRUCTOR: ::std::option::Option<
::ext_php_rs::php::types::object::ConstructorMeta<Self>
::ext_php_rs::class::ConstructorMeta<Self>
> = #constructor;

fn get_metadata() -> &'static ::ext_php_rs::php::types::object::ClassMetadata<Self> {
fn get_metadata() -> &'static ::ext_php_rs::class::ClassMetadata<Self> {
&#meta
}

fn get_properties<'a>() -> ::std::collections::HashMap<&'static str, ::ext_php_rs::php::types::props::Property<'a, Self>> {
fn get_properties<'a>() -> ::std::collections::HashMap<&'static str, ::ext_php_rs::props::Property<'a, Self>> {
use ::std::iter::FromIterator;

::std::collections::HashMap::from_iter([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ pub fn parser(input: ItemFn) -> Result<TokenStream> {
let func = quote! {
#[doc(hidden)]
pub extern "C" fn #ident(ty: i32, module_number: i32) -> i32 {
use ::ext_php_rs::php::constants::IntoConst;
use ::ext_php_rs::php::flags::PropertyFlags;
use ::ext_php_rs::constant::IntoConst;
use ::ext_php_rs::flags::PropertyFlags;

fn internal() {
#(#stmts)*
}

::ext_php_rs::php::module::ext_php_rs_startup();
::ext_php_rs::internal::ext_php_rs_startup();

#(#classes)*
#(#constants)*
Expand Down Expand Up @@ -116,7 +116,7 @@ fn build_classes(classes: &HashMap<String, Class>) -> Result<Vec<TokenStream>> {
// .collect::<Result<Vec<_>>>()?;

Ok(quote! {{
let class = ::ext_php_rs::php::class::ClassBuilder::new(#class_name)
let class = ::ext_php_rs::builders::ClassBuilder::new(#class_name)
#(#methods)*
#(#constants)*
#(#interfaces)*
Expand Down
File renamed without changes.
Loading

0 comments on commit 466c165

Please sign in to comment.