google/cloud-common-protos
Generated PHP Protocol Buffer classes shared across Google Cloud APIs (part of google-cloud-php). Install via Composer as google/cloud-common-protos to use stable, Apache-2.0 licensed common proto message types in your apps.
Install the Package
Add to composer.json:
"require": {
"google/cloud-common-protos": "^1.0"
}
Run:
composer require google/cloud-common-protos
Verify Installation Check autoloaded classes:
composer dump-autoload
Verify the Google\Cloud\Common\Protos namespace is available.
First Use Case: Audit Logs
Use the AuditLog protobuf type to structure log entries:
use Google\Cloud\Common\Protos\AuditLog;
$log = new AuditLog();
$log->setAuthenticationInfo(...);
$log->setServiceName('my-service');
Key Classes to Explore
Google\Cloud\Common\Protos\AuditLog (Audit logging)Google\Cloud\Common\Protos\Status (Error handling)Google\Cloud\Common\Protos\HttpRequest (HTTP metadata)Use generated classes as immutable DTOs for Google Cloud integrations:
use Google\Cloud\Common\Protos\Status;
$status = new Status();
$status->setCode(Status::CODE_INVALID_ARGUMENT);
$status->setMessage('Invalid request payload');
Pair with official SDKs (e.g., google/cloud-logging) for seamless protobuf handling:
use Google\Cloud\Logging\V2\LoggingClient;
use Google\Cloud\Common\Protos\AuditLog;
$logging = new LoggingClient();
$logEntry = $logging->entry('my-log', [
'protoPayload' => AuditLog::class,
'data' => $auditLog->serializeToJsonString()
]);
Extend protobuf types for domain-specific needs (e.g., adding Laravel metadata):
use Google\Cloud\Common\Protos\HttpRequest;
$request = new HttpRequest();
$request->setRequestUrl('/api/endpoint');
$request->setUserAgent('Laravel/' . app()->version());
Validate protobuf payloads before API calls:
use Google\Cloud\Common\Protos\Status;
$status = new Status();
if ($status->getCode() !== Status::CODE_OK) {
throw new \RuntimeException($status->getMessage());
}
Bind protobuf factories for dependency injection:
// app/Providers/GoogleCloudServiceProvider.php
public function register()
{
$this->app->bind(AuditLog::class, function () {
return new AuditLog();
});
}
Capture Events
Use AuditLog to log sensitive actions (e.g., user updates):
$auditLog = new AuditLog();
$auditLog->setServiceName('laravel-auth');
$auditLog->setMethodName('updateUser');
Serialize for Storage Convert to JSON for Google Cloud Logging:
$serialized = $auditLog->serializeToJsonString();
Send to GCP
Integrate with google/cloud-logging:
$logging->writeLogEntry($serialized);
Standardize Errors
Use Status for consistent error responses:
$status = new Status();
$status->setCode(Status::CODE_PERMISSION_DENIED);
Map to Laravel Exceptions Convert protobuf errors to Laravel exceptions:
throw new \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException(
$status->getMessage()
);
Define Protobuf Services Extend protobuf definitions for custom gRPC endpoints:
service MyService {
rpc LogEvent (AuditLog) returns (Status);
}
Generate PHP Clients
Use protoc to generate gRPC clients:
protoc --php_out=. --grpc_out=. my_service.proto
Integrate with Laravel Bind gRPC clients in Laravel’s DI container:
$this->app->singleton(MyServiceClient::class, function () {
return new MyServiceClient(...);
});
Use serializeToJsonString() for API payloads:
$response = [
'status' => $status->serializeToJsonString(),
'data' => $data
];
Validate protobuf fields with Laravel’s validator:
$validator = Validator::make($request->all(), [
'protoPayload' => 'required|string',
]);
Cache frequently used protobuf objects (e.g., Status for errors):
$statusCache = Cache::remember('error_status', 3600, function () {
return new Status();
});
Use PHPUnit to test protobuf serialization:
public function testAuditLogSerialization()
{
$auditLog = new AuditLog();
$serialized = $auditLog->serializeToJsonString();
$this->assertJson($serialized);
}
Serialize protobuf objects for queue jobs:
Queue::push(function () use ($auditLog) {
$serialized = $auditLog->serializeToJsonString();
// Process $serialized
});
google/protobuf, which requires PHP extensions (protobuf).php.ini includes:
extension=protobuf.so
php -m | grep protobuf
mergeFrom() or setter methods:
$newLog = clone $auditLog;
$newLog->setMethodName('new_method');
google/protobuf versions (e.g., ^3.0 vs ^4.0).composer.json:
"google/protobuf": "^3.20.0"
serializeToJsonString() may omit default values.$status->setCode(Status::CODE_OK);
$status->setMessage('OK');
ReservationResourceUsage) are deprecated.isDeprecated() checks or avoid deprecated fields:
if ($log->hasDeprecatedFields()) {
Log::warning('Deprecated protobuf fields detected');
}
InvalidProtocolBufferException during deserialization.try {
$status = Status::deserialize($json);
} catch (\InvalidArgumentException $e) {
Log::error('Protobuf deserialization failed: ' . $e->getMessage());
}
$this->assertNotNull($status->getCode(), 'Status code must be set');
Class 'Google\Protobuf\Internal\Message' not found.protobuf extension is enabled:
php -i | grep protobuf
How can I help you explore Laravel packages today?