mirror of
https://github.com/danog/php-tokio.git
synced 2024-11-26 12:24:58 +01:00
Add example
This commit is contained in:
parent
d1cdf8f594
commit
905dc72279
3
.gitignore
vendored
3
.gitignore
vendored
@ -1,4 +1,5 @@
|
||||
/target
|
||||
/Cargo.lock
|
||||
/crates/*/target
|
||||
/vendor/
|
||||
vendor
|
||||
composer.lock
|
||||
|
@ -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 <daniil@daniil.it>"]
|
||||
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"]
|
||||
|
||||
|
8
examples/reqwest/.cargo/config.toml
Normal file
8
examples/reqwest/.cargo/config.toml
Normal 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"
|
14
examples/reqwest/Cargo.toml
Normal file
14
examples/reqwest/Cargo.toml
Normal 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"
|
5
examples/reqwest/composer.json
Normal file
5
examples/reqwest/composer.json
Normal file
@ -0,0 +1,5 @@
|
||||
{
|
||||
"require": {
|
||||
"revolt/event-loop": "^1.0"
|
||||
}
|
||||
}
|
28
examples/reqwest/lib/Client.php
Normal file
28
examples/reqwest/lib/Client.php
Normal 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);
|
||||
}
|
||||
}
|
34
examples/reqwest/src/lib.rs
Normal file
34
examples/reqwest/src/lib.rs
Normal 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)
|
||||
}
|
||||
|
5
examples/reqwest/test.php
Normal file
5
examples/reqwest/test.php
Normal file
@ -0,0 +1,5 @@
|
||||
<?php
|
||||
|
||||
require 'vendor/autoload.php'
|
||||
|
||||
\Reqwest\Client::init();
|
Loading…
Reference in New Issue
Block a user