Add php protobuffer support for transition to GTFS-realtime
[busui.git] / lib / Protobuf-PHP / library / DrSlump / Protobuf / Compiler / Cli.php
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
<?php
 
namespace DrSlump\Protobuf\Compiler;
 
require_once 'Console/CommandLine.php';
 
use DrSlump\Protobuf;
 
class Cli
{
    public static function run($pluginExecutable)
    {
        // Open STDIN in non-blocking mode
        $fp = fopen('php://stdin', 'rb');
        stream_set_blocking($fp, FALSE);
 
        // Loop until STDIN is closed or we've waited too long for data
        $cnt = 0;
        $stdin = '';
        while (!feof($fp) && $cnt++ < 10) {
            // give protoc some time to feed the data
            usleep(10000);
            // read the bytes
            $bytes = fread($fp, 1024);
            if (strlen($bytes)) {
                $cnt = 0;
                $stdin .= $bytes;
            }
        }
 
        // If no input was given we launch protoc from here
        if (0 === strlen($stdin)) {
            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);
        }
    }
 
    public static function runProtoc($pluginExecutable)
    {
        $result = self::parseArguments();
 
        $protocBin = $result->options['protoc'];
 
        // Check if protoc is available
        exec("$protocBin --version", $output, $return);
 
        if (0 !== $return && 1 !== $return) {
            fputs(STDERR, "ERROR: Unable to find the protoc command.". PHP_EOL);
            fputs(STDERR, "       Please make sure it's installed and available in the path." . PHP_EOL);
            exit(1);
        }
 
        if (!preg_match('/[0-9\.]+/', $output[0], $m)) {
            fputs(STDERR, "ERROR: Unable to get protoc command version.". PHP_EOL);
            fputs(STDERR, "       Please make sure it's installed and available in the path." . PHP_EOL);
            exit(1);
        }
 
        if (version_compare($m[0], '2.3.0') < 0) {
            fputs(STDERR, "ERROR: The protoc command in your system is too old." . PHP_EOL);
            fputs(STDERR, "       Minimum version required is 2.3.0 but found {$m[0]}." . PHP_EOL);
            exit(1);
        }
 
        $cmd[] = $protocBin;
        $cmd[] = '--plugin=protoc-gen-php=' . escapeshellarg($pluginExecutable);
 
        // Include paths
        $cmd[] = '--proto_path=' . escapeshellarg(__DIR__ . DIRECTORY_SEPARATOR . 'protos');
        if (!empty($result->options['include'])) {
            foreach($result->options['include'] as $include) {
                $include = realpath($include);
                $cmd[] = '--proto_path=' . escapeshellarg($include);
            }
        }
 
        // Convert proto files to absolute paths
        $protos = array();
        foreach ($result->args['protos'] as $proto) {
            $realpath = realpath($proto);
            if (FALSE === $realpath) {
                fputs(STDERR, "ERROR: File '$proto' does not exists");
                exit(1);
            }
 
            $protos[] = $realpath;
        }
 
        // Protoc will pass custom arguments to the plugin if they are given
        // before a colon character. ie: --php_out="foo=bar:/path/to/plugin"
        // We make use of it to pass arguments encoded as an URI query string
 
        $args = array();
        if ($result->options['comments']) {
            $args['comments'] = 1;
            // Protos are only needed for comments right now
            $args['protos'] = $protos;
        }
        if ($result->options['verbose']) {
            $args['verbose'] = 1;
        }
        if ($result->options['json']) {
            $args['json'] = 1;
        }
        if ($result->options['skipImported']) {
            $args['skip-imported'] = 1;
        }
        if ($result->options['define']) {
            $args['options'] = array();
            foreach($result->options['define'] as $define) {
                $parts = explode('=', $define);
                $parts = array_filter(array_map('trim', $parts));
                if (count($parts) === 1) {
                    $parts[1] = 1;
                }
                $args['options'][$parts[0]] = $parts[1];
            }
        }
 
        $cmd[] = '--php_out=' .
                 escapeshellarg(
                     http_build_query($args, '', '&') .
                     ':' .
                     $result->options['out']
                 );
 
        // Add the chosen proto files to generate
        foreach ($protos as $proto) {
            $cmd[] = escapeshellarg($proto);
        }
 
        $cmdStr = implode(' ', $cmd);
 
        // Run command with stderr redirected to stdout
        passthru($cmdStr . ' 2>&1', $return);
 
        if ($return !== 0) {
            fputs(STDERR, PHP_EOL);
            fputs(STDERR, 'ERROR: protoc exited with an error (' . $return . ') when executed with: ' . PHP_EOL);
            fputs(STDERR, '  ' . implode(" \\\n    ", $cmd) . PHP_EOL);
            exit($return);
        }
    }
 
 
    public static function parseArguments()
    {
        $main = new \Console_CommandLine();
 
        $main->addOption('out', array(
            'short_name'    => '-o',
            'long_name'     => '--out',
            'action'        => 'StoreString',
            'description'   => 'destination directory for generated files',
            'default'       => './',
        ));
 
        $main->addOption('include', array(
            'short_name'    => '-i',
            'long_name'     => '--include',
            'action'        => 'StoreArray',
            'description'   => 'define an include path (can be repeated)',
            'multiple'      => true,
        ));
 
 
        $main->addOption('json', array(
            'short_name'    => '-j',
            'long_name'     => '--json',
            'action'        => 'StoreTrue',
            'description'   => 'turn on ProtoJson Javascript file generation',
        ));
 
        $main->addOption('protoc', array(
            'long_name'     => '--protoc',
            'action'        => 'StoreString',
            'default'       => 'protoc',
            'description'   => 'protoc compiler executable path',
        ));
 
        $main->addOption('skipImported', array(
            'long_name'     => '--skip-imported',
            'action'        => 'StoreTrue',
            'default'       => false,
            'description'   => 'do not generate imported proto files',
        ));
 
        $main->addOption('comments', array(
            'long_name'     => '--comments',
            'action'        => 'StoreTrue',
            'description'   => 'port .proto comments to generated code',
        ));
 
        $main->addOption('define', array(
            'short_name'    => '-D',
            'long_name'     => '--define',
            'action'        => 'StoreArray',
            'multiple'      => true,
            'description'   => 'define a generator option (ie: -Dmultifile -Dsuffix=pb.php)',
        ));
 
        $main->addOption('verbose', array(
            'short_name'    => '-v',
            'long_name'     => '--verbose',
            'action'        => 'StoreTrue',
            'description'   => 'turn on verbose output',
        ));
 
 
        $main->addArgument('protos', array(
            'multiple'      => true,
            'description'   => 'proto files',
        ));
 
        try {
            echo 'Protobuf-PHP ' . Protobuf::VERSION . ' by Ivan -DrSlump- Montes' . PHP_EOL . PHP_EOL;
            $result = $main->parse();
            return $result;
        } catch (\Exception $e) {
            $main->displayError($e->getMessage());
            exit(1);
        }
    }
 
}