Skip to content

Commit 51bd691

Browse files
committed
Example mini_curl: Constructor.
1 parent 6dd741c commit 51bd691

File tree

4 files changed

+119
-21
lines changed

4 files changed

+119
-21
lines changed

examples/mini-curl/src/lib.rs

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
1+
use curl::easy::Easy;
12
use phper::{
23
c_str_ptr, php_fn, php_function, php_minfo, php_minfo_function, php_minit, php_minit_function,
34
php_mshutdown, php_mshutdown_function, php_rinit, php_rinit_function, php_rshutdown,
45
php_rshutdown_function,
5-
sys::{php_info_print_table_end, php_info_print_table_start, PHP_INI_SYSTEM},
6+
sys::{
7+
php_error_docref0, php_info_print_table_end, php_info_print_table_start, E_WARNING,
8+
PHP_INI_SYSTEM,
9+
},
610
zend::{
711
api::{FunctionEntries, FunctionEntryBuilder},
8-
compile::{create_zend_arg_info, MultiInternalArgInfo},
12+
compile::{create_zend_arg_info, MultiInternalArgInfo, Visibility},
913
ini::{create_ini_entry, IniEntries},
1014
modules::{create_zend_module_entry, ModuleArgs, ModuleEntry},
11-
types::{ClassEntry, ExecuteData, ReturnValue, SetVal},
15+
types::{ClassEntry, ExecuteData, ReturnValue, SetVal, Value},
1216
},
1317
zend_get_module,
1418
};
@@ -23,6 +27,7 @@ static INI_ENTRIES: IniEntries<1> =
2327
fn module_init(args: ModuleArgs) -> bool {
2428
args.register_ini_entries(&INI_ENTRIES);
2529
MINI_CURL_CE.init("MiniCurl", &MINI_CURL_METHODS);
30+
MINI_CURL_CE.declare_property("_rust_easy_ptr", 0, Visibility::Private);
2631
true
2732
}
2833

@@ -69,13 +74,31 @@ static MINI_CURL_METHODS: FunctionEntries<2> = FunctionEntries::new([
6974

7075
#[php_function]
7176
pub fn mini_curl_construct(execute_data: &mut ExecuteData) -> impl SetVal {
72-
match execute_data
73-
.parse_parameters_optional("")
74-
.map(|_url: &str| {})
75-
{
76-
Some(_) => ReturnValue::Bool(true),
77-
None => ReturnValue::Bool(false),
77+
let url = match execute_data.parse_parameters_optional::<&str, _>("") {
78+
Some(url) => url,
79+
None => return ReturnValue::Bool(false),
80+
};
81+
82+
let this = execute_data.get_this();
83+
84+
let mut easy = Box::new(Easy::new());
85+
86+
if !url.is_empty() {
87+
if let Err(e) = easy.url(url) {
88+
unsafe {
89+
php_error_docref0(
90+
null(),
91+
E_WARNING as i32,
92+
format!("curl set failed: {}\0", e).as_ptr().cast(),
93+
);
94+
}
95+
return ReturnValue::Bool(false);
96+
}
7897
}
98+
99+
MINI_CURL_CE.update_property(this, "_rust_easy_ptr", Box::into_raw(easy) as i64);
100+
101+
ReturnValue::Null
79102
}
80103

81104
#[php_function]
@@ -84,6 +107,15 @@ pub fn mini_curl_destruct(execute_data: &mut ExecuteData) -> impl SetVal {
84107
return ReturnValue::Bool(false);
85108
}
86109

110+
let this = execute_data.get_this();
111+
let ptr = MINI_CURL_CE.read_property(this, "_rust_easy_ptr");
112+
let ptr = ptr.try_into_value().unwrap();
113+
if let Value::Long(ptr) = ptr {
114+
unsafe {
115+
drop(Box::from_raw(ptr as *mut Easy));
116+
}
117+
}
118+
87119
ReturnValue::Null
88120
}
89121

examples/mini-curl/tests/php/test.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
ini_set("display_startup_errors", "On");
55
error_reporting(E_ALL);
66

7-
new MiniCurl("http://httpbin.org/ip");
7+
$mc = new MiniCurl("http://httpbin.org/ip");
8+
var_dump($mc);
89

910
function assert_eq($left, $right) {
1011
if ($left !== $right) {

phper-sys/php_wrapper.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
#include <ext/standard/info.h>
77
#include <zend_exceptions.h>
88

9+
typedef void (ZEND_FASTCALL *zif_handler)(INTERNAL_FUNCTION_PARAMETERS);
10+
911
zend_string *zend_string_init_(const char *str, size_t len, int persistent);
1012
zend_string *zend_new_interned_string_(zend_string *str);
1113
zend_class_entry phper_init_class_entry_ex(const char *class_name, size_t class_name_len, const zend_function_entry *functions);

phper/src/zend/types.rs

Lines changed: 74 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ use crate::{
33
sys::{
44
self, phper_get_this, phper_init_class_entry_ex, phper_z_strval_p, phper_zval_get_type,
55
phper_zval_stringl, zend_class_entry, zend_declare_property_bool,
6-
zend_declare_property_null, zend_declare_property_stringl, zend_execute_data, zend_long,
7-
zend_parse_parameters, zend_read_property, zend_register_internal_class,
8-
zend_throw_exception, zval, IS_DOUBLE, IS_FALSE, IS_LONG, IS_NULL, IS_TRUE,
9-
ZEND_RESULT_CODE_SUCCESS,
6+
zend_declare_property_long, zend_declare_property_null, zend_declare_property_stringl,
7+
zend_execute_data, zend_long, zend_parse_parameters, zend_read_property,
8+
zend_register_internal_class, zend_throw_exception, zend_update_property_bool,
9+
zend_update_property_long, zend_update_property_null, zend_update_property_stringl, zval,
10+
IS_DOUBLE, IS_FALSE, IS_LONG, IS_NULL, IS_TRUE, ZEND_RESULT_CODE_SUCCESS,
1011
},
1112
zend::{api::FunctionEntries, compile::Visibility, exceptions::Throwable},
1213
};
@@ -57,16 +58,24 @@ impl ClassEntry {
5758
pub fn declare_property(
5859
&self,
5960
name: impl AsRef<str>,
60-
value: impl DeclareProperty,
61+
value: impl HandleProperty,
6162
access_type: Visibility,
6263
) -> bool {
6364
unsafe {
64-
let name = name.as_ref();
65-
value.declare_property(self.get(), name, access_type as c_int)
65+
value.declare_property(self.get(), name.as_ref(), access_type as c_int)
6666
== ZEND_RESULT_CODE_SUCCESS
6767
}
6868
}
6969

70+
pub fn update_property(
71+
&self,
72+
object: *mut zval,
73+
name: impl AsRef<str>,
74+
value: impl HandleProperty,
75+
) {
76+
unsafe { value.update_property(self.get(), object, name.as_ref()) }
77+
}
78+
7079
pub fn read_property(&self, this: *mut zval, name: impl AsRef<str>) -> &mut Val {
7180
let name = name.as_ref();
7281
unsafe {
@@ -85,16 +94,18 @@ impl ClassEntry {
8594

8695
unsafe impl Sync for ClassEntry {}
8796

88-
pub trait DeclareProperty {
97+
pub trait HandleProperty {
8998
unsafe fn declare_property(
9099
self,
91100
ce: *mut zend_class_entry,
92101
name: &str,
93102
access_type: c_int,
94103
) -> c_int;
104+
105+
unsafe fn update_property(self, scope: *mut zend_class_entry, object: *mut zval, name: &str);
95106
}
96107

97-
impl DeclareProperty for () {
108+
impl HandleProperty for () {
98109
unsafe fn declare_property(
99110
self,
100111
ce: *mut zend_class_entry,
@@ -103,9 +114,13 @@ impl DeclareProperty for () {
103114
) -> i32 {
104115
zend_declare_property_null(ce, name.as_ptr().cast(), name.len(), access_type)
105116
}
117+
118+
unsafe fn update_property(self, scope: *mut zend_class_entry, object: *mut zval, name: &str) {
119+
zend_update_property_null(scope, object, name.as_ptr().cast(), name.len())
120+
}
106121
}
107122

108-
impl DeclareProperty for bool {
123+
impl HandleProperty for bool {
109124
unsafe fn declare_property(
110125
self,
111126
ce: *mut zend_class_entry,
@@ -120,9 +135,46 @@ impl DeclareProperty for bool {
120135
access_type,
121136
)
122137
}
138+
139+
unsafe fn update_property(self, scope: *mut zend_class_entry, object: *mut zval, name: &str) {
140+
zend_update_property_bool(
141+
scope,
142+
object,
143+
name.as_ptr().cast(),
144+
name.len(),
145+
self as zend_long,
146+
)
147+
}
148+
}
149+
150+
impl HandleProperty for i64 {
151+
unsafe fn declare_property(
152+
self,
153+
ce: *mut zend_class_entry,
154+
name: &str,
155+
access_type: i32,
156+
) -> i32 {
157+
zend_declare_property_long(
158+
ce,
159+
name.as_ptr().cast(),
160+
name.len(),
161+
self as zend_long,
162+
access_type,
163+
)
164+
}
165+
166+
unsafe fn update_property(self, scope: *mut zend_class_entry, object: *mut zval, name: &str) {
167+
zend_update_property_long(
168+
scope,
169+
object,
170+
name.as_ptr().cast(),
171+
name.len(),
172+
self as zend_long,
173+
)
174+
}
123175
}
124176

125-
impl DeclareProperty for &str {
177+
impl HandleProperty for &str {
126178
unsafe fn declare_property(
127179
self,
128180
ce: *mut zend_class_entry,
@@ -138,6 +190,17 @@ impl DeclareProperty for &str {
138190
access_type,
139191
)
140192
}
193+
194+
unsafe fn update_property(self, scope: *mut zend_class_entry, object: *mut zval, name: &str) {
195+
zend_update_property_stringl(
196+
scope,
197+
object,
198+
name.as_ptr().cast(),
199+
name.len(),
200+
self.as_ptr().cast(),
201+
self.len(),
202+
)
203+
}
141204
}
142205

143206
#[repr(transparent)]

0 commit comments

Comments
 (0)