@@ -16,7 +16,8 @@ use phper::{
1616 types:: { ArgumentTypeHint , ReturnTypeHint } ,
1717 values:: ZVal ,
1818} ;
19- use std:: { collections:: HashMap , convert:: Infallible , rc:: Rc } ;
19+ use std:: { collections:: HashMap , convert:: Infallible } ;
20+ use std:: rc:: Rc ;
2021
2122pub fn integrate ( module : & mut Module ) {
2223 integrate_a ( module) ;
@@ -234,39 +235,50 @@ fn integrate_bar_extends_foo(module: &mut Module, foo_class: StateClass<Foo>) {
234235fn integrate_dependent_classes ( module : & mut Module ) {
235236 const A_CLS : & str = r"IntegrationTest\Dependency\A" ;
236237 const B_CLS : & str = r"IntegrationTest\Dependency\B" ;
238+ // Build both class entities
239+ let mut a_cls = ClassEntity :: new ( A_CLS ) ;
240+ let mut b_cls = ClassEntity :: new ( B_CLS ) ;
237241
238- let ( mut a_cls, a_ref) = define_class_stub ! ( A_CLS , a_ref) ;
239- let ( mut b_cls, b_ref) = define_class_stub ! ( B_CLS , b_ref) ;
242+ // Placeholder, will clone StateClass into closures after registration
243+ let a_cls_ref = std:: rc:: Rc :: new ( std:: cell:: RefCell :: new ( None ) ) ;
244+ let b_cls_ref = std:: rc:: Rc :: new ( std:: cell:: RefCell :: new ( None ) ) ;
240245
241246 {
242- let b_ref = Rc :: clone ( & b_ref) ;
243- a_cls
244- . add_static_method ( "createB" , Visibility :: Public , move |_| {
245- let borrow = b_ref. borrow ( ) ;
246- let b_state: & StateClass < ( ) > = borrow. as_ref ( ) . unwrap ( ) ;
247- Ok :: < _ , phper:: Error > ( b_state. init_object ( ) ?)
248- } )
249- . return_type ( ReturnType :: new ( ReturnTypeHint :: ClassEntry ( B_CLS . into ( ) ) ) ) ;
247+ let b_cls_ref = Rc :: clone ( & b_cls_ref) ;
248+ a_cls. add_static_method ( "createB" , Visibility :: Public , move |_| {
249+ let borrow = b_cls_ref. borrow ( ) ;
250+ let b_state: & StateClass < ( ) > = borrow
251+ . as_ref ( )
252+ . expect ( "B class not registered" ) ;
253+ let obj = b_state. init_object ( ) ?;
254+ Ok :: < _ , phper:: Error > ( obj)
255+ } )
256+ . return_type ( ReturnType :: new ( ReturnTypeHint :: ClassEntry ( String :: from ( B_CLS ) ) ) ) ;
250257 }
251258
252259 {
253- let a_ref = Rc :: clone ( & a_ref) ;
254- b_cls
255- . add_static_method ( "createA" , Visibility :: Public , move |_| {
256- let borrow = a_ref. borrow ( ) ;
257- let a_state: & StateClass < ( ) > = borrow. as_ref ( ) . unwrap ( ) ;
258- Ok :: < _ , phper:: Error > ( a_state. init_object ( ) ?)
259- } )
260- . return_type ( ReturnType :: new ( ReturnTypeHint :: ClassEntry ( A_CLS . into ( ) ) ) ) ;
260+ let a_cls_ref = Rc :: clone ( & a_cls_ref) ;
261+ b_cls. add_static_method ( "createA" , Visibility :: Public , move |_| {
262+ let borrow = a_cls_ref. borrow ( ) ;
263+ let a_state: & StateClass < ( ) > = borrow
264+ . as_ref ( )
265+ . expect ( "A class not registered" ) ;
266+ let obj = a_state. init_object ( ) ?;
267+ Ok :: < _ , phper:: Error > ( obj)
268+ } )
269+ . return_type ( ReturnType :: new ( ReturnTypeHint :: ClassEntry ( String :: from ( A_CLS ) ) ) ) ;
261270 }
262271
263- // Register both classes
272+
273+ // Register both classes and save the StateClass into shared RefCells
264274 let a_state = module. add_class ( a_cls) ;
265275 let b_state = module. add_class ( b_cls) ;
266- * a_ref. borrow_mut ( ) = Some ( a_state. clone ( ) ) ;
267- * b_ref. borrow_mut ( ) = Some ( b_state. clone ( ) ) ;
276+
277+ * a_cls_ref. borrow_mut ( ) = Some ( a_state) ;
278+ * b_cls_ref. borrow_mut ( ) = Some ( b_state) ;
268279}
269280
281+
270282#[ cfg( phper_major_version = "8" ) ]
271283fn integrate_stringable ( module : & mut Module ) {
272284 use phper:: { functions:: ReturnType , types:: ReturnTypeHint } ;
0 commit comments