我克隆了 Protobuf-PHP 存储库:https://github.com/drslump/Protobuf-PHP.git我在 https://github.com/drslump/Protobuf-PHP 找到的,并且我已经花了大约 12 个小时来解决安装和配置问题,试图让 protoc-gen-php 将 proto 文件转换为 PHP 类。
我正在运行 PHP 版本 5.3.2,这是我所做的:
- 已安装 PEAR v1.9.4
- 安装 Console_CommandLine,并运行 PEAR_ENV.reg 以设置 PEAR 环境变量。
- 我已经尝试了所有我能想到的排列方式来尝试让这个插件生成一个 PHP 类文件,但每次尝试都失败了。
我在根文件夹中有一个用于 C# 项目的原型(prototype)文件,我在其中 checkout 了 proto-gen-php 项目(见下文)。
message AuditLogEntry {
required int64 ID = 1;
required int64 Date = 2;
required string Message = 3;
optional int32 User_ID = 4;
optional string User_Username = 5;
optional int32 ImpUser_ID = 6;
optional string ImpUser_Username = 7;
optional string RemoteIP = 8;
}
message AuditLogQuery {
optional int64 DateRangeStart = 1;
optional int64 DateRangeEnd = 2;
optional string Search = 3;
optional int32 UserID = 4;
optional int32 Limit = 5;
optional int32 Offset = 6;
}
message AuditLogResult {
repeated AuditLogEntry LogEntries = 1;
optional int32 TotalResults = 2;
}
这里只是我遇到的几个问题:
当我尝试执行最基本的实现时:
protoc-gen-php.bat AuditLog.proto
我收到这个错误:
Could not open input file: @bin_dir@\protoc-gen-php The filename, directory name, or volume label syntax is incorrect.
我无法在线找到有关此错误的任何信息。我正在使用的其中一个软件包的安装是否存在配置问题?有什么想法吗?
由于上述错误,我在 protoc-gen-php 批处理文件中更改了这一行:
"%PHPBIN%" -d display_errors=stderr -d log_errors=On -d error_log=Off "@bin_dir@\protoc-gen-php" %*
到
"%PHPBIN%" -d display_errors=stderr -d log_errors=On -d error_log=Off "protoc-gen-php.php" %*
现在,当我运行问题 1 中给出的相同命令时,我得到以下输出:
The filename, directory name, or volume label syntax is incorrect.
Protobuf-PHP @package_version@ by Ivan -DrSlump- Montes
C:\Development\SkedFlex\php-proto-buffers\AuditLog.proto: File does not reside within any path specified using --proto_path (or -I). You must specify a --proto_path which encompasses this file. Note that the proto_path must be an exact prefix of the .proto file names -- protoc is too dumb to figure out when two paths (e.g. absolute and relative) are equivalent (it's harder than you think).
ERROR: protoc exited with an error (1) when executed with:
protoc \
--plugin=protoc-gen-php="C:\Development\SkedFlex\php-proto-buffers\protoc-gen-php.php.bat" \
--proto_path="C:\Development\SkedFlex\php-proto-buffers\library\DrSlump\Protobuf\Compiler\protos" \
--php_out=":./" \
"C:\Development\SkedFlex\php-proto-buffers\AuditLog.proto"
我尝试使用手册中指定的 -i、-o 命令的多种变体:http://drslump.github.com/Protobuf-PHP/protoc-gen-php.1.html ,但似乎无法在这方面取得进展......
最后我尝试直接执行协议(protocol):
protoc --plugin=protoc-gen-php --php_out=./build AuditLog.proto
这是错误:
--php_out: protoc-gen-php: The system cannot find the file specified.
我陷入了困境。有没有使用过 protoc-gen-php 的人可以帮助我解决我遇到的问题?
最佳答案
.bat文件从版本库中 check out 后不能直接使用,是在创建Pear包时修改的。因此,除非您想以某种方式修改库的源代码,否则您应该使用 Pear 安装 Protobuf-PHP。
pear channel-discover pear.pollinimini.net
pear install drslump/Protobuf-beta
如果您不想从 Pear 安装,则必须手动修改 protoc-gen-php.bat
文件,将最后一行替换为与此类似的内容:
C:\path\to\php.exe -d display_errors=stderr -d log_errors=On -d error_log=Off c:\path\to\protobuf-php\protoc-gen-php %*
为了修复“文件不在指定的任何路径中...”错误,您必须确保作为参数给出的任何原型(prototype)文件都在定义的包含路径中。 Google 的 protoc
编译器不会尝试猜测包含路径是什么,因此我们需要在所有情况下手动提供它们。
protoc-gen-php.bat -i . AuditLog.proto
这应该允许您为原型(prototype)消息生成 PHP 文件,尽管该脚本在 Windows 系统中的测试不如在 Linux 和 OSX 系统中的测试。如果您发现任何问题,可以直接通过 the project site 报告。如果你愿意的话。
关于php - Protobuf-PHP : Cannot Successfully Execute protoc-gen-php,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12098176/