Skip to content

Commit

Permalink
Update the example in the README
Browse files Browse the repository at this point in the history
  • Loading branch information
pjcdawkins committed Dec 5, 2024
1 parent 281703f commit ce54a80
Showing 1 changed file with 28 additions and 10 deletions.
38 changes: 28 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,29 +23,47 @@ composer require platformsh/client

Example:
```php

use Platformsh\Client\Connection\Connector;
use Platformsh\Client\PlatformClient;

// Set up configuration.
$connector = new Connector([
'api_url' => 'https://api.platform.sh',
'accounts' => 'https://api.platform.sh/',
'centralized_permissions_enabled' => true,
]);

// Initialize the client.
$client = new PlatformClient();

// Set the API token to use.
//
// N.B. you must keep your API token(s) safe!
$client->getConnector()->setApiToken($myToken, 'exchange');
$client->getConnector()->setApiToken('test', 'exchange');

// Get the user's first project.
$projects = $client->getProjects();
$project = reset($projects);
// Get a project.
$projects = $client->getMyProjects();
$project = $client->getProject('my_project_id');
if ($project) {
// Get the default (production) environment.
$environment = $project->getEnvironment($project->default_branch);

// Create a new branch.
$activity = $environment->branch('Sprint 1', 'sprint-1');

// Wait for the activity to complete.
$activity->wait();
// Create a new environment.
$result = $environment->runOperation('branch', body: ['name' => 'sprint-1', 'title' => 'Sprint 1']);

// Wait for the operation to complete.
$activities = $result->getActivities();
while (count($activities) > 0) {
foreach ($activities as $key => $activity) {
if ($activity->isComplete() || $activity->state === \Platformsh\Client\Model\Activity::STATE_CANCELLED) {
unset($activities[$key]);
} else {
echo "Waiting for the activity: {$activity->getDescription()}\n";
$activity->wait(function () { echo '.'; });
echo "\n";
}
}
}

// Get the new branch.
$sprint1 = $project->getEnvironment('sprint-1');
Expand Down

0 comments on commit ce54a80

Please sign in to comment.