Skip to content

Commit

Permalink
Merge pull request #4 from dmalusev/feat/constants
Browse files Browse the repository at this point in the history
Feat/constants
  • Loading branch information
Dusan Malusev authored Jan 10, 2024
2 parents 3ce267d + 89c1c69 commit 45e4410
Show file tree
Hide file tree
Showing 28 changed files with 659 additions and 693 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
/vendor
core
.vscode/
.idea/
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions examples/complex/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ pub fn get_module() -> Module {
foo_class.add_method(
"getFoo",
Visibility::Public,
|this: &mut StateObj<()>, _: &mut [ZVal]| {
|this: &mut StateObj, _: &mut [ZVal]| {
let prop = this.get_property("foo");
Ok::<_, phper::Error>(prop.clone())
},
Expand All @@ -86,7 +86,7 @@ pub fn get_module() -> Module {
.add_method(
"setFoo",
Visibility::Public,
|this: &mut StateObj<()>, arguments: &mut [ZVal]| -> phper::Result<()> {
|this: &mut StateObj, arguments: &mut [ZVal]| -> phper::Result<()> {
this.set_property("foo", arguments[0].clone());
Ok(())
},
Expand Down
16 changes: 9 additions & 7 deletions examples/http-client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ const HTTP_CLIENT_BUILDER_CLASS_NAME: &str = "HttpClient\\HttpClientBuilder";

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

static HTTP_CLIENT_CLASS: StaticStateClass<Option<Client>> = StaticStateClass::null();
static HTTP_CLIENT_CLASS: StaticStateClass = StaticStateClass::null();

pub fn make_client_builder_class() -> ClassEntity<ClientBuilder> {
pub fn make_client_builder_class() -> ClassEntity {
// `new_with_default_state_constructor` means initialize the state of
// `ClientBuilder` as `Default::default`.
let mut class = ClassEntity::new_with_default_state_constructor(HTTP_CLIENT_BUILDER_CLASS_NAME);
let mut class = ClassEntity::new_with_default_state_constructor::<Option<&Client>>(
HTTP_CLIENT_BUILDER_CLASS_NAME,
);

// Inner call the `ClientBuilder::timeout`.
class
Expand Down Expand Up @@ -63,9 +65,9 @@ pub fn make_client_builder_class() -> ClassEntity<ClientBuilder> {
class
}

pub fn make_client_class() -> ClassEntity<Option<Client>> {
pub fn make_client_class() -> ClassEntity {
let mut class =
ClassEntity::<Option<Client>>::new_with_default_state_constructor(HTTP_CLIENT_CLASS_NAME);
ClassEntity::new_with_default_state_constructor::<Option<Client>>(HTTP_CLIENT_CLASS_NAME);

class.bind(&HTTP_CLIENT_CLASS);

Expand All @@ -76,7 +78,7 @@ pub fn make_client_class() -> ClassEntity<Option<Client>> {
class
.add_method("get", Visibility::Public, |this, arguments| {
let url = arguments[0].expect_z_str()?.to_str().unwrap();
let client = this.as_state().as_ref().unwrap();
let client = this.as_state::<Option<Client>>().as_ref().unwrap();
let request_builder = client.get(url);
let mut object = REQUEST_BUILDER_CLASS.init_object()?;
*object.as_mut_state() = Some(request_builder);
Expand All @@ -87,7 +89,7 @@ pub fn make_client_class() -> ClassEntity<Option<Client>> {
class
.add_method("post", Visibility::Public, |this, arguments| {
let url = arguments[0].expect_z_str()?.to_str().unwrap();
let client = this.as_state().as_ref().unwrap();
let client = this.as_state::<Option<&Client>>().as_ref().unwrap();
let request_builder = client.post(url);
let mut object = REQUEST_BUILDER_CLASS.init_object()?;
*object.as_mut_state() = Some(request_builder);
Expand Down
2 changes: 1 addition & 1 deletion examples/http-client/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use phper::{
/// The exception class name of extension.
const EXCEPTION_CLASS_NAME: &str = "HttpClient\\HttpClientException";

pub fn make_exception_class() -> ClassEntity<()> {
pub fn make_exception_class() -> ClassEntity {
let mut class = ClassEntity::new(EXCEPTION_CLASS_NAME);
// The `extends` is same as the PHP class `extends`.
class.extends(exception_class);
Expand Down
11 changes: 5 additions & 6 deletions examples/http-client/src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@ use std::{convert::Infallible, mem::take};

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

pub static REQUEST_BUILDER_CLASS: StaticStateClass<Option<RequestBuilder>> =
StaticStateClass::null();
pub static REQUEST_BUILDER_CLASS: StaticStateClass = StaticStateClass::null();

pub fn make_request_builder_class() -> ClassEntity<Option<RequestBuilder>> {
let mut class = ClassEntity::<Option<RequestBuilder>>::new_with_default_state_constructor(
pub fn make_request_builder_class() -> ClassEntity {
let mut class = ClassEntity::new_with_default_state_constructor::<Option<RequestBuilder>>(
REQUEST_BUILDER_CLASS_NAME,
);

Expand All @@ -30,8 +29,8 @@ pub fn make_request_builder_class() -> ClassEntity<Option<RequestBuilder>> {
});

class.add_method("send", Visibility::Public, |this, _arguments| {
let state = take(this.as_mut_state());
let response = state.unwrap().send().map_err(HttpClientError::Reqwest)?;
let state = take(this.as_mut_state::<Option<RequestBuilder>>()).unwrap();
let response = state.send().map_err(HttpClientError::Reqwest)?;
let mut object = RESPONSE_CLASS.new_object([])?;
*object.as_mut_state() = Some(response);
Ok::<_, phper::Error>(object)
Expand Down
12 changes: 6 additions & 6 deletions examples/http-client/src/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,24 @@ use std::mem::take;

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

pub static RESPONSE_CLASS: StaticStateClass<Option<Response>> = StaticStateClass::null();
pub static RESPONSE_CLASS: StaticStateClass = StaticStateClass::null();

pub fn make_response_class() -> ClassEntity<Option<Response>> {
pub fn make_response_class() -> ClassEntity {
let mut class =
ClassEntity::<Option<Response>>::new_with_default_state_constructor(RESPONSE_CLASS_NAME);
ClassEntity::new_with_default_state_constructor::<Option<Response>>(RESPONSE_CLASS_NAME);

class.bind(&RESPONSE_CLASS);

class.add_method("body", Visibility::Public, |this, _arguments| {
let response = take(this.as_mut_state());
let response = take(this.as_mut_state::<Option<Response>>());
let response = response.ok_or(HttpClientError::ResponseHadRead)?;
let body = response.bytes().map_err(HttpClientError::Reqwest)?;
Ok::<_, phper::Error>(body.to_vec())
});

class.add_method("status", Visibility::Public, |this, _arguments| {
let response =
this.as_state()
this.as_state::<Option<Response>>()
.as_ref()
.ok_or_else(|| HttpClientError::ResponseAfterRead {
method_name: "status".to_owned(),
Expand All @@ -47,7 +47,7 @@ pub fn make_response_class() -> ClassEntity<Option<Response>> {

class.add_method("headers", Visibility::Public, |this, _arguments| {
let response =
this.as_state()
this.as_state::<Option<Response>>()
.as_ref()
.ok_or_else(|| HttpClientError::ResponseAfterRead {
method_name: "headers".to_owned(),
Expand Down
2 changes: 1 addition & 1 deletion examples/http-server/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ impl From<HttpServerError> for phper::Error {
}

/// Register the class `HttpServer\HttpServerException` by `ClassEntity`.
pub fn make_exception_class() -> ClassEntity<()> {
pub fn make_exception_class() -> ClassEntity {
let mut class = ClassEntity::new(EXCEPTION_CLASS_NAME);
// As an Exception class, inheriting from the base Exception class is important.
class.extends(exception_class);
Expand Down
15 changes: 8 additions & 7 deletions examples/http-server/src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ use std::convert::Infallible;

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

pub static HTTP_REQUEST_CLASS: StaticStateClass<()> = StaticStateClass::null();
pub static HTTP_REQUEST_CLASS: StaticStateClass = StaticStateClass::null();

/// Register the class `HttpServer\HttpRequest` by `ClassEntity`.
pub fn make_request_class() -> ClassEntity<()> {
pub fn make_request_class() -> ClassEntity {
let mut class = ClassEntity::new(HTTP_REQUEST_CLASS_NAME);

// The state class will be initialized after class registered.
Expand All @@ -34,15 +34,16 @@ pub fn make_request_class() -> ClassEntity<()> {

// Register the constructor method with public visibility, initialize the
// headers with empty array.
class.add_method("__construct", Visibility::Public, |this, _arguments| {
this.set_property("headers", ZArray::new());
Ok::<_, Infallible>(())
});
class
.add_method("__construct", Visibility::Public, |this, _arguments| {
this.set_property("headers", ZArray::new());
Ok::<_, Infallible>(())
});

class
}

/// Instantiate the object with class `HttpServer\HttpRequest`.
pub fn new_request_object() -> phper::Result<StateObject<()>> {
pub fn new_request_object() -> phper::Result<StateObject> {
HTTP_REQUEST_CLASS.new_object([])
}
9 changes: 5 additions & 4 deletions examples/http-server/src/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@ use phper::{

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

pub static HTTP_RESPONSE_CLASS: StaticStateClass<Response<Body>> = StaticStateClass::null();
pub static HTTP_RESPONSE_CLASS: StaticStateClass = StaticStateClass::null();

/// Register the class `HttpServer\HttpResponse` by `ClassEntity`, with the
/// inner state `Response<Body>`.
pub fn make_response_class() -> ClassEntity<Response<Body>> {
let mut class = ClassEntity::new_with_default_state_constructor(HTTP_RESPONSE_CLASS_NAME);
pub fn make_response_class() -> ClassEntity {
let mut class =
ClassEntity::new_with_default_state_constructor::<Response<Body>>(HTTP_RESPONSE_CLASS_NAME);

// The state class will be initialized after class registered.
class.bind(&HTTP_RESPONSE_CLASS);
Expand Down Expand Up @@ -64,6 +65,6 @@ pub fn make_response_class() -> ClassEntity<Response<Body>> {
}

/// Instantiate the object with class `HttpServer\HttpResponse`.
pub fn new_response_object() -> phper::Result<StateObject<Response<Body>>> {
pub fn new_response_object() -> phper::Result<StateObject> {
HTTP_RESPONSE_CLASS.new_object([])
}
2 changes: 1 addition & 1 deletion examples/http-server/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ thread_local! {
}

/// Register the class `HttpServer\HttpServer` by `ClassEntity`.
pub fn make_server_class() -> ClassEntity<()> {
pub fn make_server_class() -> ClassEntity {
let mut class = ClassEntity::new(HTTP_SERVER_CLASS_NAME);

// Register the server host field with public visibility.
Expand Down
18 changes: 18 additions & 0 deletions phper-sys/c/php_constants.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include <phper.h>

zend_constant phper_create_constant(const char *name, size_t name_len, zval val,
int flags) {
zend_constant c = {
.name =
zend_string_init_interned(name, name_len, flags & CONST_PERSISTENT),
.value = val,
};

return c;
}

zend_result phper_register_constant(zend_constant *constant, int flags,
int module_number) {
ZEND_CONSTANT_SET_FLAGS(constant, flags, module_number);
return zend_register_constant(constant);
}
Loading

0 comments on commit 45e4410

Please sign in to comment.