@@ -8,7 +8,62 @@ Add the library to your `composer.json`:
8
8
9
9
> composer require dapr/php-sdk
10
10
11
- Some basic documentation is below, more documentation can be found [ in the docs] ( https://docs.dapr.io/developing-applications/sdks/php/ ) ;
11
+ Some basic documentation is below, more documentation can be
12
+ found [ in the docs] ( https://docs.dapr.io/developing-applications/sdks/php/ ) ;
13
+
14
+ # Migrating to 1.2
15
+
16
+ In preparation for gRPC support in this SDK, there's now a new DaprClient in ` \Dapr\Client\DaprClient ` . Please update
17
+ your code to use the new client.
18
+
19
+ There shouldn't be many changes to your code to upgrade to 1.2+ from a prior version. Namely, there are some
20
+ deprecations:
21
+
22
+ ## Deprecations
23
+
24
+ The following have been deprecated and will be removed in 1.4+.
25
+
26
+ ### \Dapr\SecretManager has been deprecated
27
+
28
+ Simply use the new client instead.
29
+
30
+ ### \Dapr\Client has been deprecated
31
+
32
+ Simply use the new client: ` \Dapr\Client\DaprClient ` .
33
+
34
+ ### \Dapr\PubSub\Publish has been deprecated
35
+
36
+ Simply instantiate ` \Dapr\PubSub\Topic ` directly or use the new client directly.
37
+
38
+ ## Fallbacks and Upgrades
39
+
40
+ ### \Dapr\State\StateManager
41
+
42
+ This class has been upgraded to use the new client. It shouldn't require any changes to your code, however, the old
43
+ behavior can be utilized with ` \Dapr\State\StateManagerOld ` .
44
+
45
+ ### \Dapr\State\TransactionalState
46
+
47
+ This class has been upgrade to use the new client. It shouldn't require any changes to your code, however, the old
48
+ behavior can be utilized with ` \Dapr\State\TransactionalStateOld ` .
49
+
50
+ # Creating a Dapr Client
51
+
52
+ ``` php
53
+ $client = \Dapr\Client\DaprClient::clientBuilder()->build();
54
+ ```
55
+
56
+ ## Using the Middleware
57
+
58
+ The ` App ` object also implements a PSR-15 compatible middleware which implements the actor routes and subscribe routes
59
+ for you.
60
+
61
+ ``` php
62
+ $app = \Dapr\App::create(configure: [
63
+ // custom configuration
64
+ ]);
65
+ use_middleware($app);
66
+ ```
12
67
13
68
# Accessing Secrets
14
69
@@ -17,19 +72,25 @@ You can access secrets easily:
17
72
``` php
18
73
<?php
19
74
20
- $app = Dapr\App::create();
21
- $app->get('/a-secret/{name}', function(string $name, \Dapr\SecretManager $secretManager) {
22
- return $secretManager->retrieve(secret_store: 'my-secret-store', name: $name);
23
- });
24
- $app->get('/a-secret', function(\Dapr\SecretManager $secretManager) {
25
- return $secretManager->all(secret_store: 'my-secret-store');
26
- });
27
- $app->start();
75
+ // retrieve a single secret
76
+ $client->getSecret(storeName: 'kubernetes', key: 'test');
77
+
78
+ // retrieve all secrets
79
+ $client->getBulkSecret(storeName: 'kubernetes');
28
80
```
29
81
30
82
# Accessing State
31
83
32
- State is just Plain Old PHP Objects (POPO's) with an attribute:
84
+ There are several ways to access state. You can access state directly via the client or abstract access via an object.
85
+
86
+ ## Accessing State Directly
87
+
88
+ ``` php
89
+ ['value' => $value, 'etag' => $etag] = $client->getStateAndEtag(storeName: 'statestore', key: 'key', asType: SomeClass::class, consistency: \Dapr\consistency\EventualLastWrite::instance());
90
+ $value = $client->getState(storeName: 'statestore', key: 'key', asType: 'string',consistency: \Dapr\consistency\StrongFirstWrite::instance())
91
+ ```
92
+
93
+ ## Abstract via Object
33
94
34
95
``` php
35
96
<?php
@@ -47,9 +108,9 @@ class MyState {
47
108
public array $complex_type;
48
109
49
110
/**
50
- * @var Exception
111
+ * @var SomeObject
51
112
*/
52
- public Exception $object_type;
113
+ public SomeObject $object_type;
53
114
54
115
/**
55
116
* @var int
@@ -82,7 +143,19 @@ $app->start();
82
143
## Transactional State
83
144
84
145
You can also use transactional state to interact with state objects by extending ` TransactionalState ` with our state
85
- objects.
146
+ objects or commit transactions directly.
147
+
148
+ ### Directly with the client
149
+
150
+ ``` php
151
+ $transaction = [
152
+ \Dapr\Client\StateTransactionRequest::upsert(key: 'key', value: $client->serializer->as_json($new_value)),
153
+ \Dapr\Client\StateTransactionRequest::delete(key: 'key');
154
+ ];
155
+ $client->executeStateTransaction(storeName: 'statestore', operations: $transaction);
156
+ ```
157
+
158
+ ### Abstracted via an Object
86
159
87
160
``` php
88
161
#[\Dapr\State\Attributes\StateStore('statestore', \Dapr\consistency\StrongFirstWrite::class)]
@@ -180,9 +253,21 @@ use Dapr\Actors\ActorProxy;
180
253
$app->start();
181
254
```
182
255
256
+ You can also call an actor without an interface:
257
+
258
+ ``` php
259
+ $client->invokeActorMethod(
260
+ httpMethod: 'GET',
261
+ actor: new \Dapr\Actors\ActorReference(id: 'id', actor_type: 'Counter'),
262
+ method: 'increment',
263
+ parameter: 1
264
+ );
265
+ ```
266
+
183
267
## Actor Limitations
184
268
185
- 1 . There's no re-entrance to an actor, this can cause deadlocks if you're not careful.
269
+ 1 . There's no re-entrance to an actor, by default. You'll need to enable it in the ` ActorConfig `
270
+ and [ in Dapr] ( https://docs.dapr.io/operations/support/support-preview-features/ ) .
186
271
2 . By design, static functions don't work.
187
272
3 . There's overhead cost in calling "getter" functions.
188
273
@@ -195,18 +280,24 @@ implemented in this SDK.
195
280
196
281
## Publishing
197
282
198
- In order to publish an event, you just instantiate the ` Publish ` object with the ` FactoryInterface ` :
283
+ In order to publish an event, you just instantiate the ` Topic ` object:
199
284
200
285
``` php
201
286
<?php
202
287
$app = \Dapr\App::create();
203
- $app->get('/publish', function(\DI\FactoryInterface $factory ) {
204
- $publisher = $factory->make( \Dapr\PubSub\Publish::class, [ 'pubsub' => 'redis-pubsub'] );
205
- $publisher-> topic('my-topic') ->publish(['message' => 'arrive at dawn']);
288
+ $app->get('/publish', function(\Dapr\Client\DaprClient $client ) {
289
+ $topic = new \Dapr\PubSub\Topic(pubsub: 'pubsub', topic: 'topic', client: $client );
290
+ $topic->publish(['message' => 'arrive at dawn']);
206
291
});
207
292
$app->start();
208
293
```
209
294
295
+ or you can use the new client like:
296
+
297
+ ``` php
298
+ $client->publishEvent(pubsubName: 'pubsub', topicName: 'topic', data: ['message' => 'arrive at dawn'], contentType: 'application/json');
299
+ ```
300
+
210
301
## Subscribing
211
302
212
303
``` php
@@ -238,13 +329,18 @@ $app->get('/', function(\Dapr\Serialization\ISerializer $serializer) {
238
329
$app->start();
239
330
```
240
331
241
- # Development
332
+ ## Using the new client
333
+
334
+ ``` php
335
+ $client = \Dapr\Client\DaprClient::clientBuilder()
336
+ ->withDeserializationConfig($configuration)
337
+ ->withSerializationConfig($configuration)
338
+ ->build()
339
+ ```
242
340
243
- Simply run ` composer start ` on a machine where ` dapr init ` has already been run. This will start the daprd service on
244
- the current open terminal. Then navigate to [ http://localhost:9502/do_tests ] ( http://localhost:9502/do_tests ) to let the
245
- integration tests run.
341
+ # Development
246
342
247
- # Tests
343
+ ## Tests
248
344
249
345
Simply run ` composer test ` to run the unit tests. You can lint using ` composer lint ` .
250
346
@@ -255,15 +351,11 @@ You need [`docker-compose`](https://docs.docker.com/compose/gettingstarted/) and
255
351
Build and start the environment, then run the integration tests and clean up.
256
352
257
353
``` bash
258
- # clean up any existing environment
259
- docker-compose down -v
260
- # build and deploy the containers
261
- composer start
262
- # run and display the test rusults
263
- composer integration-tests | jq .
354
+ make
264
355
```
265
356
266
357
You should see output like:
358
+
267
359
``` json
268
360
{
269
361
"/test/actors" : {
0 commit comments