travel/grpc-proto
Composer 安装命令:
composer require travel/grpc-proto
包简介
Shared gRPC proto definitions and generated PHP code for Travel services
README 文档
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
统计信息
- 总下载量: 23
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-11-24