@@ -44,7 +44,9 @@ public function __construct(
4444 protected int |string |null $ id = null ,
4545 protected bool $ singleton = false ,
4646 protected ?Generator $ faker = null ,
47- protected string $ initial_event = VerbsStateInitialized::class,
47+ protected ?string $ initial_event = null ,
48+ protected Collection $ makeCallbacks = new Collection (),
49+ protected Collection $ createCallbacks = new Collection (),
4850 ) {
4951 }
5052
@@ -58,6 +60,20 @@ public function configure(): void
5860 //
5961 }
6062
63+ public function afterMaking (Closure $ callback ): static
64+ {
65+ $ this ->makeCallbacks ->push ($ callback );
66+
67+ return $ this ;
68+ }
69+
70+ public function afterCreating (Closure $ callback ): static
71+ {
72+ $ this ->createCallbacks ->push ($ callback );
73+
74+ return $ this ;
75+ }
76+
6177 /** @return static<TStateType> */
6278 public function state (callable |array $ data ): static
6379 {
@@ -121,12 +137,21 @@ public function create(array $data = [], Bits|UuidInterface|AbstractUid|int|stri
121137 /** @return TStateType */
122138 protected function createState (): State
123139 {
124- $ initialized = $ this ->initial_event ::fire (
125- state_id: $ this ->id ?? Id::make (),
126- state_class: $ this ->state_class ,
127- state_data: $ this ->getRawData (),
128- singleton: $ this ->singleton ,
129- );
140+ $ this ->makeCallbacks ->each (fn (Closure $ callback ) => $ callback ($ this ));
141+
142+ $ initialized = $ this ->initial_event
143+ ? $ this ->initial_event ::fire (
144+ ...$ this ->getRawData (),
145+ id: $ this ->id ?? Id::make (),
146+ )
147+ : VerbsStateInitialized::fire (
148+ state_id: $ this ->id ?? Id::make (),
149+ state_class: $ this ->state_class ,
150+ state_data: $ this ->getRawData (),
151+ singleton: $ this ->singleton ,
152+ );
153+
154+ $ this ->createCallbacks ->each (fn (Closure $ callback ) => $ callback ($ initialized ));
130155
131156 return $ initialized ->state ($ this ->state_class );
132157 }
0 commit comments