Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion library/DrSlump/Protobuf/Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,4 +208,3 @@ public function getComment($ident, $prefix = '')
return $comment;
}
}

6 changes: 3 additions & 3 deletions library/DrSlump/Protobuf/Compiler/Cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ public static function run($pluginExecutable)
// We have data from stdin so compile it
try {
// Create a compiler interface
$comp = new Protobuf\Compiler();
$comp = new Protobuf\Compiler(true);
echo $comp->compile($stdin);
exit(0);
} catch(\Exception $e) {
fputs(STDERR, 'ERROR: ' . $e->getMessage());
fputs(STDERR, $e->getTraceAsString());
fputs(STDERR, 'ERROR: ' . $e->getMessage() . PHP_EOL);
fputs(STDERR, $e->getTraceAsString() . PHP_EOL);
exit(255);
}
}
Expand Down
67 changes: 67 additions & 0 deletions library/DrSlump/Protobuf/Compiler/PhpGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ public function generate(proto\FileDescriptorProto $proto)
$this->addComponent($namespace, $service->getName(), $src);
}
endif;
foreach($proto->getServiceList() as $service) {
$src = $this->compileStub($service, $namespace);
$this->addComponent($namespace, $service->getName() . 'Stub', $src);
}

// Collect extensions
if ($proto->hasExtension()) {
Expand Down Expand Up @@ -409,6 +413,69 @@ protected function compileService(proto\ServiceDescriptorProto $service, $ns)
return implode(PHP_EOL, $s) . PHP_EOL;
}

protected function compileStub(proto\ServiceDescriptorProto $service, $ns){
$s = array();
$s[] = 'namespace ' . $this->normalizeNS($ns) . ' {';
$s[] = '';
$s[]= " // @@protoc_insertion_point(scope_namespace)";
$s[]= " // @@protoc_insertion_point(namespace_$ns)";
$s[]= '';

$cmt = $this->compiler->getComment($ns . '.' . $service->getName(), ' * ');
if($cmt){
$s[]= " /**";
$s[]= $cmt;
$s[]= " */";
}
$s[] = ' class ' . $service->getName() . 'Client{';
$s[] = '';
$s[] = ' private $rpc_impl;';
$s[] = '';
$s[] = ' public function __construct($rpc_impl) {';
$s[] = ' $this->rpc_impl = $rpc_impl;';
$s[] = ' }';

foreach ($service->getMethodList() as $method){
$ns_input = $this->normalizeNS($method->getInputType());
$ns_output = $this->normalizeNS($method->getOutputType());
$s[]= ' /**';

$cmt = $this->compiler->getComment($ns . '.' . $service->getName() . '.' . $method->getName(), ' * ');
if ($cmt){
$s[]= $cmt;
$s[]= ' * ';
}

$s[]= ' * @param ' . $ns_input . ' $input';
$s[]= ' */';
$server_stream = $method->getServerStreaming();
$client_stream = $method->getClientStreaming();
$service_fqn = $ns . '.' . $service->getName();
if($client_stream){
if($server_stream){
$s[]= ' public function ' . $method->getName() . '($metadata = array()) {';
$s[]= ' return $this->rpc_impl->_bidiRequest(\'/' . $service_fqn . '/' . $method->getName() . '\', \'\\' . $ns_output . '::deserialize\', $metadata);';
} else {
$s[]= ' public function ' . $method->getName() . '($arguments, $metadata = array()) {';
$s[]= ' return $this->rpc_impl->_clientStreamRequest(\'/' . $service_fqn . '/' . $method->getName() . '\', $arguments, \'\\' . $ns_output . '::deserialize\', $metadata);';
}
} else {
if($server_stream){
$s[]= ' public function ' . $method->getName() . '($argument, $metadata = array()) {';
$s[]= ' return $this->rpc_impl->_serverStreamRequest(\'/' . $service_fqn . '/' . $method->getName() . '\', $argument, \'\\' . $ns_output . '::deserialize\', $metadata);';
} else {
$s[]= ' public function ' . $method->getName() . '(\\' . $ns_input . ' $argument, $metadata = array()) {';
$s[]= ' return $this->rpc_impl->_simpleRequest(\'/' . $service_fqn . '/' . $method->getName() . '\', $argument, \'\\' . $ns_output . '::deserialize\', $metadata);';
}
}
$s[]= ' }';
}
$s[]= ' }';
$s[]= '}';

return implode(PHP_EOL, $s) . PHP_EOL;
}

protected function generatePublicField(proto\FieldDescriptorProto $field, $ns, $indent)
{
$cmt = $this->compiler->getComment($ns . '.' . $field->getNumber(), "$indent * ");
Expand Down
Loading