Exemple #1
0
 public static function run($pluginExecutable)
 {
     $stdin = '';
     // PHP doesn't implement non-blocking stdin on Windows
     // https://bugs.php.net/bug.php?id=34972
     $isWin = 'WIN' === strtoupper(substr(PHP_OS, 0, 3));
     if (!$isWin) {
         // Open STDIN in non-blocking mode
         stream_set_blocking(STDIN, FALSE);
         // Loop until STDIN is closed or we've waited too long for data
         $cnt = 0;
         while (!feof(STDIN) && $cnt++ < 10) {
             // give protoc some time to feed the data
             usleep(10000);
             // read the bytes
             $bytes = fread(STDIN, 1024);
             if (strlen($bytes)) {
                 $cnt = 0;
                 $stdin .= $bytes;
             }
         }
         // If on windows and no arguments were given
     } else {
         if ($_SERVER['argc'] < 2) {
             $stdin = fread(STDIN, 1024 * 1024);
         }
     }
     // If no input was given we launch protoc from here
     if (0 === strlen($stdin)) {
         if ($isWin) {
             $pluginExecutable .= '.bat';
         }
         self::runProtoc($pluginExecutable);
         exit(0);
     }
     // We have data from stdin so compile it
     try {
         // Create a compiler interface
         $comp = new Protobuf\Compiler();
         echo $comp->compile($stdin);
         exit(0);
     } catch (\Exception $e) {
         fputs(STDERR, 'ERROR: ' . $e->getMessage());
         fputs(STDERR, $e->getTraceAsString());
         exit(255);
     }
 }
 /**
  * Obtain the comment doc associated to an identifier
  *
  * @param string $ident
  * @param string $prefix
  * @return string
  */
 public function comment($ident, $prefix = '')
 {
     return $this->compiler->getComment($ident, $prefix);
 }
 /**
  * @param InputInterface  $input
  * @param OutputInterface $output
  *
  * @return int
  */
 protected function executeCompile(InputInterface $input, OutputInterface $output)
 {
     try {
         // Create a compiler interface
         $comp = new Protobuf\Compiler();
         echo $comp->compile($this->getStdIn());
         return 0;
     } catch (\Exception $e) {
         $output->writeln('<error>' . $e->getMessage() . '</error>');
         $output->writeln('<error>' . $e->getTraceAsString() . '<error>');
         return 255;
     }
 }