google/common-protos
Generated PHP classes for Google’s common Protocol Buffer types used across Google APIs. Stable, backwards-compatible shared dependencies published as the google/common-protos Composer package (Apache 2.0), part of the Google Cloud PHP ecosystem.
Installation:
composer require google/common-protos
This installs the generated Protocol Buffer (protobuf) classes for Google API common types.
First Use Case: Validate API request/response structures against Google's standardized protobuf definitions. Example:
use Google\Api\FieldBehavior;
use Google\Api\ResourcePermission;
// Define a resource permission for an API method
$permission = new ResourcePermission();
$permission->setName('projects.update');
$permission->setTitle('Update Project');
// Use FieldBehavior enum
$behavior = FieldBehavior::IDENTIFIER;
Key Classes to Explore:
Google\Api\MethodSettings (for API method configurations)Google\Api\ResourcePermission (for IAM-like permissions)Google\Api\FieldBehavior (field metadata)Google\Api\QuotaFailure (for quota-related errors)Documentation:
Use protobuf classes to enforce request/response schemas in Laravel services:
// In a Laravel service class
public function validateRequest(array $data): void
{
$requestProto = new Google\Api\MethodSettings();
$requestProto->setName($data['method_name']);
// Validate against protobuf structure
if (!isset($data['resource_permissions'])) {
throw new \InvalidArgumentException('Missing resource permissions');
}
}
Leverage protobuf types to generate API clients dynamically:
// Example: Build a client for a hypothetical API
$client = new Google\Cloud\ServiceClient([
'apiMethodSettings' => [
new Google\Api\MethodSettings([
'name' => 'v1.projects.update',
'resourcePermissions' => [
new Google\Api\ResourcePermission([
'name' => 'projects.update',
'title' => 'Update Project',
]),
],
]),
],
]);
Standardize error responses using protobuf-defined error types:
// In an Exception Handler
public function render($request, Throwable $exception)
{
if ($exception instanceof \Google\ApiCore\ApiException) {
$errorProto = $exception->getProto();
return response()->json([
'error' => [
'code' => $errorProto->getCode(),
'message' => $errorProto->getMessage(),
'details' => $errorProto->getDetails(),
],
]);
}
return parent::render($request, $exception);
}
Register protobuf-based services in Laravel:
// In AppServiceProvider
public function register()
{
$this->app->singleton(Google\Api\FieldBehavior::class, function () {
return new Google\Api\FieldBehavior();
});
}
Use protobuf classes in PHPUnit tests to validate API contracts:
public function testApiMethodSettings()
{
$settings = new Google\Api\MethodSettings();
$settings->setName('test.method');
$this->assertEquals('test.method', $settings->getName());
}
Pair with Google Cloud PHP SDKs (e.g., google/cloud-core) for unified protobuf handling:
use Google\Cloud\Core\Api\ApiInterface;
use Google\Api\MethodSettings;
$api = new class implements ApiInterface {
public function getMethodSettings(): MethodSettings
{
return new MethodSettings([
'name' => 'custom.method',
'resourcePermissions' => [...],
]);
}
};
google/protobuf dependency is updated:
composer require google/protobuf:^5.0
// vite.config.js
export default {
build: {
rollupOptions: {
external: ['**/vendor/google/protobuf/**'],
},
},
};
Google\Api namespace, which may conflict with Laravel's Google facade or other libraries.use Google\Api\FieldBehavior as ProtoFieldBehavior;
$proto = new Google\Api\MethodSettings();
$json = $proto->serializeToJsonString(); // Not auto-magic!
class ProtoHelper {
public static function toJson($proto): string
{
return $proto->serializeToJsonString();
}
}
$cachedProto = Cache::remember('api.method.settings', 3600, function () {
return new Google\Api\MethodSettings();
});
google/protobuf’s GeneratedMessage optimizations for repeated use:
$proto = new Google\Api\MethodSettings();
$proto->clear(); // Reuse instances
\Google\Protobuf\Internal\Debug::setEnabled(true);
var_dump($proto->debugString()) to inspect raw protobuf data.$newProto = $originalProto->toBuilder()->setName('new.name')->buildPartial();
$settings = Cache::get('api.settings', function () {
return new Google\Api\MethodSettings();
});
$proto = new Google\Api\MethodSettings();
// $proto->serializeToJsonString(); // Throws exception if required fields are missing
Google\Api\FieldBehavior::IDENTIFIER (not strings).Endpoint.aliases was un-deprecated in v4.7.0).Google\Protobuf\Internal\TestUtil for assertions:
use Google\Protobuf\Internal\TestUtil;
public function testProtoEquality()
{
$proto1 = new Google\Api\MethodSettings();
$proto2 = new Google\Api\MethodSettings();
$this->assertTrue(TestUtil::equalTo($proto1, $proto2));
}
$this->app->bind(Google\Api\MethodSettings::class, function () {
return new Google\Api\MethodSettings();
});
How can I help you explore Laravel packages today?