Skip to content

Commit 0937eb6

Browse files
committed
Example http-server, add method onRequest.
1 parent 7763790 commit 0937eb6

File tree

8 files changed

+53
-10
lines changed

8 files changed

+53
-10
lines changed

examples/http-client/src/client.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use phper::{
99
objects::Object,
1010
};
1111
use reqwest::blocking::{Client, ClientBuilder, RequestBuilder};
12-
use std::{time::Duration};
12+
use std::time::Duration;
1313

1414
const HTTP_CLIENT_BUILDER_CLASS_NAME: &'static str = "HttpClient\\HttpClientBuilder";
1515
const HTTP_CLIENT_CLASS_NAME: &'static str = "HttpClient\\HttpClient";

examples/http-server/src/lib.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
use crate::{errors::make_exception_class, server::make_server_class};
22
use phper::{modules::Module, php_get_module};
3-
use std::{
4-
mem::{forget},
5-
sync::{Arc},
6-
};
7-
use tokio::{runtime};
3+
use std::{mem::forget, sync::Arc};
4+
use tokio::runtime;
85

96
pub mod errors;
107
pub mod server;
@@ -31,7 +28,7 @@ pub fn get_module() -> Module {
3128
true
3229
});
3330

34-
module.on_module_shutdown(move |_| false);
31+
module.on_module_shutdown(move |_| true);
3532

3633
module.add_class(make_exception_class());
3734
module.add_class(make_server_class());

examples/http-server/src/server.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use phper::{
1010
values::Val,
1111
};
1212
use std::{convert::Infallible, mem::replace, net::SocketAddr};
13-
use tokio::{runtime::Handle};
13+
use tokio::runtime::Handle;
1414

1515
const HTTP_SERVER_CLASS_NAME: &'static str = "HttpServer\\HttpServer";
1616

@@ -19,6 +19,7 @@ pub fn make_server_class() -> DynamicClass<Option<Builder<AddrIncoming>>> {
1919

2020
class.add_property("host", Visibility::Private, "127.0.0.1");
2121
class.add_property("port", Visibility::Private, 8080);
22+
class.add_property("onRequestHandle", Visibility::Private, ());
2223

2324
class.add_method(
2425
"__construct",
@@ -36,6 +37,16 @@ pub fn make_server_class() -> DynamicClass<Option<Builder<AddrIncoming>>> {
3637
vec![Argument::by_val("host"), Argument::by_val("port")],
3738
);
3839

40+
class.add_method(
41+
"onRequest",
42+
Visibility::Public,
43+
|this, arguments| {
44+
this.set_property("onRequestHandle", Val::new(arguments[0].duplicate()?));
45+
Ok::<_, phper::Error>(())
46+
},
47+
vec![Argument::by_val("handle")],
48+
);
49+
3950
class.add_method(
4051
"start",
4152
Visibility::Public,

examples/http-server/tests/php/test.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,8 @@
77
error_reporting(E_ALL);
88

99
$server = new HttpServer("127.0.0.1", 9000);
10-
$server->start();
10+
$server->onRequest(function ($request, $response) {
11+
$response->header('Content-Type', 'text/plain');
12+
$response->end("Hello World\n");
13+
});
14+
// $server->start();

phper-sys/php_wrapper.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,3 +172,7 @@ bool phper_zend_hash_index_exists(const HashTable *ht, zend_ulong h) {
172172
void phper_zval_ptr_dtor_nogc(zval *zval_ptr) {
173173
zval_ptr_dtor_nogc(zval_ptr);
174174
}
175+
176+
bool phper_z_refcounted_p(zval *zval_ptr) {
177+
return Z_REFCOUNTED_P(zval_ptr);
178+
}

phper-sys/php_wrapper.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,6 @@ bool phper_zend_hash_index_exists(const HashTable *ht, zend_ulong h);
6060

6161
void phper_zval_ptr_dtor_nogc(zval *zval_ptr);
6262

63+
bool phper_z_refcounted_p(zval *zval_ptr);
64+
6365
#endif //PHPER_PHP_WRAPPER_H

phper/src/errors.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ pub enum Error {
7777
#[error(transparent)]
7878
#[throwable(transparent)]
7979
InitializeObject(#[from] InitializeObjectError),
80+
81+
#[error(transparent)]
82+
#[throwable(transparent)]
83+
NotRefCountedType(#[from] NotRefCountedTypeError),
8084
}
8185

8286
impl Error {
@@ -146,3 +150,9 @@ pub struct CallMethodError {
146150
pub struct InitializeObjectError {
147151
class_name: String,
148152
}
153+
154+
#[derive(Debug, thiserror::Error, crate::Throwable)]
155+
#[error("the type is not refcounted")]
156+
#[throwable_class("TypeError")]
157+
#[throwable_crate]
158+
pub struct NotRefCountedTypeError;

phper/src/values.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::{
44
alloc::{EAllocatable, EBox},
55
arrays::Array,
66
classes::ClassEntry,
7-
errors::{CallFunctionError, Throwable, TypeError},
7+
errors::{CallFunctionError, NotRefCountedTypeError, Throwable, TypeError},
88
functions::ZendFunction,
99
objects::Object,
1010
strings::ZendString,
@@ -242,6 +242,21 @@ impl Val {
242242
}
243243
}
244244

245+
/// Only add refcount.
246+
///
247+
/// TODO Make a reference type to wrap self.
248+
pub fn duplicate(&mut self) -> Result<EBox<Self>, NotRefCountedTypeError> {
249+
unsafe {
250+
if !phper_z_refcounted_p(self.as_mut_ptr()) {
251+
Err(NotRefCountedTypeError)
252+
} else {
253+
(*self.inner.value.counted).gc.refcount += 1;
254+
let val = EBox::from_raw(self.as_mut_ptr().cast());
255+
Ok(val)
256+
}
257+
}
258+
}
259+
245260
/// Call only when self is a callable.
246261
///
247262
/// # Errors

0 commit comments

Comments
 (0)