Skip to content

Commit

Permalink
Better Arguments
Browse files Browse the repository at this point in the history
Signed-off-by: Dusan Malusev <dusan@dusanmalusev.dev>
  • Loading branch information
CodeLieutenant committed Jun 18, 2024
1 parent cc82213 commit acd85c6
Show file tree
Hide file tree
Showing 26 changed files with 5,909 additions and 364 deletions.
667 changes: 466 additions & 201 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 0 additions & 2 deletions examples/complex/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,3 @@ phper-test = { workspace = true }

[build-dependencies]
phper-build = { workspace = true }
bindgen = "0.69.4"
walkdir = "2"
27 changes: 5 additions & 22 deletions examples/complex/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,34 +10,17 @@

use std::env;
use std::path::PathBuf;
use bindgen::Builder;
use walkdir::WalkDir;

fn main() {
phper_build::register_configures();
println!("cargo:rerun-if-changed=build.rs");
println!("cargo:rerun-if-changed=stubs");

#[cfg(target_os = "macos")]
{
println!("cargo:rustc-link-arg=-undefined");
println!("cargo:rustc-link-arg=dynamic_lookup");
}
phper_build::register_all();

let current_dir = env::current_dir().unwrap();
let current_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
let stubs = current_dir.join("stubs");


for entry in WalkDir::new(stubs) {
println!("{}", entry.unwrap().path().display());
}


let mut builder = Builder::default();

// for dir in PathBuf::from(stubs).iter().map(|include| &include[2..]) {
// println!("cargo:include={}", dir);
// let p = PathBuf::from(dir).join(".*\\.h");
// builder = builder.allowlist_file(p.display().to_string());
// }

phper_build::generate_php_function_args(&out_path, &[&stubs], None).unwrap();
}
6 changes: 6 additions & 0 deletions examples/complex/src/args_bindings.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#![allow(non_upper_case_globals)]
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]
#![allow(deref_nullptr)]
#![allow(clippy::all)]
include!(concat!(env!("OUT_DIR"), "/php_args_bindings.rs"));
47 changes: 27 additions & 20 deletions examples/complex/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,21 @@
// NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
// See the Mulan PSL v2 for more details.

mod args_bindings;

use std::convert::Infallible;
use std::ffi::CStr;

use args_bindings::{arginfo_Complex_say_hello, arginfo_Complex_throw_exception, arginfo_Complex_get_all_ini};

use phper::arrays::ZArray;
use phper::ini::{ini_get, Policy};
use phper::{
arrays::ZArray,
classes::{entity::ClassEntity, Visibility},
ini::{ini_get, Policy},
modules::Module,
objects::StateObj,
php_get_module,
values::ZVal,
zend_args,
};
use std::ptr::null;
use std::{convert::Infallible, ffi::CStr};

fn say_hello(arguments: &mut [ZVal]) -> phper::Result<String> {
let name = &mut arguments[0];
Expand Down Expand Up @@ -56,19 +60,22 @@ pub fn get_module() -> Module {
module.on_request_shutdown(|_info| {});

// register functions
module
.add_function("Comples\\say_hello", arginfo_Complex_say_hello, say_hello);
// .add_function("complex_throw_exception", null(), throw_exception)
// .add_function("complex_get_all_ini", null(), |_: &mut [ZVal]| {
// let mut arr = ZArray::new();
//
// let complex_enable = ZVal::from(ini_get::<bool>("complex.enable"));
// arr.insert("complex.enable", complex_enable);
//
// let complex_description = ZVal::from(ini_get::<Option<&CStr>>("complex.description"));
// arr.insert("complex.description", complex_description);
// Ok::<_, Infallible>(arr)
// });
module.add_function(
"Comples\\say_hello",
zend_args!(arginfo_Complex_say_hello),
say_hello,
)
.add_function("Complex\\throw_exception", zend_args!(arginfo_Complex_throw_exception), throw_exception)
.add_function("Comples\\get_all_ini", zend_args!(arginfo_Complex_get_all_ini), |_: &mut [ZVal]| {
let mut arr = ZArray::new();

let complex_enable = ZVal::from(ini_get::<bool>("complex.enable"));
arr.insert("complex.enable", complex_enable);

let complex_description = ZVal::from(ini_get::<Option<&CStr>>("complex.description"));
arr.insert("complex.description", complex_description);
Ok::<_, Infallible>(arr)
});

//
// // register classes
Expand All @@ -92,7 +99,7 @@ pub fn get_module() -> Module {
// },
// )
// .argument(Argument::by_val("foo"));
module.add_class(foo_class);
// module.add_class(foo_class);
//
// // register extra info
module.add_info("extra info key", "extra info value");
Expand Down
8 changes: 8 additions & 0 deletions examples/complex/stubs/say_hello.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,12 @@

namespace Complex {
function say_hello(string $name): string {}

function throw_exception(): void
{
}

function get_all_ini(): array
{
}
}
6 changes: 0 additions & 6 deletions examples/complex/stubs/say_hello_arginfo.h

This file was deleted.

4 changes: 4 additions & 0 deletions examples/hello/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,7 @@ crate-type = ["lib", "cdylib"]

[dependencies]
phper = { workspace = true }
phper-sys = { workspace = true }

[build-dependencies]
phper-build = { workspace = true }
18 changes: 13 additions & 5 deletions examples/hello/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,18 @@
// NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
// See the Mulan PSL v2 for more details.

use std::env::var;
use std::path::PathBuf;

fn main() {
#[cfg(target_os = "macos")]
{
println!("cargo:rustc-link-arg=-undefined");
println!("cargo:rustc-link-arg=dynamic_lookup");
}
println!("cargo:rerun-if-changed=build.rs");
println!("cargo:rerun-if-changed=stubs");

phper_build::register_all();

let current_dir = PathBuf::from(var("CARGO_MANIFEST_DIR").unwrap());
let out_path = PathBuf::from(var("OUT_DIR").unwrap());
let stubs = current_dir.join("stubs");

phper_build::generate_php_function_args(&out_path, &[&stubs], None).unwrap();
}
16 changes: 12 additions & 4 deletions examples/hello/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,17 @@
// NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
// See the Mulan PSL v2 for more details.

use std::ptr::null;
mod args {
#![allow(non_upper_case_globals)]
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]
#![allow(deref_nullptr)]
#![allow(clippy::all)]
include!(concat!(env!("OUT_DIR"), "/php_args_bindings.rs"));
}

use args::arginfo_say_hello;
use phper::zend_args;
use phper::{echo, modules::Module, php_get_module, values::ZVal};

/// The php function, receive arguments with type `ZVal`.
Expand All @@ -35,9 +45,7 @@ pub fn get_module() -> Module {
);

// Register function `say_hello`, with one argument `name`.
module
.add_function("say_hello", null(), say_hello);
// .argument(Argument::by_val("name"));
module.add_function("say_hello", zend_args!(arginfo_say_hello), say_hello);

module
}
5 changes: 5 additions & 0 deletions examples/hello/stubs/say_hello.stub.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

function say_hello(string $name): string
{
}
4 changes: 3 additions & 1 deletion phper-build/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ license = { workspace = true }
phper-sys = { workspace = true }
bindgen = "0.69.4"
walkdir = "2"
cc = "1.0.79"


[dev-dependencies]
assert_fs = {version = "^1", features = ["color"] }
assert_fs = { version = "^1", features = ["color"] }
predicates = "3.1.0"
15 changes: 15 additions & 0 deletions phper-build/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use std::env;
use std::env::current_dir;
use std::path::PathBuf;

fn main() {
println!("cargo:rerun-if-changed=build.rs");
println!("cargo:rerun-if-changed=gen_stub.php");

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

std::fs::copy(
current_dir().unwrap().join("gen_stub.php"),
out_path.join("gen_stub.php"),
).unwrap();
}
Loading

0 comments on commit acd85c6

Please sign in to comment.