stanley-cheung / Protobuf-PHP

PHP implementation of Google's Protocol Buffers with a protoc plugin compiler
http://drslump.github.com/Protobuf-PHP/
MIT License
22 stars 11 forks source link

$stdin is always empty in Cli.php #15

Open nevernet opened 7 years ago

nevernet commented 7 years ago

see the code:
(file is: \DrSlump\Protobuf\Compiler\Cli.php

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(true);
            echo $comp->compile($stdin);
            exit(0);
        } catch(\Exception $e) {
            fputs(STDERR, 'ERROR: ' . $e->getMessage() . PHP_EOL);
            fputs(STDERR, $e->getTraceAsString() . PHP_EOL);
            exit(255);
        }
    }

when i run command ./vendor/bin/protoc-gen-php.php -i . -o . helloworld.proto and the $_SERVER['argc'] is always more than 2 see details as below:

[argv] => Array
        (
            [0] => ./vendor/bin/protoc-gen-php.php
            [1] => -i
            [2] => .
            [3] => -o
            [4] => .
            [5] => helloworld.proto
        )

    [argc] => 6

so it will always execute runProtoc($pluginExecutable);, but not generate the grpc php code

please advice how to use it.

thank you

stanley-cheung commented 7 years ago

Please follow this instruction to install protoc-gen-php. Please let me know if that doesn't work.

nevernet commented 7 years ago

my steps as bleow:

git clone -b v1.0.0 https://github.com/grpc/grpc --depth=1
cd grpc 
git checkout -b v1.0.0

cd examples/php
composer install

composer outputs:

Loading composer repositories with package information
Updating dependencies (including require-dev)
  - Installing stanley-cheung/protobuf-php (dev-master e15c884)
    Cloning e15c8846bd90a2638a2017e6b6663cd195885300 from cache

  - Installing grpc/grpc (v0.15.2)
    Downloading: 100%

Writing lock file
Generating autoload files

attention protobuf-php version is: dev-master

next step:

cd vendor/stanley-cheung/protobuf-php
sudo gem install rake ronn
//outputs
//Successfully installed rake-11.3.0
//Parsing documentation for rake-11.3.0
//Successfully installed ronn-0.7.3
//Parsing documentation for ronn-0.7.3
//2 gems installed

rake pear:package version=1.0
sudo pear install Protobuf-1.0.tgz

//outputs
//downloading Console_CommandLine-1.2.2.tgz ...
//Starting to download Console_CommandLine-1.2.2.tgz (40,705 bytes)
//...........done: 40,705 bytes
//install ok: channel://pear.php.net/Console_CommandLine-1.2.2
//install ok: channel://__uri/Protobuf-1.0

//back to route_guide folder:
cd ../../../route_guide

//current folder is: 
//github/grpc/examples/php/route_guide

protoc-gen-php -i . -o . ./route_guide.proto
//outputs
//zsh: command not found: protoc-gen-php
// (because there is no global command which named protoc-gen-php)
nevernet commented 7 years ago

@stanley-cheung please above comment thank you

nevernet commented 7 years ago

additional debugs

cd $(pear config-get php_dir)
pwd
/usr/local/Cellar/php56/5.6.27_4/lib/php

the DrSlump was installed in this folder

total 200
drwxrwxr-x  24 qinxin  admin   816B 10 26 09:35 .
drwxr-xr-x   3 qinxin  admin   102B 10 19 13:10 ..
drwxrwxr-x   7 qinxin  admin   238B 10 19 13:10 .channels
-r--r--r--   1 qinxin  admin   2.7K 10 26 13:02 .depdb
-r--r--r--   1 qinxin  admin     0B 10 26 13:02 .depdblock
-r--r--r--   1 qinxin  admin    18K 10 26 13:02 .filemap
-r--r--r--   1 qinxin  admin     0B 10 26 13:02 .lock
drwxrwxr-x  10 qinxin  admin   340B 10 26 13:02 .registry
drwxrwxr-x   3 qinxin  admin   102B 10 19 13:10 Archive
drwxrwxr-x   5 qinxin  admin   170B 10 26 13:02 Console
drwxr-xr-x   4 qinxin  admin   136B 10 26 13:02 DrSlump
drwxrwxr-x   3 qinxin  admin   102B 10 19 13:10 OS
drwxrwxr-x  31 qinxin  admin   1.0K 10 19 13:10 PEAR
-r--r--r--   1 qinxin  admin    35K 10 19 13:10 PEAR.php
drwxrwxr-x   4 qinxin  admin   136B 10 19 13:10 Structures
-r--r--r--   1 qinxin  admin    20K 10 19 13:10 System.php
drwxrwxr-x   3 qinxin  admin   102B 10 19 13:10 XML
drwxrwxr-x  13 qinxin  admin   442B 10 19 13:10 build
drwxrwxr-x   4 qinxin  admin   136B 10 25 16:57 data
drwxrwxr-x   9 qinxin  admin   306B 10 26 09:35 doc
drwxr-xr-x   3 qinxin  admin   102B 10 19 13:38 extensions
-r--r--r--   1 qinxin  admin    15K 10 19 13:10 pearcmd.php
-r--r--r--   1 qinxin  admin   1.0K 10 19 13:10 peclcmd.php
drwxrwxr-x   7 qinxin  admin   238B 10 25 16:57 test
nevernet commented 7 years ago

so have to use command from vendor folder: ./vendor/bin/protoc-gen-php.php -i . -o . helloworld.proto

nevernet commented 7 years ago
 // 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);
        }

cause the $_SERVER['argc'] is greater than 2, so the $stdin is empty always, so it will always execute self::runProtoc($pluginExecutable); then exit(0)