Skip to content

Commit 3003935

Browse files
authored
Merge pull request #21 from jmjoy/0.2.x-dev
2 parents c1477d4 + d310555 commit 3003935

File tree

32 files changed

+629
-155
lines changed

32 files changed

+629
-155
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ members = [
1010
# internal
1111
"examples/hello",
1212
"examples/http-client",
13+
"examples/http-server",
1314
"examples/logging",
1415
"tests/integration",
1516
]

examples/http-client/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# hello
1+
# http-client
22

33
Http client example.
44

examples/http-client/src/client.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ pub fn make_client_builder_class() -> DynamicClass<ClientBuilder> {
2222
Visibility::Public,
2323
|this, arguments| {
2424
let ms = arguments[0].as_long()?;
25-
let state = this.as_mut_state();
26-
replace_and_set(state, ClientBuilder::new(), |builder| {
25+
let state: &mut ClientBuilder = this.as_mut_state();
26+
replace_and_set(state, |builder| {
2727
builder.timeout(Duration::from_millis(ms as u64))
2828
});
29-
Ok::<_, HttpClientError>(())
29+
Ok::<_, HttpClientError>(this.duplicate())
3030
},
3131
vec![Argument::by_val("ms")],
3232
);
@@ -37,10 +37,8 @@ pub fn make_client_builder_class() -> DynamicClass<ClientBuilder> {
3737
|this, arguments| {
3838
let enable = arguments[0].as_bool()?;
3939
let state = this.as_mut_state();
40-
replace_and_set(state, ClientBuilder::new(), |builder| {
41-
builder.cookie_store(enable)
42-
});
43-
Ok::<_, HttpClientError>(())
40+
replace_and_set(state, |builder| builder.cookie_store(enable));
41+
Ok::<_, HttpClientError>(this.duplicate())
4442
},
4543
vec![Argument::by_val("enable")],
4644
);
@@ -50,7 +48,7 @@ pub fn make_client_builder_class() -> DynamicClass<ClientBuilder> {
5048
Visibility::Public,
5149
|this, _arguments| {
5250
let state = this.as_mut_state();
53-
let client = replace_and_get(state, ClientBuilder::new(), ClientBuilder::build)?;
51+
let client = replace_and_get(state, ClientBuilder::build)?;
5452
let mut object = ClassEntry::<Option<Client>>::from_globals(HTTP_CLIENT_CLASS_NAME)?
5553
.init_object()?;
5654
*object.as_mut_state() = Some(client);
@@ -63,7 +61,7 @@ pub fn make_client_builder_class() -> DynamicClass<ClientBuilder> {
6361
}
6462

6563
pub fn make_client_class() -> DynamicClass<Option<Client>> {
66-
let mut class = DynamicClass::new_with_none(HTTP_CLIENT_CLASS_NAME);
64+
let mut class = DynamicClass::new_with_default(HTTP_CLIENT_CLASS_NAME);
6765

6866
class.add_method(
6967
"__construct",

examples/http-client/src/request.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use reqwest::blocking::{RequestBuilder, Response};
88
pub const REQUEST_BUILDER_CLASS_NAME: &'static str = "HttpClient\\RequestBuilder";
99

1010
pub fn make_request_builder_class() -> DynamicClass<Option<RequestBuilder>> {
11-
let mut class = DynamicClass::new_with_none(REQUEST_BUILDER_CLASS_NAME);
11+
let mut class = DynamicClass::new_with_default(REQUEST_BUILDER_CLASS_NAME);
1212

1313
class.add_method(
1414
"__construct",
@@ -22,7 +22,7 @@ pub fn make_request_builder_class() -> DynamicClass<Option<RequestBuilder>> {
2222
Visibility::Public,
2323
|this, _arguments| {
2424
let state = this.as_mut_state();
25-
let response = replace_and_get(state, None, |builder| builder.unwrap().send())?;
25+
let response = replace_and_get(state, |builder| builder.unwrap().send())?;
2626
let mut object =
2727
ClassEntry::<Option<Response>>::from_globals(RESPONSE_CLASS_NAME)?.init_object()?;
2828
*object.as_mut_state() = Some(response);

examples/http-client/src/response.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ use reqwest::blocking::Response;
99
pub const RESPONSE_CLASS_NAME: &'static str = "HttpClient\\Response";
1010

1111
pub fn make_response_class() -> DynamicClass<Option<Response>> {
12-
let mut class = DynamicClass::new_with_none(RESPONSE_CLASS_NAME);
12+
let mut class = DynamicClass::new_with_default(RESPONSE_CLASS_NAME);
1313

1414
class.add_method(
1515
"body",
1616
Visibility::Public,
1717
|this: &mut Object<Option<Response>>, _arguments| {
1818
let response = this.as_mut_state();
19-
let body = replace_and_get(response, None, |response| {
19+
let body = replace_and_get(response, |response| {
2020
response
2121
.ok_or(HttpClientError::ResponseHadRead)
2222
.and_then(|response| response.bytes().map_err(Into::into))

examples/http-client/src/utils.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
use std::mem::replace;
22

3-
pub fn replace_and_set<T>(t: &mut T, init: T, f: impl FnOnce(T) -> T) {
4-
let x = f(replace(t, init));
3+
pub fn replace_and_set<T: Default>(t: &mut T, f: impl FnOnce(T) -> T) {
4+
let x = f(replace(t, Default::default()));
55
let _ = replace(t, x);
66
}
77

8-
pub fn replace_and_get<T, R>(t: &mut T, init: T, f: impl FnOnce(T) -> R) -> R {
9-
f(replace(t, init))
8+
pub fn replace_and_get<T: Default, R>(t: &mut T, f: impl FnOnce(T) -> R) -> R {
9+
f(replace(t, Default::default()))
1010
}

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,12 @@
88
ini_set("display_startup_errors", "On");
99
error_reporting(E_ALL);
1010

11-
$client_builder = new HttpClientBuilder();
12-
$client_builder->timeout(15000);
13-
$client_builder->cookie_store(true);
14-
$client = $client_builder->build();
11+
$client = (new HttpClientBuilder())
12+
->timeout(15000)
13+
->cookie_store(true)
14+
->build();
1515

16-
$request_builder = $client->get("https://httpbin.org/ip");
17-
$response = $request_builder->send();
16+
$response = $client->get("https://httpbin.org/ip")->send();
1817
var_dump([
1918
"status" => $response->status(),
2019
"headers" => $response->headers(),

examples/http-server/Cargo.toml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
[package]
2+
name = "http-server"
3+
version = "0.0.0"
4+
authors = ["jmjoy <[email protected]>"]
5+
edition = "2018"
6+
publish = false
7+
license = "Unlicense"
8+
9+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
10+
11+
[lib]
12+
crate-type = ["cdylib"]
13+
14+
[dependencies]
15+
hyper = { version = "0.14.8", features = ["http1", "runtime", "server"] }
16+
phper = { version = "0.2.0", path = "../../phper" }
17+
thiserror = "1.0.24"
18+
tokio = { version = "1.6.0", features = ["full"] }
19+
20+
[dev-dependencies]
21+
phper-test = { version = "0.2.0", path = "../../phper-test" }
22+
reqwest = "0.11.3"

examples/http-server/LICENSE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../LICENSE

examples/http-server/README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# http-server
2+
3+
Http server example.
4+
5+
## Environment
6+
7+
```bash
8+
# Specify if php isn't installed globally.
9+
export PHP_CONFIG = <Your path of php-config>
10+
```
11+
12+
## Build
13+
14+
```bash
15+
cargo build --release
16+
```
17+
18+
## Test
19+
20+
```bash
21+
cargo test --release
22+
```
23+
24+
## Install
25+
26+
```bash
27+
cargo run --release -- install
28+
```
29+
30+
## License
31+
32+
[Unlicense](https://github.com/jmjoy/phper/blob/master/LICENSE).

0 commit comments

Comments
 (0)