travel / grpc-proto
Shared gRPC proto definitions and generated PHP code for Travel services
Requires
- php: >=8.1
- google/protobuf: ^4.29.0
- grpc/grpc: ^1.57
Requires (Dev)
- phpunit/phpunit: ^10.0
This package is auto-updated.
Last update: 2026-03-04 10:35:24 UTC
README
Shared gRPC protocol buffer definitions and generated PHP code for Travel services.
๐ Structure
travel-gRPC/
โโโ composer.json # Library definition
โโโ protos/ # Proto source files (edit these)
โ โโโ tour/
โ โโโ v1/
โ โโโ tour.proto
โโโ src/ # Generated PHP code (DO NOT edit manually)
โ โโโ Travel/
โ โโโ Proto/
โ โโโ Tour/
โ โโโ V1/
โ โโโ TourServiceClient.php
โ โโโ GetTourByIdRequest.php
โ โโโ ListToursRequest.php
โ โโโ TourResponse.php
โ โโโ ListToursResponse.php
โโโ scripts/
โโโ generate.sh # Compilation script
๐ Installation
Prerequisites
-
Protocol Buffers Compiler (protoc)
# Ubuntu/Debian sudo apt-get install -y protobuf-compiler # macOS brew install protobuf # Or download from: https://github.com/protocolbuffers/protobuf/releases
-
gRPC PHP Plugin (optional, for service generation)
# Follow instructions at: https://grpc.io/docs/languages/php/quickstart/
Install the Library
Add to your Laravel project's composer.json:
{
"repositories": [
{
"type": "path",
"url": "../travel-gRPC"
}
],
"require": {
"travel/grpc-proto": "*"
}
}
Then run:
composer install
๐ง Usage
1. Define Proto Files
Edit or add .proto files in the protos/ directory:
// protos/tour/v1/tour.proto syntax = "proto3"; package tour.v1; option php_namespace = "Travel\\Proto\\Tour\\V1";
2. Generate PHP Code
Run the generation script:
# From the library root composer generate # Or directly bash scripts/generate.sh
This will compile all .proto files and generate PHP classes in the src/ directory.
3. Use in Your Laravel Services
Server Side (Service Implementation)
<?php namespace App\Grpc\Services; use Travel\Proto\Tour\V1\TourServiceInterface; use Travel\Proto\Tour\V1\GetTourByIdRequest; use Travel\Proto\Tour\V1\TourResponse; class TourService implements TourServiceInterface { public function GetTourById( GetTourByIdRequest $request ): TourResponse { $tour = Tour::find($request->getTourId()); $response = new TourResponse(); $response->setId($tour->id); $response->setName($tour->name); $response->setDescription($tour->description); $response->setPrice($tour->price); return $response; } }
Client Side (Calling Another Service)
<?php namespace App\Services; use Travel\Proto\Tour\V1\TourServiceClient; use Travel\Proto\Tour\V1\GetTourByIdRequest; use Grpc\ChannelCredentials; class TourClientService { private TourServiceClient $client; public function __construct() { $this->client = new TourServiceClient( 'tour-service:50051', ['credentials' => ChannelCredentials::createInsecure()] ); } public function getTourById(string $tourId): array { $request = new GetTourByIdRequest(); $request->setTourId($tourId); $request->setLanguage('en'); [$response, $status] = $this->client->GetTourById($request)->wait(); if ($status->code !== \Grpc\STATUS_OK) { throw new \Exception("gRPC Error: " . $status->details); } return [ 'id' => $response->getId(), 'name' => $response->getName(), 'description' => $response->getDescription(), 'price' => $response->getPrice(), ]; } }
๐ Development Workflow
Adding New Proto Definitions
-
Create a new
.protofile inprotos/:mkdir -p protos/booking/v1 touch protos/booking/v1/booking.proto
-
Define your service and messages
-
Generate PHP code:
composer generate
-
Commit both the
.protofile and generated code:git add protos/ src/ git commit -m "Add booking service proto"
Updating Existing Protos
-
Edit the
.protofile inprotos/ -
Regenerate PHP code:
composer generate
-
Update your service implementations to match the new schema
-
Commit changes
โ ๏ธ Important Notes
- DO NOT manually edit files in the
src/directory - they are auto-generated - Always regenerate after modifying
.protofiles - Use semantic versioning for breaking changes
- Keep proto packages versioned (e.g.,
tour.v1,tour.v2)
๐ Versioning
When making breaking changes:
-
Create a new version directory:
mkdir -p protos/tour/v2 cp protos/tour/v1/tour.proto protos/tour/v2/tour.proto
-
Update the package and namespace in the new proto:
package tour.v2; option php_namespace = "Travel\\Proto\\Tour\\V2";
-
Make your changes in v2
-
Both v1 and v2 will coexist, allowing gradual migration
๐ฆ Publishing
To use this library across multiple projects:
-
Private Git Repository (Recommended):
{ "repositories": [ { "type": "vcs", "url": "git@github.com:your-org/travel-grpc-proto.git" } ] } -
Private Packagist or Satis
-
Path Repository (Development):
{ "repositories": [ { "type": "path", "url": "../travel-gRPC" } ] }
๐งช Testing
composer test
๐ License
MIT