|
1 | | -mod allocator; |
2 | | - |
3 | | -use ext_php_rs::{ |
4 | | - parse_args, |
5 | | - php::{ |
6 | | - args::Arg, |
7 | | - enums::DataType, |
8 | | - exceptions::PhpException, |
9 | | - execution_data::ExecutionData, |
10 | | - function::FunctionBuilder, |
11 | | - types::{ |
12 | | - array::OwnedHashTable, |
13 | | - callable::Callable, |
14 | | - closure::Closure, |
15 | | - object::{ClassObject, ClassRef}, |
16 | | - zval::{FromZval, IntoZval, Zval}, |
17 | | - }, |
18 | | - }, |
19 | | - php_class, |
20 | | - prelude::*, |
21 | | -}; |
22 | | - |
23 | | -#[php_class] |
24 | | -#[property(test = 0)] |
25 | | -#[property(another = "Hello world")] |
26 | | -#[derive(Default, Debug, Clone)] |
27 | | -pub struct Test { |
28 | | - pub test: String, |
29 | | -} |
30 | | - |
31 | | -#[php_function] |
32 | | -pub fn take_test(test: &Test) -> String { |
33 | | - test.test.clone() |
34 | | -} |
| 1 | +use ext_php_rs::prelude::*; |
35 | 2 |
|
36 | 3 | #[php_class] |
37 | | -#[derive(Default)] |
38 | | -struct PhpFuture { |
39 | | - then: Option<Callable<'static>>, |
40 | | -} |
41 | | - |
42 | | -#[php_impl] |
43 | | -impl PhpFuture { |
44 | | - pub fn then(&mut self, then: Callable<'static>) { |
45 | | - self.then = Some(then); |
46 | | - } |
47 | | - |
48 | | - pub fn now(&self) -> Result<(), PhpException> { |
49 | | - if let Some(then) = &self.then { |
50 | | - then.try_call(vec![&"Hello"]).unwrap(); |
51 | | - Ok(()) |
52 | | - } else { |
53 | | - Err(PhpException::default("No `then`".into())) |
| 4 | +struct TestClass { |
| 5 | + #[prop(rename = "Hello")] |
| 6 | + a: i32, |
| 7 | + #[prop] |
| 8 | + b: i64, |
| 9 | + #[prop] |
| 10 | + c: String, |
| 11 | +} |
| 12 | + |
| 13 | +impl Default for TestClass { |
| 14 | + fn default() -> Self { |
| 15 | + Self { |
| 16 | + a: 100, |
| 17 | + b: 123, |
| 18 | + c: "Hello, world!".into(), |
54 | 19 | } |
55 | 20 | } |
56 | | - |
57 | | - pub fn obj(&self) -> ClassObject<Test> { |
58 | | - ClassObject::new(Test { |
59 | | - test: "Hello world from class entry :)".into(), |
60 | | - }) |
61 | | - } |
62 | | - |
63 | | - pub fn return_self(&self) -> ClassRef<PhpFuture> { |
64 | | - ClassRef::from_ref(self).unwrap() |
65 | | - } |
66 | 21 | } |
67 | 22 |
|
68 | 23 | #[php_impl] |
69 | | -impl Test { |
70 | | - pub fn set_str(&mut self, str: String) { |
71 | | - self.test = str; |
| 24 | +impl TestClass { |
| 25 | + #[getter] |
| 26 | + fn get_test_name(&self) -> String { |
| 27 | + self.c.clone() |
72 | 28 | } |
73 | 29 |
|
74 | | - pub fn get_str(&self) -> String { |
75 | | - self.test.clone() |
| 30 | + #[setter] |
| 31 | + fn set_test_name(&mut self, c: String) { |
| 32 | + self.c = c; |
76 | 33 | } |
77 | 34 | } |
78 | 35 |
|
79 | | -#[php_function] |
80 | | -pub fn get_closure() -> Closure { |
81 | | - let mut x = 100; |
82 | | - Closure::wrap(Box::new(move || { |
83 | | - x += 5; |
84 | | - format!("x: {}", x) |
85 | | - }) as Box<dyn FnMut() -> String>) |
86 | | -} |
87 | | - |
88 | | -#[php_function] |
89 | | -pub fn fn_once() -> Closure { |
90 | | - let x = "Hello".to_string(); |
91 | | - Closure::wrap_once(Box::new(move || { |
92 | | - println!("val here: {}", &x); |
93 | | - x |
94 | | - }) as Box<dyn FnOnce() -> String>) |
95 | | -} |
96 | | - |
97 | | -#[php_function] |
98 | | -pub fn closure_get_string() -> Closure { |
99 | | - // Return a closure which takes two integers and returns a string |
100 | | - Closure::wrap(Box::new(|a, b| format!("A: {} B: {}", a, b)) as Box<dyn Fn(i32, i32) -> String>) |
101 | | -} |
102 | | - |
103 | | -#[php_function] |
104 | | -pub fn closure_count() -> Closure { |
105 | | - let mut count = 0i32; |
106 | | - |
107 | | - Closure::wrap(Box::new(move |a: i32| { |
108 | | - count += a; |
109 | | - count |
110 | | - }) as Box<dyn FnMut(i32) -> i32>) |
111 | | -} |
112 | | - |
113 | 36 | #[php_module] |
114 | 37 | pub fn module(module: ModuleBuilder) -> ModuleBuilder { |
115 | | - module.function( |
116 | | - FunctionBuilder::new("test_zval", test_zval) |
117 | | - .arg(Arg::new("test", DataType::Array)) |
118 | | - .build() |
119 | | - .unwrap(), |
120 | | - ) |
| 38 | + module |
121 | 39 | } |
0 commit comments