]>
Commit | Line | Data |
---|---|---|
f67539c2 TL |
1 | #!/usr/bin/env php |
2 | <?php | |
3 | ||
4 | namespace tutorial\php; | |
5 | ||
6 | error_reporting(E_ALL); | |
7 | ||
8 | require_once __DIR__.'/../../vendor/autoload.php'; | |
9 | ||
10 | use Thrift\ClassLoader\ThriftClassLoader; | |
11 | ||
12 | $GEN_DIR = realpath(dirname(__FILE__).'/..').'/gen-php'; | |
13 | ||
14 | $loader = new ThriftClassLoader(); | |
15 | $loader->registerNamespace('Thrift', __DIR__ . '/../../lib/php/lib'); | |
16 | $loader->registerNamespace('shared', $GEN_DIR); | |
17 | $loader->registerNamespace('tutorial', $GEN_DIR); | |
18 | $loader->register(); | |
19 | ||
20 | /* | |
21 | * Licensed to the Apache Software Foundation (ASF) under one | |
22 | * or more contributor license agreements. See the NOTICE file | |
23 | * distributed with this work for additional information | |
24 | * regarding copyright ownership. The ASF licenses this file | |
25 | * to you under the Apache License, Version 2.0 (the | |
26 | * "License"); you may not use this file except in compliance | |
27 | * with the License. You may obtain a copy of the License at | |
28 | * | |
29 | * http://www.apache.org/licenses/LICENSE-2.0 | |
30 | * | |
31 | * Unless required by applicable law or agreed to in writing, | |
32 | * software distributed under the License is distributed on an | |
33 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
34 | * KIND, either express or implied. See the License for the | |
35 | * specific language governing permissions and limitations | |
36 | * under the License. | |
37 | */ | |
38 | ||
39 | use Thrift\Protocol\TBinaryProtocol; | |
40 | use Thrift\Transport\TSocket; | |
41 | use Thrift\Transport\THttpClient; | |
42 | use Thrift\Transport\TBufferedTransport; | |
43 | use Thrift\Exception\TException; | |
44 | ||
45 | try { | |
46 | if (array_search('--http', $argv)) { | |
47 | $socket = new THttpClient('localhost', 8080, '/php/PhpServer.php'); | |
48 | } else { | |
49 | $socket = new TSocket('localhost', 9090); | |
50 | } | |
51 | $transport = new TBufferedTransport($socket, 1024, 1024); | |
52 | $protocol = new TBinaryProtocol($transport); | |
53 | $client = new \tutorial\CalculatorClient($protocol); | |
54 | ||
55 | $transport->open(); | |
56 | ||
57 | $client->ping(); | |
58 | print "ping()\n"; | |
59 | ||
60 | $sum = $client->add(1,1); | |
61 | print "1+1=$sum\n"; | |
62 | ||
63 | $work = new \tutorial\Work(); | |
64 | ||
65 | $work->op = \tutorial\Operation::DIVIDE; | |
66 | $work->num1 = 1; | |
67 | $work->num2 = 0; | |
68 | ||
69 | try { | |
70 | $client->calculate(1, $work); | |
71 | print "Whoa! We can divide by zero?\n"; | |
72 | } catch (\tutorial\InvalidOperation $io) { | |
73 | print "InvalidOperation: $io->why\n"; | |
74 | } | |
75 | ||
76 | $work->op = \tutorial\Operation::SUBTRACT; | |
77 | $work->num1 = 15; | |
78 | $work->num2 = 10; | |
79 | $diff = $client->calculate(1, $work); | |
80 | print "15-10=$diff\n"; | |
81 | ||
82 | $log = $client->getStruct(1); | |
83 | print "Log: $log->value\n"; | |
84 | ||
85 | $transport->close(); | |
86 | ||
87 | } catch (TException $tx) { | |
88 | print 'TException: '.$tx->getMessage()."\n"; | |
89 | } | |
90 | ||
91 | ?> |