google/common-protos
Generated PHP classes for Google’s common Protocol Buffer types used across the Google API ecosystem. Distributed as the google/common-protos Composer package under Apache 2.0 and designed to be stable for use in your applications.
Installation:
composer require google/common-protos
This provides protocol buffer classes for Google API common types (e.g., google.api.Http, google.protobuf.Timestamp).
First Use Case:
Validate API request/response structures against Google’s standardized proto definitions.
Example: Define a request payload using google.api.HttpRule:
use Google\Api\HttpRule;
use Google\Protobuf\Timestamp;
$httpRule = new HttpRule();
$httpRule->setSelector('com.example.MyService.CreateRequest');
$httpRule->setGet('/v1/{name=projects/*}');
Key Classes to Explore:
google.api.Http (for REST/HTTP mappings)google.protobuf.Timestamp (for time handling)google.api.MethodSettings (for API method configurations)google.api.FieldBehavior (for field metadata)vendor/google/common-protos/src/ for auto-generated proto classes.FieldInfo, QuotaFailure) for breaking changes.Pattern: Use proto classes to enforce schema compliance in Laravel requests/responses.
use Google\Api\HttpRule;
use Illuminate\Http\Request;
public function validateRequest(Request $request) {
$httpRule = new HttpRule();
$httpRule->setPost('/v1/messages');
$httpRule->setBody('type.googleapis.com/google.example.v1.Message');
// Validate against $httpRule->getBody() in middleware
}
TimestampPattern: Convert Carbon/Laravel timestamps to protobuf-compatible formats.
use Google\Protobuf\Timestamp;
public function toProtoTimestamp(\Carbon\Carbon $carbon) {
$timestamp = new Timestamp();
$timestamp->setSeconds($carbon->getTimestamp());
$timestamp->setNanos($carbon->getMicroseconds() * 1000);
return $timestamp;
}
Pattern: Attach MethodSettings to API routes for metadata (e.g., quota, auth).
use Google\Api\MethodSettings;
Route::post('/messages', function () {
$settings = new MethodSettings();
$settings->setName('com.example.CreateMessage');
$settings->setHttpRule(new HttpRule(['get' => '/v1/messages']));
// Use $settings in middleware or service layer
});
Pattern: Extend proto messages with custom fields using oneof or extensions.
use Google\Api\FieldBehavior;
$fieldInfo = new \Google\Api\FieldInfo();
$fieldInfo->setBehavior(FieldBehavior::IDENTIFIER);
$fieldInfo->setJsonName('custom_field');
Pattern: Pair with google/cloud-* SDKs for seamless protobuf serialization.
use Google\Cloud\Logging\V2\LoggingServiceClient;
$client = new LoggingServiceClient();
$entry = new \Google\Logging\Type\LogEntry();
$entry->setTimestamp($this->toProtoTimestamp(now()));
$client->writeLogEntries(...);
ProtobufService.HttpRule).Google\Protobuf\Internal\Message assertions in PHPUnit.Protobuf Version Mismatch:
google/protobuf dependency version diverges.google/protobuf:^5.0 in composer.json (see v4.8.3).Deprecated Fields:
Endpoint.aliases were un-deprecated in v4.7.0. Check changelogs for resurrected fields.Naming Collisions:
Timestamp) may conflict with Laravel helpers.\Google\Protobuf\Timestamp).Immutable Fields:
clear() or mergeFrom() to modify.$timestamp->clear(); // Reset all fields
$timestamp->mergeFrom($newTimestamp); // Merge another message
google/protobuf:validate to check protobuf messages.
$message->validate(); // Throws \Google\Protobuf\InvalidArgumentException
putenv('GRPC_VERBOSITY=DEBUG');
putenv('GRPC_TRACE=api');
Custom Proto Extensions:
.proto files and regenerate:
extend google.api.FieldInfo {
string custom_metadata = 1000;
}
protoc and include in your project.Laravel Service Providers:
AppServiceProvider:
$this->app->singleton(\Google\Protobuf\Internal\Message::class, function () {
return new \Google\Protobuf\Internal\Message();
});
Event Listeners:
Event::listen(\Google\Protobuf\InvalidArgumentException::class, function ($e) {
Log::error('Protobuf validation failed: ' . $e->getMessage());
});
strict_types=1.google.api.BatchRequest for grouped API calls:
$batch = new \Google\Api\BatchRequest();
$batch->addRequests([$request1, $request2]);
google.api.QuotaFailure for rate-limiting:
if ($response->hasQuotaFailure()) {
$quota = $response->getQuotaFailure();
Log::warning("Quota exceeded: {$quota->getReason()}");
}
FieldInfo.referenced_types (added in v4.7.0) for polymorphic fields.How can I help you explore Laravel packages today?