|
1 |
| -#![cfg_attr(windows, feature(abi_vectorcall))] |
2 |
| -use ext_php_rs::{ |
3 |
| - binary::Binary, |
4 |
| - boxed::ZBox, |
5 |
| - prelude::*, |
6 |
| - types::{ArrayKey, ZendHashTable, ZendObject, Zval}, |
7 |
| - zend::ProcessGlobals, |
8 |
| -}; |
9 |
| -use std::collections::HashMap; |
10 |
| - |
11 |
| -#[php_function] |
12 |
| -pub fn test_str(a: &str) -> &str { |
13 |
| - a |
14 |
| -} |
15 |
| - |
16 |
| -#[php_function] |
17 |
| -pub fn test_string(a: String) -> String { |
18 |
| - a |
19 |
| -} |
| 1 | +use ext_php_rs::prelude::php_module; |
20 | 2 |
|
21 |
| -#[php_function] |
22 |
| -pub fn test_bool(a: bool) -> bool { |
23 |
| - a |
24 |
| -} |
| 3 | +#[php_module] |
| 4 | +mod module { |
| 5 | + use ext_php_rs::{ |
| 6 | + binary::Binary, |
| 7 | + boxed::ZBox, |
| 8 | + prelude::*, |
| 9 | + types::{ArrayKey, ZendHashTable, ZendObject, Zval}, |
| 10 | + zend::ProcessGlobals, |
| 11 | + }; |
| 12 | + use std::collections::HashMap; |
| 13 | + |
| 14 | + #[php_function()] |
| 15 | + pub fn test_str(a: &str) -> &str { |
| 16 | + a |
| 17 | + } |
25 | 18 |
|
26 |
| -#[php_function] |
27 |
| -pub fn test_number_signed(a: i32) -> i32 { |
28 |
| - a |
29 |
| -} |
| 19 | + #[php_function()] |
| 20 | + pub fn test_string(a: String) -> String { |
| 21 | + a |
| 22 | + } |
30 | 23 |
|
31 |
| -#[php_function] |
32 |
| -pub fn test_number_unsigned(a: u32) -> u32 { |
33 |
| - a |
34 |
| -} |
| 24 | + #[php_function()] |
| 25 | + pub fn test_bool(a: bool) -> bool { |
| 26 | + a |
| 27 | + } |
35 | 28 |
|
36 |
| -#[php_function] |
37 |
| -pub fn test_number_float(a: f32) -> f32 { |
38 |
| - a |
39 |
| -} |
| 29 | + #[php_function()] |
| 30 | + pub fn test_number_signed(a: i32) -> i32 { |
| 31 | + a |
| 32 | + } |
40 | 33 |
|
41 |
| -#[php_function] |
42 |
| -pub fn test_array(a: Vec<String>) -> Vec<String> { |
43 |
| - a |
44 |
| -} |
| 34 | + #[php_function()] |
| 35 | + pub fn test_number_unsigned(a: u32) -> u32 { |
| 36 | + a |
| 37 | + } |
45 | 38 |
|
46 |
| -#[php_function] |
47 |
| -pub fn test_array_assoc(a: HashMap<String, String>) -> HashMap<String, String> { |
48 |
| - a |
49 |
| -} |
| 39 | + #[php_function()] |
| 40 | + pub fn test_number_float(a: f32) -> f32 { |
| 41 | + a |
| 42 | + } |
50 | 43 |
|
51 |
| -#[php_function] |
52 |
| -pub fn test_binary(a: Binary<u32>) -> Binary<u32> { |
53 |
| - a |
54 |
| -} |
| 44 | + #[php_function()] |
| 45 | + pub fn test_array(a: Vec<String>) -> Vec<String> { |
| 46 | + a |
| 47 | + } |
55 | 48 |
|
56 |
| -#[php_function] |
57 |
| -pub fn test_nullable(a: Option<String>) -> Option<String> { |
58 |
| - a |
59 |
| -} |
| 49 | + #[php_function()] |
| 50 | + pub fn test_array_assoc(a: HashMap<String, String>) -> HashMap<String, String> { |
| 51 | + a |
| 52 | + } |
60 | 53 |
|
61 |
| -#[php_function] |
62 |
| -pub fn test_object(a: &mut ZendObject) -> &mut ZendObject { |
63 |
| - a |
64 |
| -} |
| 54 | + #[php_function()] |
| 55 | + pub fn test_binary(a: Binary<u32>) -> Binary<u32> { |
| 56 | + a |
| 57 | + } |
65 | 58 |
|
66 |
| -// GLOBALS |
67 |
| -#[php_function] |
68 |
| -pub fn test_globals_http_get() -> ZBox<ZendHashTable> { |
69 |
| - ProcessGlobals::get().http_get_vars().to_owned() |
70 |
| -} |
| 59 | + #[php_function()] |
| 60 | + pub fn test_nullable(a: Option<String>) -> Option<String> { |
| 61 | + a |
| 62 | + } |
71 | 63 |
|
72 |
| -#[php_function] |
73 |
| -pub fn test_globals_http_post() -> ZBox<ZendHashTable> { |
74 |
| - ProcessGlobals::get().http_post_vars().to_owned() |
75 |
| -} |
| 64 | + #[php_function()] |
| 65 | + pub fn test_object(a: &mut ZendObject) -> &mut ZendObject { |
| 66 | + a |
| 67 | + } |
76 | 68 |
|
77 |
| -#[php_function] |
78 |
| -pub fn test_globals_http_cookie() -> ZBox<ZendHashTable> { |
79 |
| - ProcessGlobals::get().http_cookie_vars().to_owned() |
80 |
| -} |
| 69 | + // GLOBALS |
| 70 | + #[php_function] |
| 71 | + pub fn test_globals_http_get() -> ZBox<ZendHashTable> { |
| 72 | + ProcessGlobals::get().http_get_vars().to_owned() |
| 73 | + } |
81 | 74 |
|
82 |
| -#[php_function] |
83 |
| -pub fn test_globals_http_server() -> ZBox<ZendHashTable> { |
84 |
| - ProcessGlobals::get().http_server_vars().unwrap().to_owned() |
85 |
| -} |
| 75 | + #[php_function] |
| 76 | + pub fn test_globals_http_post() -> ZBox<ZendHashTable> { |
| 77 | + ProcessGlobals::get().http_post_vars().to_owned() |
| 78 | + } |
86 | 79 |
|
87 |
| -#[php_function] |
88 |
| -pub fn test_globals_http_request() -> ZBox<ZendHashTable> { |
89 |
| - ProcessGlobals::get() |
90 |
| - .http_request_vars() |
91 |
| - .unwrap() |
92 |
| - .to_owned() |
93 |
| -} |
| 80 | + #[php_function] |
| 81 | + pub fn test_globals_http_cookie() -> ZBox<ZendHashTable> { |
| 82 | + ProcessGlobals::get().http_cookie_vars().to_owned() |
| 83 | + } |
94 | 84 |
|
95 |
| -#[php_function] |
96 |
| -pub fn test_globals_http_files() -> ZBox<ZendHashTable> { |
97 |
| - ProcessGlobals::get().http_files_vars().to_owned() |
98 |
| -} |
| 85 | + #[php_function] |
| 86 | + pub fn test_globals_http_server() -> ZBox<ZendHashTable> { |
| 87 | + ProcessGlobals::get().http_server_vars().unwrap().to_owned() |
| 88 | + } |
99 | 89 |
|
100 |
| -#[php_function] |
101 |
| -pub fn test_closure() -> Closure { |
102 |
| - Closure::wrap(Box::new(|a| a) as Box<dyn Fn(String) -> String>) |
103 |
| -} |
| 90 | + #[php_function] |
| 91 | + pub fn test_globals_http_request() -> ZBox<ZendHashTable> { |
| 92 | + ProcessGlobals::get() |
| 93 | + .http_request_vars() |
| 94 | + .unwrap() |
| 95 | + .to_owned() |
| 96 | + } |
104 | 97 |
|
105 |
| -#[php_function] |
106 |
| -pub fn test_closure_once(a: String) -> Closure { |
107 |
| - Closure::wrap_once(Box::new(move || a) as Box<dyn FnOnce() -> String>) |
108 |
| -} |
| 98 | + #[php_function] |
| 99 | + pub fn test_globals_http_files() -> ZBox<ZendHashTable> { |
| 100 | + ProcessGlobals::get().http_files_vars().to_owned() |
| 101 | + } |
109 | 102 |
|
110 |
| -#[php_function] |
111 |
| -pub fn test_callable(call: ZendCallable, a: String) -> Zval { |
112 |
| - call.try_call(vec![&a]).expect("Failed to call function") |
113 |
| -} |
| 103 | + #[php_function()] |
| 104 | + pub fn test_closure() -> Closure { |
| 105 | + Closure::wrap(Box::new(|a| a) as Box<dyn Fn(String) -> String>) |
| 106 | + } |
114 | 107 |
|
115 |
| -#[php_function] |
116 |
| -pub fn iter_next(ht: &ZendHashTable) -> Vec<Zval> { |
117 |
| - ht.iter() |
118 |
| - .flat_map(|(k, v)| [key_to_zval(k), v.shallow_clone()]) |
119 |
| - .collect() |
120 |
| -} |
| 108 | + #[php_function()] |
| 109 | + pub fn test_closure_once(a: String) -> Closure { |
| 110 | + Closure::wrap_once(Box::new(move || a) as Box<dyn FnOnce() -> String>) |
| 111 | + } |
121 | 112 |
|
122 |
| -#[php_function] |
123 |
| -pub fn iter_back(ht: &ZendHashTable) -> Vec<Zval> { |
124 |
| - ht.iter() |
125 |
| - .rev() |
126 |
| - .flat_map(|(k, v)| [key_to_zval(k), v.shallow_clone()]) |
127 |
| - .collect() |
128 |
| -} |
| 113 | + #[php_function()] |
| 114 | + pub fn test_callable(call: ZendCallable, a: String) -> Zval { |
| 115 | + call.try_call(vec![&a]).expect("Failed to call function") |
| 116 | + } |
129 | 117 |
|
130 |
| -#[php_function] |
131 |
| -pub fn iter_next_back(ht: &ZendHashTable, modulus: usize) -> Vec<Option<Zval>> { |
132 |
| - let mut result = Vec::with_capacity(ht.len()); |
133 |
| - let mut iter = ht.iter(); |
| 118 | + #[php_function] |
| 119 | + pub fn iter_next(ht: &ZendHashTable) -> Vec<Zval> { |
| 120 | + ht.iter() |
| 121 | + .flat_map(|(k, v)| [key_to_zval(k), v.shallow_clone()]) |
| 122 | + .collect() |
| 123 | + } |
134 | 124 |
|
135 |
| - for i in 0..ht.len() + modulus { |
136 |
| - let entry = if i % modulus == 0 { |
137 |
| - iter.next_back() |
138 |
| - } else { |
139 |
| - iter.next() |
140 |
| - }; |
| 125 | + #[php_function] |
| 126 | + pub fn iter_back(ht: &ZendHashTable) -> Vec<Zval> { |
| 127 | + ht.iter() |
| 128 | + .rev() |
| 129 | + .flat_map(|(k, v)| [key_to_zval(k), v.shallow_clone()]) |
| 130 | + .collect() |
| 131 | + } |
141 | 132 |
|
142 |
| - if let Some((k, v)) = entry { |
143 |
| - result.push(Some(key_to_zval(k))); |
144 |
| - result.push(Some(v.shallow_clone())); |
145 |
| - } else { |
146 |
| - result.push(None); |
| 133 | + #[php_function] |
| 134 | + pub fn iter_next_back(ht: &ZendHashTable, modulus: usize) -> Vec<Option<Zval>> { |
| 135 | + let mut result = Vec::with_capacity(ht.len()); |
| 136 | + let mut iter = ht.iter(); |
| 137 | + |
| 138 | + for i in 0..ht.len() + modulus { |
| 139 | + let entry = if i % modulus == 0 { |
| 140 | + iter.next_back() |
| 141 | + } else { |
| 142 | + iter.next() |
| 143 | + }; |
| 144 | + |
| 145 | + if let Some((k, v)) = entry { |
| 146 | + result.push(Some(key_to_zval(k))); |
| 147 | + result.push(Some(v.shallow_clone())); |
| 148 | + } else { |
| 149 | + result.push(None); |
| 150 | + } |
147 | 151 | }
|
148 |
| - } |
149 | 152 |
|
150 |
| - result |
151 |
| -} |
| 153 | + result |
| 154 | + } |
152 | 155 |
|
153 |
| -fn key_to_zval(key: ArrayKey) -> Zval { |
154 |
| - match key { |
155 |
| - ArrayKey::String(s) => { |
156 |
| - let mut zval = Zval::new(); |
157 |
| - let _ = zval.set_string(s.as_str(), false); |
158 |
| - zval |
159 |
| - } |
160 |
| - ArrayKey::Long(l) => { |
161 |
| - let mut zval = Zval::new(); |
162 |
| - zval.set_long(l); |
163 |
| - zval |
| 156 | + fn key_to_zval(key: ArrayKey) -> Zval { |
| 157 | + match key { |
| 158 | + ArrayKey::String(s) => { |
| 159 | + let mut zval = Zval::new(); |
| 160 | + let _ = zval.set_string(s.as_str(), false); |
| 161 | + zval |
| 162 | + } |
| 163 | + ArrayKey::Long(l) => { |
| 164 | + let mut zval = Zval::new(); |
| 165 | + zval.set_long(l); |
| 166 | + zval |
| 167 | + } |
164 | 168 | }
|
165 | 169 | }
|
166 |
| -} |
167 |
| - |
168 |
| -#[php_class] |
169 |
| -pub struct TestClass { |
170 |
| - string: String, |
171 |
| - number: i32, |
172 |
| - #[prop] |
173 |
| - boolean: bool, |
174 |
| -} |
175 |
| - |
176 |
| -#[php_impl] |
177 |
| -impl TestClass { |
178 |
| - #[getter] |
179 |
| - pub fn get_string(&self) -> String { |
180 |
| - self.string.to_string() |
| 170 | + #[php_class] |
| 171 | + pub struct TestClass { |
| 172 | + string: String, |
| 173 | + number: i32, |
| 174 | + #[prop] |
| 175 | + boolean: bool, |
181 | 176 | }
|
182 | 177 |
|
183 |
| - #[setter] |
184 |
| - pub fn set_string(&mut self, string: String) { |
185 |
| - self.string = string; |
186 |
| - } |
| 178 | + #[php_impl] |
| 179 | + impl TestClass { |
| 180 | + #[getter] |
| 181 | + pub fn get_string(&self) -> String { |
| 182 | + self.string.to_string() |
| 183 | + } |
187 | 184 |
|
188 |
| - #[getter] |
189 |
| - pub fn get_number(&self) -> i32 { |
190 |
| - self.number |
191 |
| - } |
| 185 | + #[setter] |
| 186 | + pub fn set_string(&mut self, string: String) { |
| 187 | + self.string = string; |
| 188 | + } |
192 | 189 |
|
193 |
| - #[setter] |
194 |
| - pub fn set_number(&mut self, number: i32) { |
195 |
| - self.number = number; |
196 |
| - } |
197 |
| -} |
| 190 | + #[getter] |
| 191 | + pub fn get_number(&self) -> i32 { |
| 192 | + self.number |
| 193 | + } |
198 | 194 |
|
199 |
| -#[php_function] |
200 |
| -pub fn test_class(string: String, number: i32) -> TestClass { |
201 |
| - TestClass { |
202 |
| - string, |
203 |
| - number, |
204 |
| - boolean: true, |
| 195 | + #[setter] |
| 196 | + pub fn set_number(&mut self, number: i32) { |
| 197 | + self.number = number; |
| 198 | + } |
205 | 199 | }
|
206 |
| -} |
207 | 200 |
|
208 |
| -#[php_module] |
209 |
| -pub fn get_module(module: ModuleBuilder) -> ModuleBuilder { |
210 |
| - module |
| 201 | + #[php_function()] |
| 202 | + pub fn test_class(string: String, number: i32) -> TestClass { |
| 203 | + TestClass { |
| 204 | + string, |
| 205 | + number, |
| 206 | + boolean: true, |
| 207 | + } |
| 208 | + } |
211 | 209 | }
|
212 | 210 |
|
213 | 211 | #[cfg(test)]
|
|
0 commit comments