1212use DtmClient \Api \ApiInterface ;
1313use DtmClient \Constants \Protocol ;
1414use DtmClient \Constants \TransType ;
15+ use DtmClient \Context \Context ;
1516use DtmClient \Exception \UnsupportedException ;
1617use Google \Protobuf \Internal \Message ;
1718
1819class Saga extends AbstractTransaction
1920{
20- protected array $ orders = [] ;
21+ protected const CONCURRENT = self ::class . ' .concurrent ' ;
2122
22- protected bool $ concurrent = false ;
23+ protected const ORDERS = self ::class . ' .orders ' ;
2324
2425 protected ApiInterface $ api ;
2526
@@ -33,6 +34,8 @@ public function init(?string $gid = null)
3334 if ($ gid === null ) {
3435 $ gid = $ this ->generateGid ();
3536 }
37+ Context::set (static ::CONCURRENT , false );
38+ Context::set (static ::ORDERS , []);
3639 TransContext::init ($ gid , TransType::SAGA , '' );
3740 }
3841
@@ -60,13 +63,15 @@ public function add(string $action, string $compensate, array|object $payload):
6063
6164 public function addBranchOrder (int $ branch , array $ preBranches ): static
6265 {
63- $ this ->orders [$ branch ] = $ preBranches ;
66+ $ orders = Context::get (static ::ORDERS , []);
67+ $ orders [$ branch ] = $ preBranches ;
68+ Context::set (static ::ORDERS , $ orders );
6469 return $ this ;
6570 }
6671
6772 public function enableConcurrent ()
6873 {
69- $ this -> concurrent = true ;
74+ Context:: set ( static :: CONCURRENT , true ) ;
7075 }
7176
7277 public function submit ()
@@ -77,10 +82,10 @@ public function submit()
7782
7883 public function addConcurrentContext ()
7984 {
80- if ($ this -> concurrent ) {
85+ if (Context:: get ( static :: CONCURRENT , false ) ) {
8186 TransContext::setCustomData (json_encode ([
82- 'concurrent ' => $ this -> concurrent ,
83- 'orders ' => $ this -> orders ?: null ,
87+ 'concurrent ' => Context:: get ( static :: CONCURRENT ) ,
88+ 'orders ' => Context:: get ( static :: ORDERS ) ?: null ,
8489 ]));
8590 }
8691 }
0 commit comments