Skip to content

Commit 43410c8

Browse files
committed
Store T of Object, but the code will be removed next.
1 parent 8bbe7db commit 43410c8

File tree

14 files changed

+145
-37
lines changed

14 files changed

+145
-37
lines changed

examples/hello/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ pub fn get_module() -> Module {
6262
);
6363

6464
// register classes
65-
let mut foo_class = DynamicClass::new();
65+
let mut foo_class = DynamicClass::new("FooClass");
6666
foo_class.add_property("foo", "100".to_string());
6767
foo_class.add_method(
6868
"getFoo",
@@ -80,7 +80,7 @@ pub fn get_module() -> Module {
8080
},
8181
vec![Argument::by_val("foo")],
8282
);
83-
module.add_class("FooClass", foo_class);
83+
module.add_class(foo_class);
8484

8585
module
8686
}

examples/http-client/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ license = "Unlicense"
1212
crate-type = ["cdylib"]
1313

1414
[dependencies]
15+
anyhow = "1.0.40"
1516
phper = { version = "0.2.0-alpha.2", path = "../../phper" }
1617
reqwest = { version = "0.11.3", features = ["blocking"] }
18+
thiserror = "1.0.24"
1719

1820
[dev-dependencies]
1921
phper-test = { version = "0.2.0-alpha.2", path = "../../phper-test" }

examples/http-client/src/client.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
use reqwest::blocking::{Client, ClientBuilder};
2+
use std::time::Duration;
3+
use phper::classes::DynamicClass;
4+
use anyhow::Context;
5+
use crate::errors::HttpClientError;
6+
use phper::functions::Argument;
7+
8+
const HTTP_CLIENT_CLASS_NAME: &'static str = "HttpClient\\HttpClient";
9+
10+
pub fn make_client_class() -> DynamicClass<Client> {
11+
let mut http_client_class = DynamicClass::new_with_constructor(HTTP_CLIENT_CLASS_NAME, |_| {
12+
let client = ClientBuilder::new()
13+
.timeout(Duration::from_secs(15))
14+
.build()?;
15+
Ok::<_, HttpClientError>(client)
16+
});
17+
18+
http_client_class.add_method("get", |this, arguments| {
19+
}, vec![Argument::by_val("url")]);
20+
21+
http_client_class
22+
}

examples/http-client/src/errors.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
use phper::errors::Throwable;
2+
use phper::classes::{ClassEntry, DynamicClass};
3+
use phper::errors::Error::ClassNotFound;
4+
5+
const EXCEPTION_CLASS_NAME: &'static str = "HttpClient\\HttpClientException";
6+
7+
#[derive(thiserror::Error, Debug)]
8+
pub enum HttpClientError {
9+
#[error(transparent)]
10+
Reqwest(#[from] reqwest::Error),
11+
}
12+
13+
impl Throwable for HttpClientError {
14+
fn class_entry(&self) -> &ClassEntry {
15+
ClassEntry::from_globals(EXCEPTION_CLASS_NAME).unwrap()
16+
}
17+
}
18+
19+
pub fn make_exception_class() -> DynamicClass<()> {
20+
let exception_class = DynamicClass::new(EXCEPTION_CLASS_NAME);
21+
exception_class
22+
}

examples/http-client/src/http_client.rs

Lines changed: 0 additions & 15 deletions
This file was deleted.

examples/http-client/src/lib.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1+
use anyhow::Context;
12
use phper::{classes::DynamicClass, modules::Module, php_get_module};
3+
use crate::client::make_client_class;
24

3-
pub mod http_client;
5+
pub mod client;
6+
pub mod errors;
7+
pub mod request;
8+
pub mod response;
49

510
#[php_get_module]
611
pub fn get_module() -> Module {
@@ -10,9 +15,7 @@ pub fn get_module() -> Module {
1015
env!("CARGO_PKG_AUTHORS"),
1116
);
1217

13-
// let client = HttpClient::new();
14-
let client_class: DynamicClass<()> = DynamicClass::new();
15-
module.add_class("HttpClient", client_class);
18+
module.add_class(make_client_class());
1619

1720
module
1821
}

examples/http-client/src/request.rs

Whitespace-only changes.

examples/http-client/src/response.rs

Whitespace-only changes.

phper-sys/php_wrapper.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,4 +128,8 @@ zend_string *phper_get_function_or_method_name(const zend_function *func) {
128128

129129
void phper_zval_ptr_dtor(zval *pDest) {
130130
ZVAL_PTR_DTOR(pDest);
131+
}
132+
133+
size_t phper_zend_object_properties_size(zend_class_entry *ce) {
134+
return zend_object_properties_size(ce);
131135
}

phper-sys/php_wrapper.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,6 @@ zend_string *phper_get_function_or_method_name(const zend_function *func);
4444

4545
void phper_zval_ptr_dtor(zval *pDest);
4646

47+
size_t phper_zend_object_properties_size(zend_class_entry *ce);
48+
4749
#endif //PHPER_PHP_WRAPPER_H

0 commit comments

Comments
 (0)