Skip to content

Commit 5f49d90

Browse files
committed
Update the example in the README
1 parent 281703f commit 5f49d90

File tree

1 file changed

+39
-18
lines changed

1 file changed

+39
-18
lines changed

README.md

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,32 +23,53 @@ composer require platformsh/client
2323

2424
Example:
2525
```php
26-
26+
use Platformsh\Client\Connection\Connector;
2727
use Platformsh\Client\PlatformClient;
2828

29+
// Set up configuration.
30+
$connector = new Connector([
31+
'api_url' => 'https://api.platform.sh',
32+
'accounts' => 'https://api.platform.sh/',
33+
'centralized_permissions_enabled' => true,
34+
]);
35+
2936
// Initialize the client.
3037
$client = new PlatformClient();
3138

3239
// Set the API token to use.
3340
//
3441
// N.B. you must keep your API token(s) safe!
35-
$client->getConnector()->setApiToken($myToken, 'exchange');
36-
37-
// Get the user's first project.
38-
$projects = $client->getProjects();
39-
$project = reset($projects);
40-
if ($project) {
41-
// Get the default (production) environment.
42-
$environment = $project->getEnvironment($project->default_branch);
43-
44-
// Create a new branch.
45-
$activity = $environment->branch('Sprint 1', 'sprint-1');
46-
47-
// Wait for the activity to complete.
48-
$activity->wait();
49-
50-
// Get the new branch.
51-
$sprint1 = $project->getEnvironment('sprint-1');
42+
$client->getConnector()->setApiToken('test', 'exchange');
43+
44+
// Get the user's project list.
45+
$projects = $client->getMyProjects();
46+
foreach ($client->getMyProjects() as $projectInfo) {
47+
// Get the full project object.
48+
$project = $client->getProject($projectInfo->id);
49+
if ($project) {
50+
// Get the default (production) environment.
51+
$environment = $project->getEnvironment($project->default_branch);
52+
53+
// Create a new environment.
54+
$result = $environment->runOperation('branch', body: ['name' => 'sprint-1', 'title' => 'Sprint 1']);
55+
56+
// Wait for the operation to complete.
57+
$activities = $result->getActivities();
58+
while (count($activities) > 0) {
59+
foreach ($activities as $key => $activity) {
60+
if ($activity->isComplete() || $activity->state === \Platformsh\Client\Model\Activity::STATE_CANCELLED) {
61+
unset($activities[$key]);
62+
} else {
63+
echo "Waiting for the activity: {$activity->getDescription()}\n";
64+
$activity->wait(function () { echo '.'; });
65+
echo "\n";
66+
}
67+
}
68+
}
69+
70+
// Get the new branch.
71+
$sprint1 = $project->getEnvironment('sprint-1');
72+
}
5273
}
5374
```
5475

0 commit comments

Comments
 (0)