David Reiss | 755c815 | 2009-03-26 06:15:05 +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 | */ |
David Reiss | 755c815 | 2009-03-26 06:15:05 +0000 | [diff] [blame] | 38 | |
| 39 | /* |
| 40 | * This is not a stand-alone server. It should be run as a normal |
| 41 | * php web script (like through Apache's mod_php) or as a cgi script |
| 42 | * (like with the included runserver.py). You can connect to it with |
| 43 | * THttpClient in any language that supports it. The PHP tutorial client |
| 44 | * will work if you pass it the argument "--http". |
| 45 | */ |
| 46 | |
| 47 | if (php_sapi_name() == 'cli') { |
| 48 | ini_set("display_errors", "stderr"); |
| 49 | } |
| 50 | |
Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 51 | use Thrift\Protocol\TBinaryProtocol; |
| 52 | use Thrift\Transport\TPhpStream; |
| 53 | use Thrift\Transport\TBufferedTransport; |
David Reiss | 755c815 | 2009-03-26 06:15:05 +0000 | [diff] [blame] | 54 | |
Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 55 | class CalculatorHandler implements \tutorial\CalculatorIf { |
David Reiss | 755c815 | 2009-03-26 06:15:05 +0000 | [diff] [blame] | 56 | protected $log = array(); |
| 57 | |
| 58 | public function ping() { |
| 59 | error_log("ping()"); |
| 60 | } |
| 61 | |
| 62 | public function add($num1, $num2) { |
| 63 | error_log("add({$num1}, {$num2})"); |
| 64 | return $num1 + $num2; |
| 65 | } |
| 66 | |
Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 67 | public function calculate($logid, \tutorial\Work $w) { |
David Reiss | 755c815 | 2009-03-26 06:15:05 +0000 | [diff] [blame] | 68 | error_log("calculate({$logid}, {{$w->op}, {$w->num1}, {$w->num2}})"); |
| 69 | switch ($w->op) { |
Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 70 | case \tutorial\Operation::ADD: |
David Reiss | 755c815 | 2009-03-26 06:15:05 +0000 | [diff] [blame] | 71 | $val = $w->num1 + $w->num2; |
| 72 | break; |
Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 73 | case \tutorial\Operation::SUBTRACT: |
David Reiss | 755c815 | 2009-03-26 06:15:05 +0000 | [diff] [blame] | 74 | $val = $w->num1 - $w->num2; |
| 75 | break; |
Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 76 | case \tutorial\Operation::MULTIPLY: |
David Reiss | 755c815 | 2009-03-26 06:15:05 +0000 | [diff] [blame] | 77 | $val = $w->num1 * $w->num2; |
| 78 | break; |
Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 79 | case \tutorial\Operation::DIVIDE: |
David Reiss | 755c815 | 2009-03-26 06:15:05 +0000 | [diff] [blame] | 80 | if ($w->num2 == 0) { |
Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 81 | $io = new \tutorial\InvalidOperation(); |
David Reiss | 755c815 | 2009-03-26 06:15:05 +0000 | [diff] [blame] | 82 | $io->what = $w->op; |
| 83 | $io->why = "Cannot divide by 0"; |
| 84 | throw $io; |
| 85 | } |
| 86 | $val = $w->num1 / $w->num2; |
| 87 | break; |
| 88 | default: |
Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 89 | $io = new \tutorial\InvalidOperation(); |
David Reiss | 755c815 | 2009-03-26 06:15:05 +0000 | [diff] [blame] | 90 | $io->what = $w->op; |
| 91 | $io->why = "Invalid Operation"; |
| 92 | throw $io; |
| 93 | } |
| 94 | |
Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 95 | $log = new \shared\SharedStruct(); |
David Reiss | 755c815 | 2009-03-26 06:15:05 +0000 | [diff] [blame] | 96 | $log->key = $logid; |
| 97 | $log->value = (string)$val; |
| 98 | $this->log[$logid] = $log; |
| 99 | |
| 100 | return $val; |
| 101 | } |
| 102 | |
| 103 | public function getStruct($key) { |
| 104 | error_log("getStruct({$key})"); |
| 105 | // This actually doesn't work because the PHP interpreter is |
| 106 | // restarted for every request. |
| 107 | //return $this->log[$key]; |
Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 108 | return new \shared\SharedStruct(array("key" => $key, "value" => "PHP is stateless!")); |
David Reiss | 755c815 | 2009-03-26 06:15:05 +0000 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | public function zip() { |
| 112 | error_log("zip()"); |
| 113 | } |
| 114 | |
| 115 | }; |
| 116 | |
| 117 | header('Content-Type', 'application/x-thrift'); |
| 118 | if (php_sapi_name() == 'cli') { |
| 119 | echo "\r\n"; |
| 120 | } |
| 121 | |
| 122 | $handler = new CalculatorHandler(); |
Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 123 | $processor = new \tutorial\CalculatorProcessor($handler); |
David Reiss | 755c815 | 2009-03-26 06:15:05 +0000 | [diff] [blame] | 124 | |
| 125 | $transport = new TBufferedTransport(new TPhpStream(TPhpStream::MODE_R | TPhpStream::MODE_W)); |
| 126 | $protocol = new TBinaryProtocol($transport, true, true); |
| 127 | |
| 128 | $transport->open(); |
| 129 | $processor->process($protocol, $protocol); |
| 130 | $transport->close(); |