1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 | <?php namespace DrSlump\Protobuf; // Load descriptor messages require_once __DIR__ . '/Compiler/protos/descriptor.pb.php'; require_once __DIR__ . '/Compiler/protos/plugin.pb.php'; require_once __DIR__ . '/Compiler/protos/php.pb.php'; require_once __DIR__ . '/Compiler/protos/json.pb.php'; use DrSlump\Protobuf; use google\protobuf as proto; class Compiler { /** @var bool */ protected $verbose = false; /** @var array */ protected $packages = array(); /** @var \DrSlump\Protobuf\Compiler\CommentsParser */ protected $comments; /** @var bool */ protected $skipImported = false; /** @var array */ protected $options = array(); /** @var array */ protected $protos = array(); public function __construct($verbose = false) { $this->verbose = $verbose; $this->comments = new Compiler\CommentsParser(); } public function stderr($str) { $str = str_replace("\n", PHP_EOL, $str); fputs(STDERR, $str . PHP_EOL); } public function notice($str) { if ($this->verbose) { $this->stderr('NOTICE: ' . $str); } } public function warning($str) { $this->stderr('WARNING: ' . $str); } protected function error($str) { $this->stderr('ERROR: ' . $str); } public function getPackages() { return $this->packages; } public function hasPackage($package) { return isset($this->packages[$package]); } public function getPackage($package) { return $this->packages[$package]; } public function setPackage($package, $namespace) { $this->packages[$package] = $namespace; } public function getOption($option, $type = 'string') { $value = isset($this->options[$option]) ? $this->options[$option] : null; switch ($type) { case 'bool': return filter_var($value, FILTER_VALIDATE_BOOLEAN); default: return $value; } } public function camelize($name) { return preg_replace_callback( '/_([a-z])/i', function($m){ return strtoupper($m[1]); }, $name ); } public function compile($data) { // Parse the request $req = new \google\protobuf\compiler\CodeGeneratorRequest($data); // Set default generator class $generator = __CLASS__ . '\PhpGenerator'; // Reset comments parser $this->comments->reset(); $parseComments = false; // Get plugin arguments if ($req->hasParameter()) { parse_str($req->getParameter(), $args); foreach ($args as $arg=>$val) { switch($arg){ case 'verbose': $this->verbose = filter_var($val, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE); break; case 'json': $this->notice("Using ProtoJson generator"); $generator = __CLASS__ . '\JsonGenerator'; break; case 'comments': $parseComments = filter_var($val, FILTER_VALIDATE_BOOLEAN); break; case 'protos': $this->protos = $val; break; case 'skip-imported': $this->skipImported = filter_var($val, FILTER_VALIDATE_BOOLEAN); break; case 'options': $this->options = $val; break; default: $this->warning('Skipping unknown option ' . $arg); } } } // Parse comments if we're told to do so if ($parseComments) { if (empty($this->protos)) { throw new \RuntimeException('Unable to port comments if .proto files are not passed as argument'); } foreach ($this->protos as $fname) { $src = file_get_contents($fname); if (FALSE === $src) { throw new \RuntimeException('Unable to parse file ' . $fname . ' for comments'); } $this->comments->parse($src); } } /** @var $generator \DrSlump\Protobuf\Compiler\AbstractGenerator */ $generator = new $generator($this); // Setup response object $resp = new \google\protobuf\compiler\CodeGeneratorResponse(); // First iterate over all the protos to get a map of namespaces $this->packages = array(); foreach($req->getProtoFileList() as $proto) { $package = $proto->getPackage(); $namespace = $generator->getNamespace($proto); if (isset($this->packages[$package]) && $namespace !== $this->packages[$package]) { $this->warning("Package $package was already mapped to {$this->packages[$package]} but has now been overridden to $namespace"); } $this->packages[$package] = $namespace; $this->notice("Mapping $package to $namespace"); } // Get the list of files to generate $files = $req->getFileToGenerate(); // Run each file foreach ($req->getProtoFileList() as $file) { // Only compile those given to generate, not the imported ones if ($this->skipImported && !in_array($file->getName(), $files)) { $this->notice('Skipping generation of imported file "' . $file->getName() . '"'); continue; } $sources = $generator->generate($file); foreach ($sources as $source) { $this->notice('Generating "' . $source->getName() . '"'); $resp->addFile($source); } } // Finally serialize the response object return $resp->serialize(); } public function getComment($ident, $prefix = '') { if (!$this->comments->hasComment($ident)) { return null; } $comment = $this->comments->getComment($ident); if (0 < strlen($prefix)) { $comment = $prefix . str_replace("\n", "\n$prefix", $comment); } return $comment; } } |