Skip to content

Commit 643d60b

Browse files
Start updating the readme (#114)
* Start updating the readme * Update readme with more details * Remove older dapr versions from tests
1 parent bf50ad8 commit 643d60b

File tree

8 files changed

+175
-34
lines changed

8 files changed

+175
-34
lines changed

.github/workflows/php.yml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ jobs:
204204
timeout-minutes: 5
205205
strategy:
206206
matrix:
207-
dapr-version: [ 1.3.0-rc.4, 1.2.0, 1.2.1, 1.2.2 ]
207+
dapr-version: [ 1.3.0, 1.2.1, 1.2.2 ]
208208
steps:
209209
- name: Download Caddy Image
210210
uses: actions/download-artifact@v2
@@ -248,8 +248,7 @@ jobs:
248248
strategy:
249249
matrix:
250250
dapr-version:
251-
- 1.3.0-rc.4
252-
- 1.2.0
251+
- 1.3.0
253252
- 1.2.1
254253
- 1.2.2
255254
example:

readme.md

Lines changed: 121 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,62 @@ Add the library to your `composer.json`:
88

99
> composer require dapr/php-sdk
1010
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+
```
1267

1368
# Accessing Secrets
1469

@@ -17,19 +72,25 @@ You can access secrets easily:
1772
```php
1873
<?php
1974

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');
2880
```
2981

3082
# Accessing State
3183

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
3394

3495
```php
3596
<?php
@@ -47,9 +108,9 @@ class MyState {
47108
public array $complex_type;
48109

49110
/**
50-
* @var Exception
111+
* @var SomeObject
51112
*/
52-
public Exception $object_type;
113+
public SomeObject $object_type;
53114

54115
/**
55116
* @var int
@@ -82,7 +143,19 @@ $app->start();
82143
## Transactional State
83144

84145
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
86159

87160
```php
88161
#[\Dapr\State\Attributes\StateStore('statestore', \Dapr\consistency\StrongFirstWrite::class)]
@@ -180,9 +253,21 @@ use Dapr\Actors\ActorProxy;
180253
$app->start();
181254
```
182255

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+
183267
## Actor Limitations
184268

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/).
186271
2. By design, static functions don't work.
187272
3. There's overhead cost in calling "getter" functions.
188273

@@ -195,18 +280,24 @@ implemented in this SDK.
195280

196281
## Publishing
197282

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:
199284

200285
```php
201286
<?php
202287
$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']);
206291
});
207292
$app->start();
208293
```
209294

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+
210301
## Subscribing
211302

212303
```php
@@ -238,13 +329,18 @@ $app->get('/', function(\Dapr\Serialization\ISerializer $serializer) {
238329
$app->start();
239330
```
240331

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+
```
242340

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
246342

247-
# Tests
343+
## Tests
248344

249345
Simply run `composer test` to run the unit tests. You can lint using `composer lint`.
250346

@@ -255,15 +351,11 @@ You need [`docker-compose`](https://docs.docker.com/compose/gettingstarted/) and
255351
Build and start the environment, then run the integration tests and clean up.
256352

257353
```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
264355
```
265356

266357
You should see output like:
358+
267359
```json
268360
{
269361
"/test/actors": {

src/lib/Client/StateTransactionRequest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,28 @@
1010
*/
1111
abstract class StateTransactionRequest
1212
{
13+
public static function upsert(
14+
string $key,
15+
array|string|null $value,
16+
string $etag = '',
17+
array $metadata = [],
18+
Consistency|null $consistency = null
19+
): UpsertTransactionRequest {
20+
return new UpsertTransactionRequest(
21+
key: $key,
22+
value: $value,
23+
etag: $etag,
24+
metadata: $metadata,
25+
consistency: $consistency
26+
);
27+
}
28+
29+
public static function delete(
30+
string $key,
31+
string $etag = '',
32+
array $metadata = [],
33+
Consistency|null $consistency = null
34+
): DeleteTransactionRequest {
35+
return new DeleteTransactionRequest(key: $key, etag: $etag, metadata: $metadata, consistency: $consistency);
36+
}
1337
}

src/lib/consistency/Consistency.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ abstract class Consistency
2121
*
2222
* @return string
2323
*/
24-
public abstract function get_consistency(): string;
24+
abstract public function get_consistency(): string;
2525

26-
public abstract function get_concurrency(): string;
26+
abstract public function get_concurrency(): string;
27+
28+
abstract public static function instance(): Consistency;
2729
}

src/lib/consistency/EventualFirstWrite.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@
1010
*/
1111
class EventualFirstWrite extends Consistency
1212
{
13+
public static function instance(): EventualFirstWrite
14+
{
15+
static $instance;
16+
return $instance ??= new EventualFirstWrite();
17+
}
18+
1319
public function get_consistency(): string
1420
{
1521
return self::EVENTUAL;

src/lib/consistency/EventualLastWrite.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@
99
*/
1010
class EventualLastWrite extends Consistency
1111
{
12+
public static function instance(): EventualLastWrite
13+
{
14+
static $instance;
15+
return $instance ??= new EventualLastWrite();
16+
}
17+
1218
public function get_consistency(): string
1319
{
1420
return self::EVENTUAL;

src/lib/consistency/StrongFirstWrite.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@
99
*/
1010
class StrongFirstWrite extends Consistency
1111
{
12+
public static function instance(): StrongFirstWrite
13+
{
14+
static $instance;
15+
return $instance ??= new StrongFirstWrite();
16+
}
17+
1218
public function get_consistency(): string
1319
{
1420
return self::STRONG;

src/lib/consistency/StrongLastWrite.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@
99
*/
1010
class StrongLastWrite extends Consistency
1111
{
12+
public static function instance(): StrongLastWrite
13+
{
14+
static $instance;
15+
return $instance ??= new StrongLastWrite();
16+
}
17+
1218
public function get_consistency(): string
1319
{
1420
return self::STRONG;

0 commit comments

Comments
 (0)