Add example

This commit is contained in:
Daniil Gentili 2023-08-27 19:32:52 +02:00
parent d1cdf8f594
commit 905dc72279
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7
8 changed files with 102 additions and 2 deletions

3
.gitignore vendored
View File

@ -1,4 +1,5 @@
/target /target
/Cargo.lock /Cargo.lock
/crates/*/target /crates/*/target
/vendor/ vendor
composer.lock

View File

@ -4,7 +4,7 @@ repository = "https://github.com/danog/php-tokio"
homepage = "https://github.com/danog/php-tokio" homepage = "https://github.com/danog/php-tokio"
license = "AGPL-3.0-or-later" license = "AGPL-3.0-or-later"
description = "Use any async Rust library from PHP!" description = "Use any async Rust library from PHP!"
version = "0.1.0" version = "0.1.1"
authors = ["Daniil Gentili <daniil@daniil.it>"] authors = ["Daniil Gentili <daniil@daniil.it>"]
edition = "2021" edition = "2021"
@ -20,4 +20,9 @@ php-tokio-derive = { version = "=0.1.0", path = "./crates/macros" }
[workspace] [workspace]
members = [ members = [
"crates/macros", "crates/macros",
"examples/reqwest",
] ]
[package.metadata.docs.rs]
rustdoc-args = ["--cfg", "docs"]

View File

@ -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"

View File

@ -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"

View File

@ -0,0 +1,5 @@
{
"require": {
"revolt/event-loop": "^1.0"
}
}

View File

@ -0,0 +1,28 @@
<?php
namespace Reqwest;
final class Client {
private static ?string $id = null;
public static function register(): void {
if (self::$id !== null) {
return;
}
$f = fopen("php://fd/".\reqwest_async_init(), 'r+');
stream_set_blocking($f, false);
self::$id = EventLoop::onReadable($f, fn () => \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);
}
}

View File

@ -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<u64>{
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)
}

View File

@ -0,0 +1,5 @@
<?php
require 'vendor/autoload.php'
\Reqwest\Client::init();