From 905dc72279d40e107c032bf60f326c2494d9ec36 Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Sun, 27 Aug 2023 19:32:52 +0200 Subject: [PATCH] Add example --- .gitignore | 3 ++- Cargo.toml | 7 +++++- examples/reqwest/.cargo/config.toml | 8 +++++++ examples/reqwest/Cargo.toml | 14 ++++++++++++ examples/reqwest/composer.json | 5 +++++ examples/reqwest/lib/Client.php | 28 ++++++++++++++++++++++++ examples/reqwest/src/lib.rs | 34 +++++++++++++++++++++++++++++ examples/reqwest/test.php | 5 +++++ 8 files changed, 102 insertions(+), 2 deletions(-) create mode 100644 examples/reqwest/.cargo/config.toml create mode 100644 examples/reqwest/Cargo.toml create mode 100644 examples/reqwest/composer.json create mode 100644 examples/reqwest/lib/Client.php create mode 100644 examples/reqwest/src/lib.rs create mode 100644 examples/reqwest/test.php diff --git a/.gitignore b/.gitignore index 59b5122..73b9e2f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ /target /Cargo.lock /crates/*/target -/vendor/ +vendor +composer.lock diff --git a/Cargo.toml b/Cargo.toml index 570c90b..947be8c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,7 +4,7 @@ repository = "https://github.com/danog/php-tokio" homepage = "https://github.com/danog/php-tokio" license = "AGPL-3.0-or-later" description = "Use any async Rust library from PHP!" -version = "0.1.0" +version = "0.1.1" authors = ["Daniil Gentili "] edition = "2021" @@ -20,4 +20,9 @@ php-tokio-derive = { version = "=0.1.0", path = "./crates/macros" } [workspace] members = [ "crates/macros", + "examples/reqwest", ] + +[package.metadata.docs.rs] +rustdoc-args = ["--cfg", "docs"] + diff --git a/examples/reqwest/.cargo/config.toml b/examples/reqwest/.cargo/config.toml new file mode 100644 index 0000000..fd663c7 --- /dev/null +++ b/examples/reqwest/.cargo/config.toml @@ -0,0 +1,8 @@ +[target.'cfg(not(target_os = "windows"))'] +rustflags = ["-C", "link-arg=-Wl,-undefined,dynamic_lookup"] + +[target.x86_64-pc-windows-msvc] +linker = "rust-lld" + +[target.i686-pc-windows-msvc] +linker = "rust-lld" diff --git a/examples/reqwest/Cargo.toml b/examples/reqwest/Cargo.toml new file mode 100644 index 0000000..d586a9e --- /dev/null +++ b/examples/reqwest/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "example-reqwest" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[lib] +crate-type = ["cdylib"] + +[dependencies] +php-tokio = "^0.1" +nicelocal-ext-php-rs = { version = "^0.10.3", features = ["anyhow"] } +reqwest = "^0.11" \ No newline at end of file diff --git a/examples/reqwest/composer.json b/examples/reqwest/composer.json new file mode 100644 index 0000000..e783d19 --- /dev/null +++ b/examples/reqwest/composer.json @@ -0,0 +1,5 @@ +{ + "require": { + "revolt/event-loop": "^1.0" + } +} diff --git a/examples/reqwest/lib/Client.php b/examples/reqwest/lib/Client.php new file mode 100644 index 0000000..1eb00e6 --- /dev/null +++ b/examples/reqwest/lib/Client.php @@ -0,0 +1,28 @@ + \reqwest_async_wakeup()); + } + + public static function reference(): void{ + EventLoop::reference(self::$id); + } + public static function unreference(): void { + EventLoop::unreference(self::$id); + } + + public static function __callStatic(string $name, array $args): mixed { + return \Client::$name($args); + } +} diff --git a/examples/reqwest/src/lib.rs b/examples/reqwest/src/lib.rs new file mode 100644 index 0000000..1d2357b --- /dev/null +++ b/examples/reqwest/src/lib.rs @@ -0,0 +1,34 @@ +use php_tokio::{EventLoop, php_async_impl}; +use nicelocal_ext_php_rs::prelude::*; + +#[php_class] +struct Client { +} + +#[php_async_impl] +impl Client { + pub fn init() -> PhpResult{ + EventLoop::init() + } + pub fn wakeup() -> PhpResult<()> { + EventLoop::wakeup() + } + pub async fn get(url: &str) -> String { + reqwest::get("https://www.rust-lang.org") + .await? + .text() + .await? + } +} + +pub extern "C" fn request_shutdown(_type: i32, _module_number: i32) -> i32 { + EventLoop::shutdown(); + 0 +} + +#[php_module] +pub fn get_module(module: ModuleBuilder) -> ModuleBuilder { + module + .request_shutdown_function(request_shutdown) +} + diff --git a/examples/reqwest/test.php b/examples/reqwest/test.php new file mode 100644 index 0000000..109c70f --- /dev/null +++ b/examples/reqwest/test.php @@ -0,0 +1,5 @@ +