danielpanzella/platformsh-client-php-bundle
Symfony bundle that registers the Platform.sh PHP API client as a service. Configure API token and token type in YAML, then fetch the PlatformClient from the container or inject it via Symfony 3.3+ autowiring.
Install the Bundle Add the package via Composer:
composer require danielpanzella/platformsh-client-php-bundle
Enable the bundle in config/bundles.php:
return [
// ...
DanielPanzella\PlatformshClientBundle\PlatformshClientBundle::class => ['all' => true],
];
Configure API Credentials
Add your Platform.sh API token to config/packages/platform_client.yaml:
platform_client:
api_token: "%env(PLATFORM_API_TOKEN)%" # Use env vars for security
api_token_type: "project" # or "user" if applicable
First Use Case: Fetch Project Info Inject the client in a service/controller:
use Platformsh\Client\PlatformClient;
class MyService {
public function __construct(PlatformClient $platformClient) {
$this->platformClient = $platformClient;
}
public function getProjectDetails() {
$project = $this->platformClient->getProject();
return $project->getName(); // e.g., "my-project"
}
}
Environment Management List environments and their URLs:
$environments = $this->platformClient->getEnvironments();
foreach ($environments as $env) {
echo $env->getId() . ": " . $env->getUrl() . "\n";
}
Branch/Environment Sync Trigger a build for a specific branch:
$branch = $this->platformClient->getBranch('main');
$branch->triggerBuild();
Service Communication Get service details (e.g., database credentials):
$services = $this->platformClient->getServices();
$dbService = $services->getService('database');
$credentials = $dbService->getCredentials();
PLATFORM_ENVIRONMENT to conditionally interact with the API (e.g., skip in local dev).$cacheKey = 'platformsh_project_' . $projectId;
$project = $this->cache->get($cacheKey) ?: $this->platformClient->getProject($projectId);
$this->cache->set($cacheKey, $project, 3600); // Cache for 1 hour
Token Permissions
Deprecated Methods
PlatformClient methods may not exist in newer platformsh-client-php versions.getProject() → getProjectById()).Symfony Version Mismatch
autowire: true in services.yaml for the PlatformClient.services.yaml).Rate Limiting
Platformsh\Client\Exception\RateLimitExceededException gracefully.platform_client:
debug: true # Logs API requests/responses to `var/log/platformsh.log`
try {
$project = $this->platformClient->getProject();
} catch (\Platformsh\Client\Exception\ApiException $e) {
// Log $e->getResponse()->getBody()
}
Custom API Clients Extend the bundle to support multiple projects/clients:
# config/packages/platform_client.yaml
platform_client:
clients:
project1:
api_token: "%env(PLATFORM_PROJECT1_TOKEN)%"
api_token_type: "project"
project2:
api_token: "%env(PLATFORM_PROJECT2_TOKEN)%"
Then inject Platformsh\Client\PlatformClientInterface and resolve specific clients via the container.
Event Listeners Listen for environment events (e.g., build completion):
// src/EventListener/PlatformshListener.php
class PlatformshListener {
public function onKernelRequest(GetResponseEvent $event) {
if ($event->isMasterRequest()) {
$this->platformClient->getWebhook()->listen('build.finished');
}
}
}
Testing
Mock the PlatformClient in tests:
$mockClient = $this->createMock(PlatformClient::class);
$mockClient->method('getProject')->willReturn(new Project('my-project'));
$this->container->set(PlatformClient::class, $mockClient);
How can I help you explore Laravel packages today?