protected function buildFile(proto\FileDescriptorProto $proto, $fname, $contents) { $suffix = $this->getOption('suffix') ?: '.php'; $fname .= $suffix; $file = new \google\protobuf\compiler\CodeGeneratorResponse\File(); $file->setName($fname); $s = array(); $s[] = "<?php"; $s[] = "// DO NOT EDIT! Generated by Protobuf-PHP protoc plugin " . Protobuf::VERSION; $s[] = "// Source: " . $proto->getName(); $s[] = "// Date: " . date('Y-m-d H:i:s'); $s[] = ""; $s[] = "// @@protoc_insertion_point(scope_file)"; $s[] = ""; $contents = implode(PHP_EOL, $s) . PHP_EOL . $contents; // If we don't want insertion points remove them if (!$this->getOption('insertions')) { $contents = preg_replace('#^\\s*//\\s+@@protoc_insertion_point.*$\\n#m', '', $contents); } $file->setContent($contents); return $file; }
public function compileProtoFile(proto\FileDescriptorProto $proto) { $file = new \google\protobuf\compiler\CodeGeneratorResponse\File(); $opts = $proto->getOptions(); $name = pathinfo($proto->getName(), PATHINFO_FILENAME); $name .= isset($opts['json.suffix']) ? $opts['json.suffix'] : '.js'; $file->setName($name); $namespace = $this->getNamespace($proto); $s[] = "// DO NOT EDIT! Generated by Protobuf for PHP protoc plugin " . Protobuf::VERSION; $s[] = "// Source: " . $proto->getName(); $s[] = "// Date: " . date('Y-m-d H:i:s'); $s[] = ""; $s[] = "(function(){"; $s[] = "/** @namespace */"; $s[] = "var {$namespace} = {$namespace} || {};"; $s[] = ""; $s[] = "// Make it CommonJS compatible"; $s[] = "if (typeof exports !== 'undefined') {"; $s[] = " var ProtoJson = this.ProtoJson;"; $s[] = " if (!ProtoJson && typeof require !== 'undefined')"; $s[] = " ProtoJson = require('ProtoJson');"; $s[] = " {$namespace} = exports;"; $s[] = "} else {"; $s[] = " this.{$namespace} = {$namespace};"; $s[] = "}"; $s[] = ""; // Generate Enums foreach ($proto->getEnumTypeList() as $enum) { $s[] = $this->compileEnum($enum, $namespace); } // Generate Messages foreach ($proto->getMessageTypeList() as $msg) { $s[] = $this->compileMessage($msg, $namespace); } // Collect extensions if ($proto->hasExtension()) { foreach ($proto->getExtensionList() as $field) { $this->extensions[$field->getExtendee()][] = array($namespace, $field); } } // Dump all extensions found in this proto file if (count($this->extensions)) { foreach ($this->extensions as $extendee => $fields) { foreach ($fields as $pair) { list($ns, $field) = $pair; $s[] = $this->compileExtension($field, $ns, ''); } } } $s[] = "})();"; $src = implode("\n", $s); $file->setContent($src); return array($file); }