| Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 1 | #!/usr/bin/env php | 
|  | 2 | <?php | 
| Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 3 |  | 
|  | 4 | namespace tutorial\php; | 
|  | 5 |  | 
|  | 6 | error_reporting(E_ALL); | 
|  | 7 |  | 
|  | 8 | require_once __DIR__.'/../../lib/php/lib/Thrift/ClassLoader/ThriftClassLoader.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->registerDefinition('shared', $GEN_DIR); | 
|  | 17 | $loader->registerDefinition('tutorial', $GEN_DIR); | 
|  | 18 | $loader->register(); | 
|  | 19 |  | 
| David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 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 | */ | 
| Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 38 |  | 
| Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 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; | 
| Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 44 |  | 
| Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 45 | try { | 
| David Reiss | 755c815 | 2009-03-26 06:15:05 +0000 | [diff] [blame] | 46 | if (array_search('--http', $argv)) { | 
|  | 47 | $socket = new THttpClient('localhost', 8080, '/php/PhpServer.php'); | 
|  | 48 | } else { | 
|  | 49 | $socket = new TSocket('localhost', 9090); | 
|  | 50 | } | 
| Mark Slee | 7679196 | 2007-03-14 02:47:35 +0000 | [diff] [blame] | 51 | $transport = new TBufferedTransport($socket, 1024, 1024); | 
|  | 52 | $protocol = new TBinaryProtocol($transport); | 
| Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 53 | $client = new \tutorial\CalculatorClient($protocol); | 
| David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 54 |  | 
| Mark Slee | 7679196 | 2007-03-14 02:47:35 +0000 | [diff] [blame] | 55 | $transport->open(); | 
| David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 56 |  | 
| Mark Slee | 7679196 | 2007-03-14 02:47:35 +0000 | [diff] [blame] | 57 | $client->ping(); | 
|  | 58 | print "ping()\n"; | 
| David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 59 |  | 
| Mark Slee | 7679196 | 2007-03-14 02:47:35 +0000 | [diff] [blame] | 60 | $sum = $client->add(1,1); | 
|  | 61 | print "1+1=$sum\n"; | 
| David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 62 |  | 
| Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 63 | $work = new \tutorial\Work(); | 
| David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 64 |  | 
| Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 65 | $work->op = \tutorial\Operation::DIVIDE; | 
| Mark Slee | 7679196 | 2007-03-14 02:47:35 +0000 | [diff] [blame] | 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"; | 
| Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 72 | } catch (\tutorial\InvalidOperation $io) { | 
| Mark Slee | 7679196 | 2007-03-14 02:47:35 +0000 | [diff] [blame] | 73 | print "InvalidOperation: $io->why\n"; | 
|  | 74 | } | 
| David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 75 |  | 
| Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 76 | $work->op = \tutorial\Operation::SUBTRACT; | 
| Mark Slee | 7679196 | 2007-03-14 02:47:35 +0000 | [diff] [blame] | 77 | $work->num1 = 15; | 
|  | 78 | $work->num2 = 10; | 
|  | 79 | $diff = $client->calculate(1, $work); | 
|  | 80 | print "15-10=$diff\n"; | 
| David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 81 |  | 
| Mark Slee | 7679196 | 2007-03-14 02:47:35 +0000 | [diff] [blame] | 82 | $log = $client->getStruct(1); | 
|  | 83 | print "Log: $log->value\n"; | 
| David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 84 |  | 
| Mark Slee | 7679196 | 2007-03-14 02:47:35 +0000 | [diff] [blame] | 85 | $transport->close(); | 
| David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 86 |  | 
| Mark Slee | 7679196 | 2007-03-14 02:47:35 +0000 | [diff] [blame] | 87 | } catch (TException $tx) { | 
|  | 88 | print 'TException: '.$tx->getMessage()."\n"; | 
| Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 89 | } | 
|  | 90 |  | 
| Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 91 | ?> |