2
2
3
3
namespace Dapr \Actors ;
4
4
5
+ use Dapr \Actors \Internal \Caches \CacheInterface ;
5
6
use Dapr \Actors \Internal \Caches \FileCache ;
6
- use Dapr \Actors \Internal \Caches \NoCache ;
7
- use Dapr \Deserialization \IDeserializer ;
7
+ use Dapr \Client \DaprClient ;
8
8
use Dapr \exceptions \DaprException ;
9
9
use Dapr \exceptions \Http \NotFound ;
10
10
use Dapr \exceptions \SaveStateFailure ;
11
- use DI \Container ;
12
11
use DI \DependencyException ;
13
12
use DI \FactoryInterface ;
14
13
use DI \NotFoundException ;
15
14
use Exception ;
16
- use Psr \Log \ LoggerInterface ;
15
+ use Psr \Container \ ContainerInterface ;
17
16
use ReflectionClass ;
18
17
use ReflectionException ;
19
18
use ReflectionNamedType ;
28
27
class ActorRuntime
29
28
{
30
29
public function __construct (
31
- protected LoggerInterface $ logger ,
32
30
protected ActorConfig $ actor_config ,
33
31
protected FactoryInterface $ factory ,
34
- protected Container $ container ,
35
- protected IDeserializer $ deserializer ,
32
+ protected ContainerInterface $ container ,
33
+ protected DaprClient $ client
36
34
) {
37
35
}
38
36
@@ -49,7 +47,7 @@ public function __construct(
49
47
public function do_method (IActor $ actor , string $ method , mixed $ arg ): mixed
50
48
{
51
49
$ reflection = new ReflectionClass ($ actor );
52
- $ method = $ reflection ->getMethod ($ method );
50
+ $ method = $ reflection ->getMethod ($ method );
53
51
if (empty ($ arg )) {
54
52
return $ method ->invoke ($ actor );
55
53
}
@@ -61,18 +59,18 @@ public function do_method(IActor $actor, string $method, mixed $arg): mixed
61
59
62
60
return $ method ->invokeArgs (
63
61
$ actor ,
64
- [$ parameter ->getName () => $ this ->deserializer ->detect_from_parameter ($ parameter , $ arg )]
62
+ [$ parameter ->getName () => $ this ->client -> deserializer ->detect_from_parameter ($ parameter , $ arg )]
65
63
);
66
64
}
67
65
68
66
public function deactivate_actor (IActor $ actor , string $ dapr_type ): void
69
67
{
70
- $ id = $ actor ->get_id ();
71
- $ activation_tracker = hash ('sha256 ' , $ dapr_type. $ id );
68
+ $ id = $ actor ->get_id ();
69
+ $ activation_tracker = hash ('sha256 ' , $ dapr_type . $ id );
72
70
$ activation_tracker = rtrim (
73
- sys_get_temp_dir (),
74
- DIRECTORY_SEPARATOR
75
- ). DIRECTORY_SEPARATOR . 'dapr_ ' . $ activation_tracker ;
71
+ sys_get_temp_dir (),
72
+ DIRECTORY_SEPARATOR
73
+ ) . DIRECTORY_SEPARATOR . 'dapr_ ' . $ activation_tracker ;
76
74
77
75
$ is_activated = file_exists ($ activation_tracker );
78
76
@@ -86,21 +84,20 @@ public function deactivate_actor(IActor $actor, string $dapr_type): void
86
84
/**
87
85
* Resolve an actor, then calls your callback with the actor before committing any state changes
88
86
*
89
- * @param string $dapr_type The dapr type to resolve
90
- * @param string $id The id of the actor to resolve
87
+ * @param ActorReference $reference
91
88
* @param callable $loan A callback that takes an IActor
92
89
*
93
90
* @return mixed The result of you callback
94
91
* @throws NotFound
95
92
* @throws SaveStateFailure
96
93
*/
97
- public function resolve_actor (string $ dapr_type , string $ id , callable $ loan ): mixed
94
+ public function resolve_actor (ActorReference $ reference , callable $ loan ): mixed
98
95
{
99
96
try {
100
- $ reflection = $ this ->locate_actor ($ dapr_type );
97
+ $ reflection = $ this ->locate_actor ($ reference );
101
98
$ this ->validate_actor ($ reflection );
102
- $ states = $ this ->get_states ($ reflection , $ dapr_type , $ id );
103
- $ actor = $ this ->get_actor ($ reflection , $ dapr_type , $ id , $ states );
99
+ $ states = $ this ->get_states ($ reflection , $ reference , $ this -> client );
100
+ $ actor = $ this ->get_actor ($ reflection , $ reference , $ states );
104
101
// @codeCoverageIgnoreStart
105
102
} catch (Exception $ exception ) {
106
103
throw new NotFound ('Actor could not be located ' , previous: $ exception );
@@ -123,18 +120,17 @@ public function resolve_actor(string $dapr_type, string $id, callable $loan): mi
123
120
/**
124
121
* Locates an actor implementation
125
122
*
126
- * @param string $dapr_type The dapr type to locate
127
- *
123
+ * @param ActorReference $reference The actor reference
128
124
* @return ReflectionClass
129
125
* @throws NotFound
130
126
* @throws ReflectionException
131
127
*/
132
- protected function locate_actor (string $ dapr_type ): ReflectionClass
128
+ protected function locate_actor (ActorReference $ reference ): ReflectionClass
133
129
{
134
- $ type = $ this ->actor_config ->get_actor_type_from_dapr_type ($ dapr_type );
135
- if ( ! class_exists ($ type )) {
130
+ $ type = $ this ->actor_config ->get_actor_type_from_dapr_type ($ reference -> get_actor_type () );
131
+ if (! class_exists ($ type )) {
136
132
// @codeCoverageIgnoreStart
137
- $ this ->logger ->critical ('Unable to locate an actor for {t} ' , ['t ' => $ type ]);
133
+ $ this ->client -> logger ->critical ('Unable to locate an actor for {t} ' , ['t ' => $ type ]);
138
134
throw new NotFound ();
139
135
// @codeCoverageIgnoreEnd
140
136
}
@@ -156,7 +152,7 @@ protected function validate_actor(ReflectionClass $reflection): bool
156
152
}
157
153
158
154
// @codeCoverageIgnoreStart
159
- $ this ->logger ->critical ('Actor does not implement the IActor interface ' );
155
+ $ this ->client -> logger ->critical ('Actor does not implement the IActor interface ' );
160
156
throw new NotFound ();
161
157
// @codeCoverageIgnoreEnd
162
158
}
@@ -165,18 +161,19 @@ protected function validate_actor(ReflectionClass $reflection): bool
165
161
* Retrieves an array of states for a located actor
166
162
*
167
163
* @param ReflectionClass $reflection The class we're loading states for
168
- * @param string $dapr_type The dapr type
169
- * @param string $id The id
170
- *
164
+ * @param ActorReference $reference
171
165
* @return array
172
- * @throws ReflectionException
173
166
* @throws DependencyException
174
167
* @throws NotFoundException
168
+ * @throws ReflectionException
175
169
*/
176
- protected function get_states (ReflectionClass $ reflection , string $ dapr_type , string $ id ): array
177
- {
170
+ protected function get_states (
171
+ ReflectionClass $ reflection ,
172
+ ActorReference $ reference ,
173
+ DaprClient $ client
174
+ ): array {
178
175
$ constructor = $ reflection ->getMethod ('__construct ' );
179
- $ states = [];
176
+ $ states = [];
180
177
foreach ($ constructor ->getParameters () as $ parameter ) {
181
178
$ type = $ parameter ->getType ();
182
179
if ($ type instanceof ReflectionNamedType) {
@@ -185,10 +182,16 @@ protected function get_states(ReflectionClass $reflection, string $dapr_type, st
185
182
$ reflected_type = new ReflectionClass ($ type_name );
186
183
if ($ reflected_type ->isSubclassOf (ActorState::class)) {
187
184
$ state = $ this ->container ->make ($ type_name );
188
- $ this ->begin_transaction ($ state , $ reflected_type , $ dapr_type , $ id );
185
+ $ this ->begin_transaction (
186
+ $ state ,
187
+ $ reflected_type ,
188
+ $ reference ,
189
+ $ client ,
190
+ $ this ->get_cache_for_actor ($ reference , $ type_name )
191
+ );
189
192
190
193
$ states [$ parameter ->name ] = $ state ;
191
- $ this ->logger ?->debug('Found state {t} ' , ['t ' => $ type_name ]);
194
+ $ this ->client -> logger ?->debug('Found state {t} ' , ['t ' => $ type_name ]);
192
195
}
193
196
}
194
197
}
@@ -211,24 +214,31 @@ protected function get_states(ReflectionClass $reflection, string $dapr_type, st
211
214
protected function begin_transaction (
212
215
ActorState $ state ,
213
216
ReflectionClass $ reflected_type ,
214
- string $ dapr_type ,
215
- string $ actor_id ,
217
+ ActorReference $ reference ,
218
+ DaprClient $ client ,
219
+ CacheInterface $ cache ,
216
220
?ReflectionClass $ original = null
217
221
): void {
218
222
if ($ reflected_type ->name !== ActorState::class) {
219
223
$ this ->begin_transaction (
220
224
$ state ,
221
225
$ reflected_type ->getParentClass (),
222
- $ dapr_type ,
223
- $ actor_id ,
226
+ $ reference ,
227
+ $ client ,
228
+ $ cache ,
224
229
$ original ?? $ reflected_type
225
230
);
226
231
227
232
return ;
228
233
}
229
234
$ begin_transaction = $ reflected_type ->getMethod ('begin_transaction ' );
230
235
$ begin_transaction ->setAccessible (true );
231
- $ begin_transaction ->invoke ($ state , $ dapr_type , $ actor_id );
236
+ $ begin_transaction ->invoke ($ state , $ reference , $ client , $ cache );
237
+ }
238
+
239
+ protected function get_cache_for_actor (ActorReference $ reference , string $ type_name ): CacheInterface
240
+ {
241
+ return $ this ->factory ->make (CacheInterface::class, ['reference ' => $ reference , 'state_name ' => $ type_name ]);
232
242
}
233
243
234
244
/**
@@ -243,23 +253,23 @@ protected function begin_transaction(
243
253
* @throws DependencyException
244
254
* @throws NotFoundException
245
255
*/
246
- protected function get_actor (ReflectionClass $ reflection , string $ dapr_type , string $ id , array $ states ): IActor
256
+ protected function get_actor (ReflectionClass $ reflection , ActorReference $ reference , array $ states ): IActor
247
257
{
248
- $ states ['id ' ] = $ id ;
249
- $ this ->container ->set (ActorReference::class, new ActorReference ( $ id , $ dapr_type ) );
250
- $ actor = $ this ->factory ->make ($ reflection ->getName (), $ states );
251
- $ activation_tracker = hash ('sha256 ' , $ dapr_type . $ id );
258
+ $ states ['id ' ] = $ reference -> get_actor_id () ;
259
+ $ this ->container ->set (ActorReference::class, $ reference );
260
+ $ actor = $ this ->factory ->make ($ reflection ->getName (), $ states );
261
+ $ activation_tracker = hash ('sha256 ' , $ reference -> get_actor_type () . $ reference -> get_actor_id () );
252
262
$ activation_tracker = rtrim (
253
- sys_get_temp_dir (),
254
- DIRECTORY_SEPARATOR
255
- ). DIRECTORY_SEPARATOR . 'dapr_ ' . $ activation_tracker ;
263
+ sys_get_temp_dir (),
264
+ DIRECTORY_SEPARATOR
265
+ ) . DIRECTORY_SEPARATOR . 'dapr_ ' . $ activation_tracker ;
256
266
257
267
$ is_activated = file_exists ($ activation_tracker );
258
268
259
- if ( ! $ is_activated ) {
260
- $ this ->logger ?->info(
269
+ if (! $ is_activated ) {
270
+ $ this ->client -> logger ?->info(
261
271
'Activating {type}||{id} ' ,
262
- ['type ' => $ dapr_type , 'id ' => $ id ]
272
+ ['type ' => $ reference -> get_actor_type () , 'id ' => $ reference -> get_actor_id () ]
263
273
);
264
274
touch ($ activation_tracker );
265
275
$ actor ->on_activation ();
0 commit comments