| David Reiss | 755c815 | 2009-03-26 06:15:05 +0000 | [diff] [blame] | 1 | #!/usr/bin/env php | 
 | 2 | <?php | 
 | 3 |  | 
 | 4 | /* | 
 | 5 |  * This is not a stand-alone server.  It should be run as a normal | 
 | 6 |  * php web script (like through Apache's mod_php) or as a cgi script | 
 | 7 |  * (like with the included runserver.py).  You can connect to it with | 
 | 8 |  * THttpClient in any language that supports it.  The PHP tutorial client | 
 | 9 |  * will work if you pass it the argument "--http". | 
 | 10 |  */ | 
 | 11 |  | 
 | 12 | if (php_sapi_name() == 'cli') { | 
 | 13 |   ini_set("display_errors", "stderr"); | 
 | 14 | } | 
 | 15 |  | 
 | 16 | $GLOBALS['THRIFT_ROOT'] = realpath(dirname(__FILE__).'/../..').'/lib/php/src'; | 
 | 17 |  | 
 | 18 | require_once $GLOBALS['THRIFT_ROOT'].'/Thrift.php'; | 
 | 19 | require_once $GLOBALS['THRIFT_ROOT'].'/protocol/TBinaryProtocol.php'; | 
 | 20 | require_once $GLOBALS['THRIFT_ROOT'].'/transport/TPhpStream.php'; | 
 | 21 | require_once $GLOBALS['THRIFT_ROOT'].'/transport/TBufferedTransport.php'; | 
 | 22 |  | 
 | 23 | /** | 
 | 24 |  * Suppress errors in here, which happen because we have not installed into | 
 | 25 |  * $GLOBALS['THRIFT_ROOT'].'/packages/tutorial' like we are supposed to! | 
 | 26 |  * | 
 | 27 |  * Normally we would only have to include Calculator.php which would properly | 
 | 28 |  * include the other files from their packages/ folder locations, but we | 
 | 29 |  * include everything here due to the bogus path setup. | 
 | 30 |  */ | 
 | 31 | error_reporting(E_NONE); | 
 | 32 | $GEN_DIR = realpath(dirname(__FILE__).'/..').'/gen-php'; | 
 | 33 | require_once $GEN_DIR.'/SharedService.php'; | 
 | 34 | require_once $GEN_DIR.'/shared_types.php'; | 
 | 35 | require_once $GEN_DIR.'/Calculator.php'; | 
 | 36 | require_once $GEN_DIR.'/tutorial_types.php'; | 
 | 37 | error_reporting(E_ALL); | 
 | 38 |  | 
 | 39 | class CalculatorHandler implements CalculatorIf { | 
 | 40 |   protected $log = array(); | 
 | 41 |  | 
 | 42 |   public function ping() { | 
 | 43 |     error_log("ping()"); | 
 | 44 |   } | 
 | 45 |  | 
 | 46 |   public function add($num1, $num2) { | 
 | 47 |     error_log("add({$num1}, {$num2})"); | 
 | 48 |     return $num1 + $num2; | 
 | 49 |   } | 
 | 50 |  | 
 | 51 |   public function calculate($logid, $w) { | 
 | 52 |     error_log("calculate({$logid}, {{$w->op}, {$w->num1}, {$w->num2}})"); | 
 | 53 |     switch ($w->op) { | 
 | 54 |       case tutorial_Operation::ADD: | 
 | 55 |         $val = $w->num1 + $w->num2; | 
 | 56 |         break; | 
 | 57 |       case tutorial_Operation::SUBTRACT: | 
 | 58 |         $val = $w->num1 - $w->num2; | 
 | 59 |         break; | 
 | 60 |       case tutorial_Operation::MULTIPLY: | 
 | 61 |         $val = $w->num1 * $w->num2; | 
 | 62 |         break; | 
 | 63 |       case tutorial_Operation::DIVIDE: | 
 | 64 |         if ($w->num2 == 0) { | 
 | 65 |           $io = new tutorial_InvalidOperation(); | 
 | 66 |           $io->what = $w->op; | 
 | 67 |           $io->why = "Cannot divide by 0"; | 
 | 68 |           throw $io; | 
 | 69 |         } | 
 | 70 |         $val = $w->num1 / $w->num2; | 
 | 71 |         break; | 
 | 72 |       default: | 
 | 73 |         $io = new tutorial_InvalidOperation(); | 
 | 74 |         $io->what = $w->op; | 
 | 75 |         $io->why = "Invalid Operation"; | 
 | 76 |         throw $io; | 
 | 77 |     } | 
 | 78 |  | 
 | 79 |     $log = new SharedStruct(); | 
 | 80 |     $log->key = $logid; | 
 | 81 |     $log->value = (string)$val; | 
 | 82 |     $this->log[$logid] = $log; | 
 | 83 |  | 
 | 84 |     return $val; | 
 | 85 |   } | 
 | 86 |  | 
 | 87 |   public function getStruct($key) { | 
 | 88 |     error_log("getStruct({$key})"); | 
 | 89 |     // This actually doesn't work because the PHP interpreter is | 
 | 90 |     // restarted for every request. | 
 | 91 |     //return $this->log[$key]; | 
 | 92 |     return new SharedStruct(array("key" => $key, "value" => "PHP is stateless!")); | 
 | 93 |   } | 
 | 94 |  | 
 | 95 |   public function zip() { | 
 | 96 |     error_log("zip()"); | 
 | 97 |   } | 
 | 98 |  | 
 | 99 | }; | 
 | 100 |  | 
 | 101 | header('Content-Type', 'application/x-thrift'); | 
 | 102 | if (php_sapi_name() == 'cli') { | 
 | 103 |   echo "\r\n"; | 
 | 104 | } | 
 | 105 |  | 
 | 106 | $handler = new CalculatorHandler(); | 
 | 107 | $processor = new CalculatorProcessor($handler); | 
 | 108 |  | 
 | 109 | $transport = new TBufferedTransport(new TPhpStream(TPhpStream::MODE_R | TPhpStream::MODE_W)); | 
 | 110 | $protocol = new TBinaryProtocol($transport, true, true); | 
 | 111 |  | 
 | 112 | $transport->open(); | 
 | 113 | $processor->process($protocol, $protocol); | 
 | 114 | $transport->close(); |