Skip to content

Commit 43c8896

Browse files
authored
Support registering interface. (#126)
1 parent 8dcb3c4 commit 43c8896

File tree

14 files changed

+359
-64
lines changed

14 files changed

+359
-64
lines changed

examples/http-client/src/client.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
use crate::{errors::HttpClientError, request::REQUEST_BUILDER_CLASS};
1212
use phper::{
1313
alloc::ToRefOwned,
14-
classes::{ClassEntity, StateClass, Visibility},
14+
classes::{ClassEntity, StaticStateClass, Visibility},
1515
functions::Argument,
1616
};
1717
use reqwest::blocking::{Client, ClientBuilder};
@@ -21,7 +21,7 @@ const HTTP_CLIENT_BUILDER_CLASS_NAME: &str = "HttpClient\\HttpClientBuilder";
2121

2222
const HTTP_CLIENT_CLASS_NAME: &str = "HttpClient\\HttpClient";
2323

24-
static HTTP_CLIENT_CLASS: StateClass<Option<Client>> = StateClass::null();
24+
static HTTP_CLIENT_CLASS: StaticStateClass<Option<Client>> = StaticStateClass::null();
2525

2626
pub fn make_client_builder_class() -> ClassEntity<ClientBuilder> {
2727
// `new_with_default_state_constructor` means initialize the state of

examples/http-client/src/request.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@
99
// See the Mulan PSL v2 for more details.
1010

1111
use crate::{errors::HttpClientError, response::RESPONSE_CLASS};
12-
use phper::classes::{ClassEntity, StateClass, Visibility};
12+
use phper::classes::{ClassEntity, StaticStateClass, Visibility};
1313
use reqwest::blocking::RequestBuilder;
1414
use std::{convert::Infallible, mem::take};
1515

1616
pub const REQUEST_BUILDER_CLASS_NAME: &str = "HttpClient\\RequestBuilder";
1717

18-
pub static REQUEST_BUILDER_CLASS: StateClass<Option<RequestBuilder>> = StateClass::null();
18+
pub static REQUEST_BUILDER_CLASS: StaticStateClass<Option<RequestBuilder>> =
19+
StaticStateClass::null();
1920

2021
pub fn make_request_builder_class() -> ClassEntity<Option<RequestBuilder>> {
2122
let mut class = ClassEntity::<Option<RequestBuilder>>::new_with_default_state_constructor(

examples/http-client/src/response.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@
1111
use crate::errors::HttpClientError;
1212
use phper::{
1313
arrays::{InsertKey, ZArray},
14-
classes::{ClassEntity, StateClass, Visibility},
14+
classes::{ClassEntity, StaticStateClass, Visibility},
1515
values::ZVal,
1616
};
1717
use reqwest::blocking::Response;
1818
use std::mem::take;
1919

2020
pub const RESPONSE_CLASS_NAME: &str = "HttpClient\\Response";
2121

22-
pub static RESPONSE_CLASS: StateClass<Option<Response>> = StateClass::null();
22+
pub static RESPONSE_CLASS: StaticStateClass<Option<Response>> = StaticStateClass::null();
2323

2424
pub fn make_response_class() -> ClassEntity<Option<Response>> {
2525
let mut class =

examples/http-server/src/request.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@
1010

1111
use phper::{
1212
arrays::ZArray,
13-
classes::{ClassEntity, StateClass, Visibility},
13+
classes::{ClassEntity, StaticStateClass, Visibility},
1414
objects::StateObject,
1515
};
1616
use std::convert::Infallible;
1717

1818
pub const HTTP_REQUEST_CLASS_NAME: &str = "HttpServer\\HttpRequest";
1919

20-
pub static HTTP_REQUEST_CLASS: StateClass<()> = StateClass::null();
20+
pub static HTTP_REQUEST_CLASS: StaticStateClass<()> = StaticStateClass::null();
2121

2222
/// Register the class `HttpServer\HttpRequest` by `ClassEntity`.
2323
pub fn make_request_class() -> ClassEntity<()> {

examples/http-server/src/response.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ use axum::{
1414
http::{HeaderName, HeaderValue, Response},
1515
};
1616
use phper::{
17-
classes::{ClassEntity, StateClass, Visibility},
17+
classes::{ClassEntity, StaticStateClass, Visibility},
1818
functions::Argument,
1919
objects::StateObject,
2020
};
2121

2222
pub const HTTP_RESPONSE_CLASS_NAME: &str = "HttpServer\\HttpResponse";
2323

24-
pub static HTTP_RESPONSE_CLASS: StateClass<Response<Body>> = StateClass::null();
24+
pub static HTTP_RESPONSE_CLASS: StaticStateClass<Response<Body>> = StaticStateClass::null();
2525

2626
/// Register the class `HttpServer\HttpResponse` by `ClassEntity`, with the
2727
/// inner state `Response<Body>`.

phper-doc/doc/_02_quick_start/_02_write_a_simple_http_client/index.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ Now let's begin to finish the logic.
193193

194194
use phper::{
195195
alloc::ToRefOwned,
196-
classes::{StateClass, Visibility},
196+
classes::{StaticStateClass, Visibility},
197197
functions::Argument,
198198
};
199199
use reqwest::blocking::{Client, ClientBuilder};
@@ -203,9 +203,9 @@ Now let's begin to finish the logic.
203203

204204
const HTTP_CLIENT_CLASS_NAME: &str = "HttpClient\\HttpClient";
205205

206-
// The static StateClass is bind to ClassEntity of HttpClient, When the class registered,
207-
// the StateClass will be initialized, so you can use it to new stateful object, etc.
208-
static HTTP_CLIENT_CLASS: StateClass<Option<Client>> = StateClass::null();
206+
// The static StaticStateClass is bind to ClassEntity of HttpClient, When the class registered,
207+
// the StaticStateClass will be initialized, so you can use it to new stateful object, etc.
208+
static HTTP_CLIENT_CLASS:StaticStateClass<Option<Client>> =StaticStateClass::null();
209209

210210
pub fn make_client_builder_class() -> ClassEntity<ClientBuilder> {
211211
// `new_with_default_state_constructor` means initialize the state of `ClientBuilder` as
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# Register interface
2+
3+
Registering interfaces is similar of registering classes.
4+
5+
First, you have to new the class builder
6+
[`InterfaceEntity`](phper::classes::InterfaceEntity),
7+
then extends interfaces (if there are),
8+
add public abstract methods, finally add it into the `Module`.
9+
10+
Here is the simplest example:
11+
12+
```rust,no_run
13+
use phper::{classes::InterfaceEntity, modules::Module, php_get_module};
14+
15+
#[php_get_module]
16+
pub fn get_module() -> Module {
17+
let mut module = Module::new(
18+
env!("CARGO_CRATE_NAME"),
19+
env!("CARGO_PKG_VERSION"),
20+
env!("CARGO_PKG_AUTHORS"),
21+
);
22+
23+
let foo = InterfaceEntity::new("Foo");
24+
25+
module.add_interface(foo);
26+
27+
module
28+
}
29+
```
30+
31+
Just like these codes in PHP:
32+
33+
```php
34+
<?php
35+
36+
interface Foo {}
37+
```
38+
39+
## Extends interfaces
40+
41+
If you want the interface `Foo` extends `ArrayAccess` and `Iterator` interfaces.
42+
43+
```rust,no_run
44+
use phper::classes::{InterfaceEntity, ClassEntry};
45+
use phper::classes::{array_access_class, iterator_class};
46+
47+
let mut foo = InterfaceEntity::new("Foo");
48+
foo.extends(|| array_access_class());
49+
foo.extends(|| iterator_class());
50+
```
51+
52+
Same as:
53+
54+
```php
55+
<?php
56+
57+
interface Foo extends ArrayAccess, Iterator {}
58+
```
59+
60+
## Add methods
61+
62+
Interface can add public abstract methods.
63+
64+
```rust,no_run
65+
use phper::classes::{InterfaceEntity, ClassEntry, Visibility};
66+
use phper::functions::Argument;
67+
use phper::objects::StateObj;
68+
use phper::values::ZVal;
69+
70+
let mut foo = InterfaceEntity::new("Foo");
71+
foo.add_method("doSomethings").argument(Argument::by_val("name"));
72+
```
73+
74+
Note that abstract has no method body, so you don't need to add the handler to the method.

phper-doc/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ pub mod _06_module {
6666

6767
#[doc = include_str!("../doc/_06_module/_06_register_class/index.md")]
6868
pub mod _06_register_class {}
69+
70+
#[doc = include_str!("../doc/_06_module/_07_register_interface/index.md")]
71+
pub mod _07_register_interface {}
6972
}
7073

7174
/// TODO

0 commit comments

Comments
 (0)