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.
Installation Add the package via Composer:
composer require google/cloud-common-protos
Ensure your environment supports the google/protobuf PHP extension (required for protobuf serialization).
First Use Case: Audit Logs
Use the Google\Cloud\Core\V1\AuditLog class to structure log entries for Google Cloud Audit Logs:
use Google\Cloud\Core\V1\AuditLog;
use Google\Cloud\Core\V1\LogEntry;
$logEntry = new LogEntry();
$logEntry->setTimestamp(new \Google\Type\Timestamp());
$logEntry->setProtoPayload(new AuditLog());
// Populate AuditLog fields (e.g., authentication, methodName, etc.)
Key Classes to Explore
Google\Cloud\Core\V1\AuditLog: For audit logging compliance.Google\Rpc\Status: Standardize error responses.Google\Logging\Type\HttpRequest: Enrich logs with HTTP metadata.Google\Protobuf\Internal\Message: Base class for protobuf messages.Where to Look First
google/cloud-logging).Indirect Usage via Google Cloud SDKs
Most Laravel developers will use this package indirectly through Google Cloud SDKs (e.g., google/cloud-logging). Example:
use Google\Cloud\Logging\V2\LoggingClient;
$logging = new LoggingClient();
$logName = $logging->logName('your-project-id', 'your-log');
$entry = $logging->entry($logName, [
'severity' => 'INFO',
'message' => 'Test log entry',
'httpRequest' => (new \Google\Logging\Type\HttpRequest())
->setRequestUrl('https://example.com')
->setRequestMethod('GET')
]);
$logging->write($entry);
The SDK handles protobuf serialization under the hood.
Direct Protobuf Usage For custom gRPC services or advanced integrations, use generated protobuf classes directly:
use Google\Protobuf\Internal\Message;
use Google\Cloud\Core\V1\AuditLog;
$auditLog = new AuditLog();
$auditLog->setAuthenticationInfo('user@example.com');
$auditLog->setMethodName('/api/v1/resource');
// Serialize to binary protobuf format
$binaryData = $auditLog->serializeToString();
Laravel Service Container Integration Bind protobuf clients to Laravel’s container for dependency injection:
$this->app->bind(LoggingClient::class, function ($app) {
return new LoggingClient([
'keyFilePath' => storage_path('service-account.json'),
]);
});
Protobuf Message Extension Extend protobuf messages to add custom fields while maintaining compatibility:
use Google\Logging\Type\LogEntry;
class CustomLogEntry extends LogEntry {
public function setCustomField($value) {
$this->customField = $value;
}
}
Audit Logging Workflow
AuditLog to structure log entries for Google Cloud Audit Logs.$logger = new \Monolog\Logger('google');
$logger->pushHandler(new GoogleCloudLogHandler($loggingClient));
Error Standardization
Google\Rpc\Status:
$status = new \Google\Rpc\Status();
$status->setCode(\Google\Rpc\Code::INVALID_ARGUMENT);
$status->setMessage('Invalid input data');
throw new \Google\ApiCore\ApiException($status);
HTTP Metadata Enrichment
$httpRequest = new \Google\Logging\Type\HttpRequest();
$httpRequest->setRequestUrl(request()->fullUrl());
$httpRequest->setRequestMethod(request()->method());
$logEntry->setHttpRequest($httpRequest);
Environment Setup
google/protobuf extension is enabled in php.ini:
extension=protobuf.so
Dockerfile:
RUN docker-php-ext-install protobuf
Laravel Logging
class GoogleCloudLogHandler extends \Monolog\Handler\AbstractProcessingHandler {
public function __construct(LoggingClient $loggingClient) {
parent::__construct(Monolog\Logger::DEBUG, true);
$this->loggingClient = $loggingClient;
}
protected function write(array $record): void {
$logEntry = $this->loggingClient->entry(...);
$this->loggingClient->write($logEntry);
}
}
Protobuf Schema Evolution
^1.0) to stay aligned with Google’s releases.Testing
$mockAuditLog = $this->createMock(AuditLog::class);
$mockAuditLog->method('serializeToString')->willReturn('mocked-data');
Extension Dependency
google/protobuf PHP extension is required but not always enabled by default.Fatal error: Uncaught Error: Class 'Google\Protobuf\Internal\Message' not found
google/cloud-sdk).Immutable DTOs
$updatedLog = $logEntry->withCustomField('new-value');
Schema Versioning
ReservationResourceUsage).Binary Data Handling
serializeToString() for binary data or toArray() for debug-friendly output (if available).Laravel Serialization
$response->setData([
'log_entry' => $logEntry->serializeToString(), // Binary data
]);
Performance Overhead
Inspect Protobuf Messages
var_dump($message->serializeToString(), true) to inspect binary data.Common Errors
Class not found: Ensure google/cloud-common-protos and google/protobuf are installed.Method not found: Verify the protobuf class version matches your Google Cloud SDK version.Logging Protobuf Data
\Log::debug('Protobuf data', [
'binary' => $message->serializeToString(),
'json' => json_encode($message->toArray()), // If supported
]);
google/cloud-logging over direct protobuf usage to avoid reinventing serialization logic.How can I help you explore Laravel packages today?